Skip to content

Commit 71ae6ca

Browse files
authored
Resolve underlying reference value with custom variables (#221)
1 parent 453bac0 commit 71ae6ca

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

ServiceNow/Public/Get-ServiceNowRecord.ps1

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ function Get-ServiceNowRecord {
151151
[ValidateScript( {
152152
if ($_ -match '^[a-zA-Z0-9]{32}$' -or $_ -match '^([a-zA-Z]+)[0-9]+$') {
153153
$true
154-
} else {
154+
}
155+
else {
155156
throw 'Id must be either a 32 character alphanumeric, ServiceNow sysid, or prefix/id, ServiceNow number.'
156157
}
157158
})]
@@ -162,7 +163,8 @@ function Get-ServiceNowRecord {
162163
[ValidateScript( {
163164
if ($_ -match '^[a-zA-Z0-9]{32}$' -or $_ -match '^([a-zA-Z]+)[0-9]+$') {
164165
$true
165-
} else {
166+
}
167+
else {
166168
throw 'ParentId must be either a 32 character alphanumeric, ServiceNow sysid, or prefix/id, ServiceNow number.'
167169
}
168170
})]
@@ -241,7 +243,8 @@ function Get-ServiceNowRecord {
241243
}
242244

243245
$idFilter = @('sys_id', '-eq', $ID)
244-
} else {
246+
}
247+
else {
245248
if ( -not $thisTable ) {
246249
# get table name from prefix if only Id was provided
247250
$idPrefix = ($ID | Select-String -Pattern '^([a-zA-Z]+)([0-9]+$)').Matches.Groups[1].Value.ToLower()
@@ -256,7 +259,8 @@ function Get-ServiceNowRecord {
256259

257260
if ( $invokeParams.Filter ) {
258261
$invokeParams.Filter = $invokeParams.Filter, 'and', $idFilter
259-
} else {
262+
}
263+
else {
260264
$invokeParams.Filter = $idFilter
261265
}
262266

@@ -268,13 +272,15 @@ function Get-ServiceNowRecord {
268272
if ( $ParentID ) {
269273
if ( $ParentID -match '^[a-zA-Z0-9]{32}$' ) {
270274
$parentIdFilter = @('parent.sys_id', '-eq', $ParentID)
271-
} else {
275+
}
276+
else {
272277
$parentIdFilter = @('parent.number', '-eq', $ParentID)
273278
}
274279

275280
if ( $invokeParams.Filter ) {
276281
$invokeParams.Filter = $invokeParams.Filter, 'and', $parentIdFilter
277-
} else {
282+
}
283+
else {
278284
$invokeParams.Filter = $parentIdFilter
279285
}
280286
}
@@ -288,7 +294,8 @@ function Get-ServiceNowRecord {
288294

289295
if ( $invokeParams.Filter ) {
290296
$invokeParams.Filter = $invokeParams.Filter, 'and', @($thisTable.DescriptionField, '-like', $Description)
291-
} else {
297+
}
298+
else {
292299
$invokeParams.Filter = @($thisTable.DescriptionField, '-like', $Description)
293300
}
294301
}
@@ -317,10 +324,14 @@ function Get-ServiceNowRecord {
317324
# for each record, get the variable names and then get the variable values
318325
foreach ($record in $result) {
319326

327+
$recordSysId = if ($DisplayValue -eq 'all') { $record.sys_id.value } else { $record.sys_id }
328+
329+
# YES_NO = 1; MULTI_LINE_TEXT = 2; MULTIPLE_CHOICE = 3; NUMERIC_SCALE = 4; SELECT_BOX = 5; SINGLE_LINE_TEXT = 6; CHECKBOX = 7; REFERENCE = 8; DATE = 9; DATE_TIME = 10; LABEL = 11; BREAK = 12; MACRO = 14; UI_PAGE = 15; WIDE_SINGLE_LINE_TEXT = 16; MACRO_WITH_LABEL = 17; LOOKUP_SELECT_BOX = 18; CONTAINER_START = 19; CONTAINER_END = 20; LIST_COLLECTOR = 21; LOOKUP_MULTIPLE_CHOICE = 22; HTML = 23; SPLIT = 24; MASKED = 25;
330+
320331
$customVarParams = @{
321332
Table = 'sc_item_option_mtom'
322-
Filter = @('request_item', '-eq', $record.sys_id), 'and', @('sc_item_option.item_option_new.type', '-in', '1,2,3,4,5,6,7,8,9,10,16,18,21,22,26')
323-
Property = 'sc_item_option.item_option_new.name', 'sc_item_option.value', 'sc_item_option.item_option_new.type', 'sc_item_option.item_option_new.question_text'
333+
Filter = @('request_item', '-eq', $recordSysId), 'and', @('sc_item_option.item_option_new.type', '-in', '1,2,3,4,5,6,7,8,9,10,16,18,21,22,26')
334+
Property = 'sc_item_option.item_option_new.name', 'sc_item_option.value', 'sc_item_option.item_option_new.type', 'sc_item_option.item_option_new.question_text', 'sc_item_option.item_option_new.reference'
324335
IncludeTotalCount = $true
325336
ServiceNowSession = $ServiceNowSession
326337
}
@@ -333,16 +344,23 @@ function Get-ServiceNowRecord {
333344
$record | Add-Member @{
334345
'CustomVariable' = [pscustomobject]@{}
335346
}
347+
336348
foreach ($var in $customVarsOut) {
337-
$record.CustomVariable | Add-Member @{
338-
$var.'sc_item_option.item_option_new.name' = [pscustomobject] @{
339-
Value = $var.'sc_item_option.value'
340-
DisplayName = $var.'sc_item_option.item_option_new.question_text'
341-
Type = $var.'sc_item_option.item_option_new.type'
342-
}
349+
$newVar = [pscustomobject] @{
350+
Value = $var.'sc_item_option.value'
351+
DisplayName = $var.'sc_item_option.item_option_new.question_text'
352+
Type = $var.'sc_item_option.item_option_new.type'
353+
}
354+
355+
# show the underlying value if the option is a reference type
356+
if ($newVar.Type -eq 'Reference' ) {
357+
$newVar.Value = (Get-ServiceNowRecord -Table $var.'sc_item_option.item_option_new.reference' -ID $var.'sc_item_option.value' -Property name -AsValue -ServiceNowSession $ServiceNowSession)
343358
}
359+
360+
$record.CustomVariable | Add-Member @{ $var.'sc_item_option.item_option_new.name' = $newVar }
344361
}
345-
} else {
362+
}
363+
else {
346364

347365
Write-Warning 'The format for custom variables will soon change. Start using -New with -IncludeCustomVariable to preview.'
348366

@@ -370,22 +388,26 @@ function Get-ServiceNowRecord {
370388

371389
if ( $addedSysIdProp ) {
372390
$record | Select-Object -Property * -ExcludeProperty sys_id
373-
} else {
391+
}
392+
else {
374393
$record
375394
}
376395
}
377396

378-
} else {
397+
}
398+
else {
379399

380400
# format the results
381401
if ( $Property ) {
382402
if ( $Property.Count -eq 1 -and $AsValue ) {
383403
$propName = $result | Get-Member | Where-Object { $_.MemberType -eq 'NoteProperty' } | Select-Object -exp Name
384404
$result | Select-Object -ExpandProperty $propName
385-
} else {
405+
}
406+
else {
386407
$result
387408
}
388-
} else {
409+
}
410+
else {
389411
if ($thisTable.Type) {
390412
$result | ForEach-Object { $_.PSObject.TypeNames.Insert(0, $thisTable.Type) }
391413
}

0 commit comments

Comments
 (0)