Skip to content

Commit c4079d9

Browse files
committed
fix code paths to auto determine table
1 parent ffba5b8 commit c4079d9

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

ServiceNow/Public/Get-ServiceNowRecord.ps1

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ function Get-ServiceNowRecord {
161161
)
162162

163163
$invokeParams = @{
164-
Table = $Table
165164
Filter = $Filter
166165
Property = $Property
167166
Sort = $Sort
@@ -173,40 +172,51 @@ function Get-ServiceNowRecord {
173172
ServiceNowSession = $ServiceNowSession
174173
}
175174

175+
if ( $Table ) {
176+
$thisTable = $script:ServiceNowTable | Where-Object { $_.Name.ToLower() -eq $Table.ToLower() -or $_.ClassName.ToLower() -eq $Table.ToLower() }
177+
if ( -not $thisTable ) {
178+
# we aren't aware of this table, create default config
179+
$thisTable = @{
180+
Name = $Table
181+
ClassName = $null
182+
Type = $null
183+
NumberPrefix = $null
184+
DescriptionField = $null
185+
}
186+
}
187+
}
188+
176189
if ( $Id ) {
177190
if ( $Id -match '[a-zA-Z0-9]{32}' ) {
178-
if ( $PSCmdlet.ParameterSetName -eq 'Id' ) {
191+
if ( -not $thisTable ) {
179192
throw 'Providing sys_id for -Id requires a value for -Table. Alternatively, provide an Id with a prefix, eg. INC1234567, and the table will be automatically determined.'
180193
}
181-
194+
182195
$idFilter = @('sys_id', '-eq', $Id)
183196
}
184197
else {
185-
if ( $PSCmdlet.ParameterSetName -eq 'Id' ) {
198+
if ( -not $thisTable ) {
186199
# get table name from prefix if only Id was provided
187200
$thisTable = $script:ServiceNowTable | Where-Object { $_.NumberPrefix -and $Id.ToLower().StartsWith($_.NumberPrefix) }
188-
if ( $thisTable ) {
189-
$invokeParams.Table = $thisTable.Name
190-
}
191-
else {
201+
if ( -not $thisTable ) {
192202
throw ('The prefix for Id ''{0}'' was not found and the appropriate table cannot be determined. Known prefixes are {1}. Please provide a value for -Table.' -f $Id, ($ServiceNowTable.NumberPrefix.Where( { $_ }) -join ', '))
193203
}
194204
}
195205
$idFilter = @('number', '-eq', $Id)
196206
}
197-
207+
198208
if ( $invokeParams.Filter ) {
199209
$invokeParams.Filter = $invokeParams.Filter, 'and', $idFilter
200210
}
201211
else {
202212
$invokeParams.Filter = $idFilter
203213
}
204-
}
205-
else {
206-
# table name was provided, get the config entry if there is one
207-
$thisTable = $script:ServiceNowTable | Where-Object { $_.Name.ToLower() -eq $Table.ToLower() -or $_.ClassName.ToLower() -eq $Table.ToLower() }
214+
208215
}
209216

217+
# we have the table, update the params
218+
$invokeParams.Table = $thisTable.Name
219+
210220
if ( $ParentId ) {
211221
if ( $ParentId -match '[a-zA-Z0-9]{32}' ) {
212222
$parentIdFilter = @('parent.sys_id', '-eq', $ParentId)
@@ -225,19 +235,16 @@ function Get-ServiceNowRecord {
225235

226236
if ( $Description ) {
227237
# determine the field we should compare for 'description' and add the filter
228-
if ( $thisTable ) {
229-
$nameFilter = @($thisTable.DescriptionField, '-like', $Description)
230-
}
231-
else {
232-
Write-Warning ('We do not have a description field for table ''{0}''; short_description will be used' -f $Table)
233-
$nameFilter = @('short_description', '-like', $Description)
238+
if ( -not $thisTable.DescriptionField ) {
239+
Write-Warning ('We do not have table ''{0}'' in the config; short_description will be used as the description field' -f $Table)
240+
$thisTable.DescriptionField = 'short_description'
234241
}
235242

236243
if ( $invokeParams.Filter ) {
237-
$invokeParams.Filter = $invokeParams.Filter, 'and', $nameFilter
244+
$invokeParams.Filter = $invokeParams.Filter, 'and', @($thisTable.DescriptionField, '-like', $Description)
238245
}
239246
else {
240-
$invokeParams.Filter = $nameFilter
247+
$invokeParams.Filter = @($thisTable.DescriptionField, '-like', $Description)
241248
}
242249
}
243250

0 commit comments

Comments
 (0)