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+ }
0 commit comments