Skip to content

Commit b523f18

Browse files
authored
Full service catalog cart lifecycle (#290)
1 parent 8f38f1c commit b523f18

9 files changed

+440
-170
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<#
2+
.SYNOPSIS
3+
Tests if a string is a valid ServiceNow sys_id format
4+
5+
.DESCRIPTION
6+
Validates that a string matches the ServiceNow sys_id format of a 32-character alphanumeric string
7+
8+
.PARAMETER InputObject
9+
The string value to test
10+
11+
.EXAMPLE
12+
Test-ServiceNowSysId -InputObject '9d385017c611228701d22104cc95c371'
13+
Returns $true
14+
15+
.EXAMPLE
16+
Test-ServiceNowSysId -InputObject 'INC0010001'
17+
Returns $false
18+
19+
.OUTPUTS
20+
System.Boolean
21+
#>
22+
function Test-ServiceNowSysId {
23+
[CmdletBinding()]
24+
[OutputType([System.Boolean])]
25+
param (
26+
[Parameter(Mandatory, ValueFromPipeline)]
27+
[AllowEmptyString()]
28+
[string] $InputObject
29+
)
30+
31+
process {
32+
if ([string]::IsNullOrEmpty($InputObject)) {
33+
return $false
34+
}
35+
36+
return $InputObject -match '^[a-zA-Z0-9]{32}$'
37+
}
38+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<#
2+
.SYNOPSIS
3+
Get the cart of the current user
4+
.DESCRIPTION
5+
Get the cart of the current user
6+
.PARAMETER ServiceNowSession
7+
ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession.
8+
.EXAMPLE
9+
Get-ServiceNowCart
10+
Get the cart of the current user
11+
.INPUTS
12+
None
13+
.OUTPUTS
14+
PSCustomObject representing the cart
15+
#>
16+
function Get-ServiceNowCart {
17+
param
18+
(
19+
[Parameter()]
20+
[hashtable]$ServiceNowSession = $script:ServiceNowSession
21+
)
22+
23+
$params = @{
24+
Method = 'Get'
25+
UriLeaf = "/servicecatalog/cart"
26+
Namespace = 'sn_sc'
27+
ServiceNowSession = $ServiceNowSession
28+
}
29+
Invoke-ServiceNowRestMethod @params | Select-Object @{n = 'sys_id'; e = { $_.cart_id } }, * -ExcludeProperty cart_id
30+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<#
2+
.SYNOPSIS
3+
Add item to the cart of the current user
4+
5+
.DESCRIPTION
6+
Adds a catalog item to the cart of the current user and optionally checks out the cart to create the order.
7+
8+
.PARAMETER CatalogItem
9+
Name or ID of the catalog item that will be created.
10+
Use Tab or Menu Completion to see available catalog items.
11+
12+
.PARAMETER Quantity
13+
Quantity of the catalog item to request. Default is 1.
14+
15+
.PARAMETER ItemValues
16+
Key/value pairs of variable names and their values
17+
18+
.PARAMETER Checkout
19+
Checkout the cart after adding the item to create the order.
20+
If not checking out, you can use Submit-ServiceNowCatalogOrder to checkout later.
21+
22+
.PARAMETER PassThru
23+
If provided, the new record, either cart addition or checked out order, will be returned
24+
25+
.PARAMETER ServiceNowSession
26+
ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession.
27+
28+
.EXAMPLE
29+
New-ServiceNowCartItem -CatalogItem "Standard Laptop"
30+
31+
Add 1 of the catalog item to the cart without checking out
32+
33+
.EXAMPLE
34+
New-ServiceNowCartItem -CatalogItem "Standard Laptop" -Quantity 3
35+
36+
Add 3 of the catalog item to the cart without checking out
37+
38+
.EXAMPLE
39+
New-ServiceNowCartItem -CatalogItem "Standard Laptop" -Quantity 3 -Checkout
40+
41+
Add 3 of the catalog item to the cart and checkout to create the order
42+
43+
.EXAMPLE
44+
'Standard Laptop', 'Adobe Acrobat Pro' | New-ServiceNowCartItem
45+
46+
Add multiple catalog items to the cart
47+
48+
.EXAMPLE
49+
New-ServiceNowCartItem -CatalogItem "Standard Laptop" -PassThru
50+
51+
Add a catalog item to the cart and return the cart details
52+
53+
.EXAMPLE
54+
New-ServiceNowCartItem -CatalogItem "Standard Laptop" -PassThru -Checkout
55+
56+
Add a catalog item to the cart, checkout to create the order, and return the order details
57+
58+
.EXAMPLE
59+
'Packaging and Shipping' | New-ServiceNowCartItem -ItemValues @{'type'='Inter-office';'parcel_details'='fragile'}
60+
61+
Add a catalog item to the cart with mandatory values
62+
63+
.EXAMPLE
64+
New-ServiceNowCartItem -CatalogItem 'ce40793b53d6ba10295d38e0a0490e86'
65+
66+
Add a catalog item to the cart using its sys_id
67+
68+
.INPUTS
69+
CatalogItem
70+
71+
.OUTPUTS
72+
PSCustomObject if PassThru provided
73+
#>
74+
function New-ServiceNowCartItem {
75+
[CmdletBinding(SupportsShouldProcess)]
76+
[Alias('Add-ServiceNowCartItem')]
77+
78+
param
79+
(
80+
[Parameter(Mandatory, ValueFromPipeline)]
81+
[string] $CatalogItem,
82+
83+
[Parameter()]
84+
[ValidateRange(1, [int32]::MaxValue)]
85+
[int32] $Quantity = 1,
86+
87+
[Parameter()]
88+
[hashtable]$ItemValues,
89+
90+
[Parameter()]
91+
[switch]$Checkout,
92+
93+
[Parameter()]
94+
[switch]$PassThru,
95+
96+
[Parameter()]
97+
[hashtable]$ServiceNowSession = $script:ServiceNowSession
98+
)
99+
100+
process {
101+
if ($CatalogItem | Test-ServiceNowSysId ) {
102+
#Verify the sys_id of the Catalog Item
103+
$catalogItemID = Get-ServiceNowRecord -Table sc_cat_item -AsValue -ID $CatalogItem -Property sys_id
104+
}
105+
else {
106+
#Lookup the sys_id of the Catalog Item
107+
$catalogItemID = Get-ServiceNowRecord -Table sc_cat_item -AsValue -Filter @('name', '-eq', $CatalogItem ) -Property sys_id
108+
}
109+
110+
if ([string]::IsNullOrEmpty($catalogItemID)) {
111+
throw "Unable to find catalog item '$CatalogItem'"
112+
}
113+
else {
114+
Write-Verbose "Found $catalogItemID via lookup from '$CatalogItem'"
115+
}
116+
117+
$addItemToCart = @{
118+
Method = 'Post'
119+
UriLeaf = "/servicecatalog/items/{0}/add_to_cart" -f $catalogItemID
120+
Values = @{
121+
'sysparm_quantity' = $Quantity
122+
}
123+
Namespace = 'sn_sc'
124+
ServiceNowSession = $ServiceNowSession
125+
}
126+
127+
if ( $ItemValues ) {
128+
$addItemToCart.Values.variables = $ItemValues
129+
}
130+
131+
if ( $PSCmdlet.ShouldProcess($catalogItemID, 'Create new catalog item request') ) {
132+
133+
try {
134+
$addItemCartResponse = Invoke-ServiceNowRestMethod @addItemToCart
135+
}
136+
catch {
137+
if ( ($_.ErrorDetails.Message | ConvertFrom-Json | Select-Object -ExpandProperty error | Select-Object -ExpandProperty message) -match 'Mandatory Variables are required' ) {
138+
$catItem = Invoke-ServiceNowRestMethod -UriLeaf "/servicecatalog/items/$catalogItemID" -Namespace 'sn_sc'
139+
$mandatoryVars = $catItem.variables | Where-Object { $_.mandatory -eq $true } | Select-Object -ExpandProperty name
140+
throw ('Failed to add item to cart. The following mandatory variables must be provided: {0}' -f ($mandatoryVars -join ', '))
141+
}
142+
else {
143+
throw $_
144+
}
145+
}
146+
147+
if ( -not $addItemCartResponse.cart_id ) {
148+
throw ('Failed to add item to cart. Response: {0}' -f ($addItemCartResponse | ConvertTo-Json))
149+
}
150+
else {
151+
Write-Verbose "Added item to cart with Cart ID: $($addItemCartResponse.cart_id)"
152+
$out = $addItemCartResponse
153+
}
154+
155+
Write-Verbose ("Current cart items:`n{0}" -f ($addItemCartResponse.items | Select-Object item_name, quantity, price | Out-String))
156+
Write-Verbose ('Cart Total: {0}' -f $addItemCartResponse.subtotal)
157+
}
158+
}
159+
160+
end {
161+
if ( $Checkout ) {
162+
$submitResponse = Submit-ServiceNowCart -ServiceNowSession $ServiceNowSession -PassThru
163+
Write-Verbose 'Order submitted successfully.'
164+
$out = $submitResponse
165+
}
166+
167+
if ( $PassThru ) {
168+
$out
169+
}
170+
}
171+
}

ServiceNow/Public/New-ServiceNowCatalogItem.ps1

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)