How I Used GlideQueryString() in ServiceNow (undocumented API)
As I mentioned in my recent post, I needed a way to validate the condition operators in the ServiceNow UI before trying to POST to the other system. This way, I could ensure a smooth UX for our analysts by preventing invalid operators prior to attempting the POST. Otherwise, the analyst would submit and would find out after the fact that their condition was not valid and would need to be modified. So what does the code for this look like? See below.
Please note: I did redact some code to preserve any company-specific tables or nomenclature and made this as generic as possible.
Components to make this all work: system property to store the invalid operators as a JSON object, display business rule to get the invalid operators, onSubmit() client script to perform validation prior to sending out outbound request, and our script include method by which our client script calls and passes the needed parameters via GlideAjax.
System Property:
/***
* System property - put into JSON format. This sys property contains a ',' delimited string of operators that our external system will NOT accept in their database
*/
{
"is not one of": "NOT IN",
"is one of": "IN",
"does not contain": "DOES NOT CONTAIN",
"is anything": "ANYTHING",
"is different": "NSAMEAS",
"is same": "SAMEAS",
"is not empty": "NOTEMPTY",
"is empty": "EMPTY",
"less than or is": "<=",
"less than": "<",
"greater than or is": ".=",
"greater than": ">",
"between": "BETWEEN",
"not on": "NOTONToday",
"on": "ONToday",
"<": "BEFORE",
"trend (on or after)": "DATEPART",
"relative (after)": "RELATIVEGT",
"relative (before)": "RELATIVELT",
"is more than": "MORETHAN",
"is less than": "LESSTHAN",
"greater than field": "GT_FIELD",
"less than field": "LT_FIELD",
"greater than or is field": "GT_OR_EQUALS_FIELD",
"less than or is field": "LT_OR_EQUALS_FIELD",
"is empty string": "EMPTYSTRING",
"is (dynamic)": "DYNAMIC"
}
Display Business Rule:
领英推荐
(function executeRule(current, previous /*null when async*/) {
/*
grab the sys_property that holds the update action fields allowed to be used with the client script called 'Check update rule values submit'
*/
var invalidConditionOperators = gs.getProperty('sn_si.rules.engine.invalid_condition_operators');
g_scratchpad.invalidConditionOperators = invalidConditionOperators;
})(current, previous);
Script include method (where we use the GlideQueryString API:
getConditionQueryFields: function() {
var resultsObject = {};
var fieldsArray = [];
var conditionQuery = this.getParameter('sysparm_conditionQuery');
var tableName = this.getParameter('sysparm_tableName');
//takes a table and a condition as its parameters
var fields = new GlideQueryString(tableName, conditionQuery);
fields.deserialize();
//getTerms() method outputs an array of 'terms'from our condition
var terms = fields.getTerms();
//iterate through the array and leverage some of the methods available
for (var i=0; i<terms.size();i++) {
var term = terms.get(i);
var termField = term.getTermField();
//getOperator() method is what we ultimately are needing/using for this use-case which will output the values seen in our system property
var termOperator = term.getOperator();
fieldsArray.push(termOperator);
}
//make sure we don't repeat any of our operators, because we fail and alert to the user on the first invalid operator found
var uniqueFieldsArray = new ArrayUtil().unique(fieldsArray);
//make sure none of the values are empty
if(uniqueFieldsArray.indexOf(null) != -1) {
uniqueFieldsArray.splice(uniqueFieldsArray.indexOf(null,1));
}
//build our object to send back via GlideAjax
resultsObject.conditionFields = uniqueFieldsArray;
return JSON.stringify(resultsObject);
}
OnSubmit() Client Script:
function onSubmit() {
//retrieve the invalidConditionOperators property value as a string and store in a variable
var invalidConditionOperators = g_scratchpad.invalidConditionOperators + '';
//retrieve the value of the u_condition field and store in in a variable
var condition = g_form.getValue('u_condition');
var conditionJSON = JSON.parse(invalidConditionOperators);
var conditionFieldsOnly;
var conditionFieldsOnlyParsed;
var getConditionFields = new GlideAjax('myScriptInclude');
getConditionFields.addParam('sysparm_namme', 'getConditionQueryFields');
getConditionFields.addParam('sysparm_conditionQuery', condition);
getConditionFields.addParam('sysparm_tableName', 'sn_si_incident');
getConditionFields.getXMLAnswer(conditionFields);
function conditionFields(answer) {
if (answer) {
conditionFieldsOnly = answer;
conditionFieldsOnlyParsed = JSON.parse(conditionFieldsOnly);
//if condition is not empty
if (condition != '') {
//loop through each character in the condition field
for (var prop in conditionFieldsOnlyParsed) {
//check if an invalid operator is contained in the invalidConditionOperators string
var conditionJSONprop = conditionFieldsOnlyParsed[prop];
for (var i=0; i<conditionJSONprop.length; i++){
for (var conditionValue in conditionJSON) {
if (conditionJSONprop[i] == conditionJSON[conditionValue]){
alert ('The operator "' + conditionValue + '" is not a valid operator for a rule condition. Please remove it or replace it with a valid value before submitting. Otherwise this will fail validation and will require an updated condition and resubmission for validation.');
return false;
}
}
}
}
}
}
else {
alert('An error has occurred. Please reach out to ServiceNow Support regarding this issue');
}
}
}
Thanks for reading! I hope you got something out of this.