is there a removeQuery, changeQuery, or other way to manipulate a query in a business rule?

akaupisch
Kilo Guru

So the reason I'm doing this is to get around a bug in servicenow. The issue involves Resource Workbench and it overwriting my date with their hard coded default date.

My Business Rule:

When: Before

Query: true (the rest false)

No filter conditions

Advanced: true

under the Advanced Tab, Script:

(function executeRule(current, previous /*null when async*/) {

  try {

            if (current.getTableName() == 'roster_schedule_span')

                      return;

            var map = gs.action.getGlideURI().getMap();

            var referringProcessor = map.get('sysparm_processor');

            if (JSUtil.notNil(referringProcessor)) {

                      if (referringProcessor.toString().indexOf("com.snc.resource_management") > -1)

                                current.addQuery("show_as", "!=", "on_call"); // another bug fix that we have....sigh

                      else if (referringProcessor.toString().indexOf('ResourceWorkbenchService') > -1) {

                                /* This is a total hack to get around 2 bugs with ResourceWorkbench

                                * Bug 1: They hard code the default view to be 12 months -1 day from today

                                * Bug 2: If you set the EndDate prior to selecting the Group, it

                                *               automatically overrides your date with their default 12

                                *               months - 1 day.

                                * As such, we check to see if the end date is exactly 1 year - 1 day

                                *     from today. If it is, then we default to 3 months out

                                */

                                var endDate = new GlideDateTime(map.get('sysparm_end_date').toString());

                                var defaultDate = new GlideDateTime();

                                defaultDate.addYears(1);

                                defaultDate.addDays(-1);

         

                                if (defaultDate.equals(endDate))

                                          ;// !!!!!!! this is where I'd like to remove the end_date part of the query and instead

                                            // default to a different date something like current.changeQuery('end_date', myDefaultDate);

                        }

              }

  }

  catch(ex)

  {

            gs.error('Schedules Exception:\r\n' + ex );

  }

})(current, previous);

Check below the 'if (defaultDate.equals...)' statement, that's where I'd like to override/change this query.

3 REPLIES 3

The SN Nerd
Giga Sage
Giga Sage

The best you can probably do is use .addOrCondition() with your date query.


There is no API to change or remove an existing query without re-initializing current.



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

the or condition wont work as it'll include the dates we don't want. I like the idea of re-init, but is there a way to grab the original conditions before you re-init?


You might try



var oldQuery = current.getEncodedQuery();   // returns an Encoded Query String with all queries added to current so far


var newQuery = oldQuery.replace(defaultDate, myDate);   // use RegEx to match defaultDate


current.initialize();


current.addQuery(newQuery);



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate