-
Notifications
You must be signed in to change notification settings - Fork 171
Add Additional Namespace & Catalog Request Support #284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
fe9b6b8
ba19c85
8869f17
86d60f2
ec233ed
1f61779
92530f7
4295689
38dc592
4f6bf2b
426dd67
3ca6dbc
7b0333b
fe37223
642ece9
481e66c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
CATgwalker marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,97 @@ | ||||||
| <# | ||||||
| .SYNOPSIS | ||||||
| Submit a catalog request using Service Catalog API | ||||||
|
|
||||||
| .DESCRIPTION | ||||||
| Create a new catalog item request using Service Catalog API. Reference: https://www.servicenow.com/community/itsm-articles/submit-catalog-request-using-service-catalog-api/ta-p/2305836 | ||||||
|
|
||||||
| .PARAMETER CatalogItemName | ||||||
| Name of the catalog item that will be created | ||||||
|
|
||||||
| .PARAMETER CatalogItemID | ||||||
| SysID of the catalog item that will be created | ||||||
|
|
||||||
| .PARAMETER Variables | ||||||
| Key/value pairs of variable names and their values | ||||||
|
|
||||||
| .PARAMETER PassThru | ||||||
| If provided, the new record will be returned | ||||||
|
|
||||||
| .PARAMETER Connection | ||||||
| Azure Automation Connection object containing username, password, and URL for the ServiceNow instance | ||||||
|
|
||||||
| .PARAMETER ServiceNowSession | ||||||
| ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession. | ||||||
|
|
||||||
| .EXAMPLE | ||||||
| New-ServiceNowRecord -CatalogItemName "Standard Laptop" -Variables @{'acrobat' = 'true'; 'photoshop' = 'true'; ' Additional_software_requirements' = 'Testing Service catalog API' } | ||||||
CATgwalker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| Raise a new catalog request using Item Name | ||||||
|
|
||||||
| .EXAMPLE | ||||||
| New-ServiceNowRecord -CatalogItemID "04b7e94b4f7b42000086eeed18110c7fd" -Variables @{'acrobat' = 'true'; 'photoshop' = 'true'; ' Additional_software_requirements' = 'Testing Service catalog API' } | ||||||
CATgwalker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| Raise a new catalog request using Item ID | ||||||
|
|
||||||
| .INPUTS | ||||||
| InputData | ||||||
|
|
||||||
| .OUTPUTS | ||||||
| PSCustomObject if PassThru provided | ||||||
| #> | ||||||
| function New-ServiceNowCatalogItem { | ||||||
| [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'ID')] | ||||||
| param | ||||||
| ( | ||||||
| [Parameter(Mandatory, ParameterSetName = 'Name')] | ||||||
| [string]$CatalogItemName, | ||||||
| [Parameter(Mandatory, ParameterSetName = 'ID')] | ||||||
| [string]$CatalogItemID, | ||||||
CATgwalker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| [Parameter(Mandatory, ParameterSetName = 'Name')] | ||||||
| [Parameter(Mandatory, ParameterSetName = 'ID')] | ||||||
| [Alias('Variables')] | ||||||
| [hashtable]$InputData, | ||||||
| [Parameter()][Hashtable]$Connection, | ||||||
| [Parameter()][hashtable]$ServiceNowSession = $script:ServiceNowSession, | ||||||
| [Parameter()][switch]$PassThru | ||||||
| ) | ||||||
|
|
||||||
| begin { | ||||||
| if ($CatalogItemName) { | ||||||
| #Lookup the sys_id of the Catalog Item name | ||||||
| $CatalogItemID = (Get-ServiceNowRecord -Table sc_cat_item -AsValue -Filter @('name', '-eq', $CatalogItemName )).sys_id | ||||||
CATgwalker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| if ([string]::IsNullOrEmpty($CatalogItemID)) { throw "Unable to find catalog item by name '$($catalogitemname)'" } else { Write-Verbose "Found $($catalogitemid) via lookup from '$($CatalogItemName)'" } | ||||||
| } | ||||||
| } | ||||||
| process { | ||||||
|
|
||||||
| $AddItemToCart = @{ | ||||||
| Method = 'Post' | ||||||
| UriLeaf = "/servicecatalog/items/{0}/add_to_cart" -f $CatalogItemID | ||||||
| Values = @{'sysparm_quantity' = 1; 'variables' = $InputData } | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should make quantity a function parameter along with the other request body parameters, sysparm_also_request_for and sysparm_requested_for. https://www.servicenow.com/docs/bundle/zurich-api-reference/page/integrate/inbound-rest/concept/c_ServiceCatalogAPI.html#title_servicecat-POST-items-add_to_cart. add examples for these new parameters as well. |
||||||
| Namespace = 'sn_sc' | ||||||
| Connection = $Connection | ||||||
| ServiceNowSession = $ServiceNowSession | ||||||
| } | ||||||
|
|
||||||
| if ( $PSCmdlet.ShouldProcess($CatalogItemID, 'Create new catalog item request') ) { | ||||||
|
|
||||||
| $AddItemCartResponse = Invoke-ServiceNowRestMethod @AddItemToCart | ||||||
|
|
||||||
| if ($AddItemCartResponse.cart_id) { | ||||||
|
||||||
| $SubmitOrder = @{ | ||||||
| Method = 'Post' | ||||||
| UriLeaf = "/servicecatalog/cart/submit_order" | ||||||
| Namespace = 'sn_sc' | ||||||
| Connection = $Connection | ||||||
| ServiceNowSession = $ServiceNowSession | ||||||
| } | ||||||
|
|
||||||
| $SubmitOrderResponse = Invoke-ServiceNowRestMethod @SubmitOrder | ||||||
| } | ||||||
| if ( $PassThru ) { | ||||||
| $SubmitOrderResponse | ||||||
|
||||||
| $SubmitOrderResponse | |
| $SubmitOrderResponse | Select-Object @{'n'='number';'e'={$_.request_number}}, request_id |
consider updating the output to something like the above so it can be piped directly into the other functions, eg. Get-ServiceNowRecord
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upon further review of the API documentation, I'm not sure how this would behave when a two-step checkout process is configured.
I do not have a way to test two-step checkout at the moment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: needs to be tested to validate the return behavior when multiple quantities of a catalog item or multiple catalog items are returned from the submit_order POST.
CATgwalker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
CATgwalker marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.