Skip to content

Commit

Permalink
support filter string from ui
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbarron committed Jul 11, 2024
1 parent 730ae64 commit f5d2d5d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
12 changes: 10 additions & 2 deletions ServiceNow/Private/Invoke-ServiceNowRestMethod.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ function Invoke-ServiceNowRestMethod {
[parameter()]
[object[]] $Filter,

[parameter()]
[string] $FilterString,

[parameter()]
[object[]] $Sort = @('opened_at', 'desc'),

Expand Down Expand Up @@ -98,10 +101,15 @@ function Invoke-ServiceNowRestMethod {
if ( $Method -eq 'Get') {
$Body = @{
'sysparm_display_value' = $DisplayValue
'sysparm_query' = (New-ServiceNowQuery -Filter $Filter -Sort $Sort)
'sysparm_limit' = 10
}

if ( $FilterString ) {
$Body.sysparm_query = $FilterString
} else {
$Body.sysparm_query = (New-ServiceNowQuery -Filter $Filter -Sort $Sort)
}

# Handle paging parameters
# The value of -First defaults to [uint64]::MaxValue if not specified.
# If no paging information was provided, default to the legacy behavior, which was to return 10 records.
Expand Down Expand Up @@ -275,4 +283,4 @@ function Invoke-ServiceNowRestMethod {
}

$records
}
}
20 changes: 19 additions & 1 deletion ServiceNow/Public/Get-ServiceNowRecord.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
For a complete list of comparison operators, see $script:ServiceNowOperator and use Name in your filter.
See the examples.
.PARAMETER FilterString
A string representation of the filter. This is useful when the filter is complex and hard to specify as an array.
Retrieve the filter string from the ServiceNow UI via right click on the filter and selecting 'Copy query'.
.PARAMETER Sort
Array or multidimensional array of fields to sort on.
Each array should be of the format @(field, asc/desc).
Expand Down Expand Up @@ -143,6 +147,11 @@
Get a specific record by number using the function alias
.EXAMPLE
Get-ServiceNowRecord -Table 'incident' -FilterString 'active=true^state=1'
Provide a filter string from the UI to get records where active is true and state is 1
.INPUTS
ID
Expand All @@ -165,6 +174,7 @@ function Get-ServiceNowRecord {
[Parameter(ParameterSetName = 'Table', Mandatory)]
[Parameter(ParameterSetName = 'TableId', Mandatory)]
[Parameter(ParameterSetName = 'TableParentId')]
[Parameter(ParameterSetName = 'FilterString', Mandatory)]
[Alias('sys_class_name')]
[string] $Table,

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

[Parameter(ParameterSetName = 'FilterString', Mandatory)]
[Alias('fs')]
[string] $FilterString,

[Parameter(ParameterSetName = 'Table')]
[Parameter(ParameterSetName = 'TableParentId')]
[ValidateNotNullOrEmpty()]
Expand Down Expand Up @@ -234,7 +248,6 @@ function Get-ServiceNowRecord {
process {

$thisParams = @{
Filter = $Filter
Property = $Property
Sort = $Sort
DisplayValue = $DisplayValue
Expand All @@ -246,13 +259,18 @@ function Get-ServiceNowRecord {
}

if ( $PSBoundParameters.ContainsKey('Filter') ) {
$thisParams.Filter = $Filter
# # we always want the filter to be arrays separated by joins
if ( $Filter[0].GetType().Name -ne 'Object[]' ) {
#
$thisParams.Filter = , $Filter
}
}

if ( $FilterString ) {
$thisParams.FilterString = $FilterString
}

$addedSysIdProp = $false
# we need the sys_id value in order to get custom var data
# add it in if specific properties were requested and not part of the list
Expand Down

0 comments on commit f5d2d5d

Please sign in to comment.