Skip to content

Commit fe37223

Browse files
committed
Added delayed checkout functionality, pipeline support of returned request response,
1 parent 7b0333b commit fe37223

File tree

2 files changed

+80
-10
lines changed

2 files changed

+80
-10
lines changed

ServiceNow/Public/New-ServiceNowCatalogItem.ps1

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
.PARAMETER Variables
1212
Key/value pairs of variable names and their values
1313
14+
.PARAMETER CheckoutImmediately
15+
If provided, a second Post for cart checkout to submit_order API Endpoint will be sent
16+
1417
.PARAMETER PassThru
1518
If provided, the new record will be returned
1619
@@ -47,20 +50,21 @@ function New-ServiceNowCatalogItem {
4750
[hashtable]$InputData,
4851
[Parameter()][Hashtable]$Connection,
4952
[Parameter()][hashtable]$ServiceNowSession = $script:ServiceNowSession,
53+
[Parameter()][switch]$CheckoutImmediately,
5054
[Parameter()][switch]$PassThru
5155
)
5256

5357
begin {
58+
if (-not $PSBoundParameters.ContainsKey('CheckoutImmediately')) {
59+
$CheckoutImmediately = $false
60+
}
5461
if ($CatalogItem -match '^[a-zA-Z0-9]{32}$') {
5562
#Verify the sys_id of the Catalog Item
56-
$CatalogItemID = (Get-ServiceNowRecord -Table sc_cat_item -AsValue -ID $CatalogItem).sys_id
57-
<<<<<<< HEAD
63+
$CatalogItemID = Get-ServiceNowRecord -Table sc_cat_item -AsValue -ID $CatalogItem -Property sys_id
5864
if ([string]::IsNullOrEmpty($CatalogItemID)) { throw "Unable to find catalog item by ID '$($CatalogItem)'" } else { Write-Verbose "Found $($catalogitemid) via lookup from '$($CatalogItem)'" }
59-
=======
60-
>>>>>>> 426dd6771816051effc02db641c4428a78259537
6165
} else {
6266
#Lookup the sys_id of the Catalog Item
63-
$CatalogItemID = (Get-ServiceNowRecord -Table sc_cat_item -AsValue -Filter @('name', '-eq', $CatalogItem )).sys_id
67+
$CatalogItemID = Get-ServiceNowRecord -Table sc_cat_item -AsValue -Filter @('name', '-eq', $CatalogItem ) -Property sys_id
6468
if ([string]::IsNullOrEmpty($CatalogItemID)) { throw "Unable to find catalog item by name '$($CatalogItem)'" } else { Write-Verbose "Found $($catalogitemid) via lookup from '$($CatalogItem)'" }
6569
}
6670
}
@@ -79,7 +83,7 @@ function New-ServiceNowCatalogItem {
7983

8084
$AddItemCartResponse = Invoke-ServiceNowRestMethod @AddItemToCart
8185

82-
if ($AddItemCartResponse.cart_id) {
86+
if ($AddItemCartResponse.cart_id -and $CheckoutImmediately) {
8387
$SubmitOrder = @{
8488
Method = 'Post'
8589
UriLeaf = "/servicecatalog/cart/submit_order"
@@ -89,10 +93,13 @@ function New-ServiceNowCatalogItem {
8993
}
9094

9195
$SubmitOrderResponse = Invoke-ServiceNowRestMethod @SubmitOrder
96+
97+
if ($PassThru) {
98+
$SubmitOrderResponse | Select-Object @{'n' = 'number'; 'e' = { $_.request_number } }, request_id
99+
}
92100
}
93-
if ( $PassThru ) {
94-
$SubmitOrderResponse
95-
}
101+
} else {
102+
$AddItemToCart | Out-String
96103
}
97104
}
98-
}
105+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<#
2+
.SYNOPSIS
3+
Submit a catalog request using Service Catalog API
4+
5+
.DESCRIPTION
6+
Checks out the user cart, based on the current check-out type (one-step or two-step). Reference: https://developer.servicenow.com/dev.do#!/reference/api/zurich/rest/c_ServiceCatalogAPI#servicecat-POST-cart-sub_order?navFilter=serv
7+
8+
.PARAMETER PassThru
9+
If provided, the new record will be returned
10+
11+
.PARAMETER Connection
12+
Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
13+
14+
.PARAMETER ServiceNowSession
15+
ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession.
16+
17+
.EXAMPLE
18+
Submit-ServiceNowCatalogOrder
19+
20+
Checks out the user cart, based on the current check-out type (one-step or two-step).
21+
22+
.EXAMPLE
23+
Submit-ServiceNowCatalogOrder -PassThru
24+
25+
Checks out the user cart, based on the current check-out type (one-step or two-step) and returns the request numbers as an object.
26+
27+
.INPUTS
28+
InputData
29+
30+
.OUTPUTS
31+
PSCustomObject if PassThru provided
32+
#>
33+
function Submit-ServiceNowCatalogOrder {
34+
[CmdletBinding(SupportsShouldProcess)]
35+
param
36+
(
37+
[Parameter()][Hashtable]$Connection,
38+
[Parameter()][hashtable]$ServiceNowSession = $script:ServiceNowSession,
39+
[Parameter()][switch]$PassThru
40+
)
41+
42+
process {
43+
44+
if ( $PSCmdlet.ShouldProcess('POST cart to Submit_Order API') ) {
45+
$SubmitOrder = @{
46+
Method = 'Post'
47+
UriLeaf = "/servicecatalog/cart/submit_order"
48+
Namespace = 'sn_sc'
49+
Connection = $Connection
50+
ServiceNowSession = $ServiceNowSession
51+
}
52+
53+
$SubmitOrderResponse = Invoke-ServiceNowRestMethod @SubmitOrder
54+
55+
if ($PassThru) {
56+
$SubmitOrderResponse | Select-Object @{'n' = 'number'; 'e' = { $_.request_number } }, request_id
57+
}
58+
59+
} else {
60+
Write-Output "Checks out the user cart, based on the current check-out type (one-step or two-step).`n`nIf one-step checkout, the method checks out (saves) the cart and returns the request number and the request order ID. If two-step checkout, the method returns the cart order status and all the information required for two-step checkout."
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)