Skip to content

Commit 84f3308

Browse files
authored
support filter string from ui (#268)
1 parent 4ca49f7 commit 84f3308

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

ServiceNow/Private/Invoke-ServiceNowRestMethod.ps1

+10-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ function Invoke-ServiceNowRestMethod {
4545
[parameter()]
4646
[object[]] $Filter,
4747

48+
[parameter()]
49+
[string] $FilterString,
50+
4851
[parameter()]
4952
[object[]] $Sort = @('opened_at', 'desc'),
5053

@@ -98,10 +101,15 @@ function Invoke-ServiceNowRestMethod {
98101
if ( $Method -eq 'Get') {
99102
$Body = @{
100103
'sysparm_display_value' = $DisplayValue
101-
'sysparm_query' = (New-ServiceNowQuery -Filter $Filter -Sort $Sort)
102104
'sysparm_limit' = 10
103105
}
104106

107+
if ( $FilterString ) {
108+
$Body.sysparm_query = $FilterString
109+
} else {
110+
$Body.sysparm_query = (New-ServiceNowQuery -Filter $Filter -Sort $Sort)
111+
}
112+
105113
# Handle paging parameters
106114
# The value of -First defaults to [uint64]::MaxValue if not specified.
107115
# If no paging information was provided, default to the legacy behavior, which was to return 10 records.
@@ -275,4 +283,4 @@ function Invoke-ServiceNowRestMethod {
275283
}
276284

277285
$records
278-
}
286+
}

ServiceNow/Public/Get-ServiceNowRecord.ps1

+19-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
For a complete list of comparison operators, see $script:ServiceNowOperator and use Name in your filter.
3434
See the examples.
3535
36+
.PARAMETER FilterString
37+
A string representation of the filter. This is useful when the filter is complex and hard to specify as an array.
38+
Retrieve the filter string from the ServiceNow UI via right click on the filter and selecting 'Copy query'.
39+
3640
.PARAMETER Sort
3741
Array or multidimensional array of fields to sort on.
3842
Each array should be of the format @(field, asc/desc).
@@ -143,6 +147,11 @@
143147
144148
Get a specific record by number using the function alias
145149
150+
.EXAMPLE
151+
Get-ServiceNowRecord -Table 'incident' -FilterString 'active=true^state=1'
152+
153+
Provide a filter string from the UI to get records where active is true and state is 1
154+
146155
.INPUTS
147156
ID
148157
@@ -165,6 +174,7 @@ function Get-ServiceNowRecord {
165174
[Parameter(ParameterSetName = 'Table', Mandatory)]
166175
[Parameter(ParameterSetName = 'TableId', Mandatory)]
167176
[Parameter(ParameterSetName = 'TableParentId')]
177+
[Parameter(ParameterSetName = 'FilterString', Mandatory)]
168178
[Alias('sys_class_name')]
169179
[string] $Table,
170180

@@ -204,6 +214,10 @@ function Get-ServiceNowRecord {
204214
[Parameter(ParameterSetName = 'TableParentId')]
205215
[object[]] $Filter = @(),
206216

217+
[Parameter(ParameterSetName = 'FilterString', Mandatory)]
218+
[Alias('fs')]
219+
[string] $FilterString,
220+
207221
[Parameter(ParameterSetName = 'Table')]
208222
[Parameter(ParameterSetName = 'TableParentId')]
209223
[ValidateNotNullOrEmpty()]
@@ -234,7 +248,6 @@ function Get-ServiceNowRecord {
234248
process {
235249

236250
$thisParams = @{
237-
Filter = $Filter
238251
Property = $Property
239252
Sort = $Sort
240253
DisplayValue = $DisplayValue
@@ -246,13 +259,18 @@ function Get-ServiceNowRecord {
246259
}
247260

248261
if ( $PSBoundParameters.ContainsKey('Filter') ) {
262+
$thisParams.Filter = $Filter
249263
# # we always want the filter to be arrays separated by joins
250264
if ( $Filter[0].GetType().Name -ne 'Object[]' ) {
251265
#
252266
$thisParams.Filter = , $Filter
253267
}
254268
}
255269

270+
if ( $FilterString ) {
271+
$thisParams.FilterString = $FilterString
272+
}
273+
256274
$addedSysIdProp = $false
257275
# we need the sys_id value in order to get custom var data
258276
# add it in if specific properties were requested and not part of the list

0 commit comments

Comments
 (0)