Skip to content

Commit e500b2d

Browse files
CATgwalkergdbarron
andauthored
Update Get-ServiceNowRecord.ps1 (#263)
* Update Get-ServiceNowRecord.ps1 Fixes error "Id must either be a SysId 32 character alphanumeric or Number with prefix and id." and retains ID from reference table for further lookup. * Update Get-ServiceNowRecord.ps1 Additional fixes for reference table lookup where value is not in the 32 character format expected by SNOW. * Update ServiceNow/Public/Get-ServiceNowRecord.ps1 Co-authored-by: Greg Brownstein <greg@jagtechnical.com> --------- Co-authored-by: Greg Brownstein <greg@jagtechnical.com>
1 parent f67829a commit e500b2d

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

ServiceNow/Public/Get-ServiceNowRecord.ps1

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ function Get-ServiceNowRecord {
183183
[ValidateScript( {
184184
if ($_ -match '^[a-zA-Z0-9]{32}$' -or $_ -match '^([a-zA-Z]+)[0-9]+$') {
185185
$true
186-
}
187-
else {
186+
} else {
188187
throw 'Id must either be a SysId 32 character alphanumeric or Number with prefix and id.'
189188
}
190189
})]
@@ -195,8 +194,7 @@ function Get-ServiceNowRecord {
195194
[ValidateScript( {
196195
if ($_ -match '^[a-zA-Z0-9]{32}$' -or $_ -match '^([a-zA-Z]+)[0-9]+$') {
197196
$true
198-
}
199-
else {
197+
} else {
200198
throw 'ParentId must either be a SysId 32 character alphanumeric or Number with prefix and id.'
201199
}
202200
})]
@@ -287,8 +285,7 @@ function Get-ServiceNowRecord {
287285

288286
if ( $thisID -match '^[a-zA-Z0-9]{32}$' ) {
289287
$thisParams.Filter += , @('sys_id', '-eq', $thisID)
290-
}
291-
else {
288+
} else {
292289
$thisParams.Filter += , @('number', '-eq', $thisID)
293290
}
294291
}
@@ -301,8 +298,7 @@ function Get-ServiceNowRecord {
301298

302299
if ( $ParentID -match '^[a-zA-Z0-9]{32}$' ) {
303300
$thisParams.Filter += , @('parent.sys_id', '-eq', $ParentID)
304-
}
305-
else {
301+
} else {
306302
$thisParams.Filter += , @('parent.number', '-eq', $ParentID)
307303
}
308304

@@ -381,43 +377,50 @@ function Get-ServiceNowRecord {
381377

382378
# show the underlying value if the option is a reference type
383379
if ( $newVar.Type -eq 'Reference' ) {
384-
$newVar | Add-Member @{'ReferenceTable' = $var.'sc_item_option.item_option_new.reference' }
385-
# issue 234. ID might not be sysid or number for reference...odd
386-
$refValue = Get-ServiceNowRecord -Table $var.'sc_item_option.item_option_new.reference' -ID $var.'sc_item_option.value' -Property name -AsValue -ServiceNowSession $ServiceNowSession -ErrorAction SilentlyContinue
387-
if ( $refValue ) {
388-
$newVar.Value = $refValue
380+
#do not do any further lookup when the value is blank or null
381+
#resolves #234 and 262
382+
if ($var.'sc_item_option.value' -eq "" -or $null -eq $var.'sc_item_option.value') {
383+
continue
389384
}
385+
$sysidPattern = "[0-9a-fA-F]{32}"
386+
$sysid = [Regex]::Matches($var.'sc_item_option.value', $sysidPattern).Value
387+
if ($sysid) {
388+
Write-Verbose "Custom variable lookup for $($newvar.name) from table '$($var.'sc_item_option.item_option_new.reference')' sysid:'$($var.'sc_item_option.value')'"
389+
$newVar | Add-Member @{'ReferenceTable' = $var.'sc_item_option.item_option_new.reference' }
390+
$newVar | Add-Member @{'ReferenceID' = $var.'sc_item_option.value' }
391+
# issue 234. ID might not be sysid or number for reference...odd
392+
$refValue = Get-ServiceNowRecord -Table $var.'sc_item_option.item_option_new.reference' -ID $var.'sc_item_option.value' -Property name -AsValue -ServiceNowSession $ServiceNowSession -ErrorAction SilentlyContinue
393+
if ( $refValue ) {
394+
$newVar.Value = $refValue
395+
}
396+
}
397+
390398
}
391399

392400
if ( $var.'sc_item_option.item_option_new.name' ) {
393401
$record.CustomVariable | Add-Member @{ $var.'sc_item_option.item_option_new.name' = $newVar }
394-
}
395-
else {
402+
} else {
396403
$record.CustomVariable | Add-Member @{ $var.'sc_item_option.item_option_new.question_text' = $newVar }
397404
}
398405
}
399406

400407
if ( $addedSysIdProp ) {
401408
$record | Select-Object -Property * -ExcludeProperty sys_id
402-
}
403-
else {
409+
} else {
404410
$record
405411
}
406412
}
407-
}
408-
else {
413+
} else {
409414

410415
# format the results
411416
if ( $Property ) {
412417
if ( $Property.Count -eq 1 -and $AsValue ) {
413418
$propName = $result | Get-Member | Where-Object { $_.MemberType -eq 'NoteProperty' } | Select-Object -ExpandProperty Name
414419
$result.$propName
415-
}
416-
else {
420+
} else {
417421
$result
418422
}
419-
}
420-
else {
423+
} else {
421424
if ($thisTable.Type) {
422425
$result | ForEach-Object { $_.PSObject.TypeNames.Insert(0, $thisTable.Type) }
423426
}

0 commit comments

Comments
 (0)