Skip to main content

ServiceNow Global Script

 

//Global search script var term = "search,terms"; //use comma to separate terms var useAndQuery = true; //search for term 1 and term 2. If false, it will look for term 1 or term 2. var debug = false; //set this true if you want to troubleshoot the script. var delimiter = ","; //if you want to split terms on something other than a comma var globalScriptSearch = { search: null, delimiter: ",", out: ["\n"], debug: false, useAndQuery: true, /* Main method that calls all the sub-methods.*/ search: function(term, useAndQuery, debug, delimiter) { if (typeof debug != "undefined") this.debug = debug; if (typeof delimiter != "undefined") this.delimiter = delimiter; if (typeof useAndQuery != "undefined") this.useAndQuery = useAndQuery; if (typeof term != "string") { gs.print("GlobalScriptSearch - ERROR: must pass a valid search term to search"); return false; } this.search = term; this._searchLocations(); this._searchFieldTypes(); this._searchWorkflow(); gs.print(this.out.join("\n")); return true; }, _searchLocations: function() { var locations = { "question": "default_value", "sys_trigger": "job_context", "sys_dictionary": "calculation", "sys_dictionary_override": "calculation", "sys_ui_macro": "xml", "sys_ui_page": "html", "sys_impex_entry": "default_value", "content_block_programmatic": "programmatic_content", } for (var location in locations) { this._searchLocation(location, locations[location]); } }, _searchFieldTypes: function() { var fieldList = new GlideRecord("sys_dictionary"); var fieldTypes = ["script_plain", "script", "email_script", "condition_string", "conditions"]; var aq = null; for (var ic = 0; ic < fieldTypes.length; ic++) { if (aq == null) aq = fieldList.addQuery("internal_type", fieldTypes[ic]); else aq.addOrCondition("internal_type", fieldTypes[ic]); } this._searchFieldsNamedScript(aq); //exclude all field types that don't come from String var nonString = new GlideRecord("sys_glide_object"); nonString.addQuery("scalar_type", "!=", "string"); nonString.query(); while (nonString.next()) { fieldList.addQuery("internal_type", "!=", nonString.name + ""); } fieldList.addQuery("internal_type", "!=", "boolean"); fieldList.query(); while (fieldList.next()) { if (fieldList.name.indexOf("var__") >= 0) continue; this._searchLocation(fieldList.name, fieldList.element); } }, _searchFieldsNamedScript: function(addQuery) { addQuery.addOrCondition("element", "ENDSWITH", "script"); addQuery.addOrCondition("element", "STARTSWITH", "script"); }, _searchWorkflow: function() { var allDocs = []; var tableName = 'sys_variable_value'; var fieldName = 'value'; var rec = new GlideRecord(tableName); rec.addQuery('document', 'wf_activity'); var qc = rec.addQuery('variable.element', 'CONTAINS', 'script'); qc.addOrCondition('variable.internal_type', 'CONTAINS', 'script'); var terms = this.search.split(this.delimiter); for (var ib = 0; ib < terms.length; ib++) { if (this.useAndQuery) { rec.addQuery(fieldName, 'CONTAINS', terms[ib]); } else { if (ib == 0) var aq = rec.addQuery(fieldName, "CONTAINS", terms[ib]); else aq.addOrCondition(fieldName, "CONTAINS", terms[ib]); } } this._addMatches(tableName, fieldName, rec); }, _searchLocation: function(table, field) { var fieldName = field + ""; var tableName = table + ""; if (this.debug) gs.print("_searchLocation: " + tableName + " " + fieldName); var target = new GlideRecord(tableName); if (!target.isValid()) { gs.print("GlobalScriptSearch - ERRROR: " + tableName + " was an invalid table name (field: " + fieldName + ")."); return; } var terms = this.search.split(this.delimiter); if (this.debug) gs.print("_searchLocation: terms.length " + terms.length); if (this.useAndQuery) { for (var ia = 0; ia < terms.length; ia++) { target.addQuery(fieldName, "CONTAINS", terms[ia]); } } else { var aq; for (var ia = 0; ia < terms.length; ia++) { if (ia == 0) var aq = target.addQuery(fieldName, "CONTAINS", terms[ia]); else aq.addOrCondition(fieldName, "CONTAINS", terms[ia]); } } this._addMatches(tableName, fieldName, target); }, _addMatches: function(tableName, fieldName, match) { try { match.query(); if (match.getRowCount() < 1) return; var matchList = []; this.out.push("\n\n* Searching - " + tableName + "." + fieldName + " ***"); while(match.next()) { matchList.push(match.sys_id+""); this.out.push(match.getClassDisplayValue() + " - " + match.getDisplayValue() + ": /" + tableName + ".do?sys_id=" + match.sys_id); } this.out.push("\n[All matches] /" + tableName + "_list.do?sysparm_query=sys_idIN" + matchList.join(",")); } catch(e) { gs.print("GlobalScriptSearch - ERRROR: failure while trying to insert match " + e); } }, type: 'GlobalScriptSearch' } globalScriptSearch.search(term, useAndQuery, debug, delimiter);

 

(ServiceNow)

Comments

Popular posts from this blog

ServiceNow timeout settings for virtual agent chat

  After connecting to live agent User is connected to live agent and not responding to live agent chat or distracted. The below properties controls the timeout settings, the OOB settings for reminder is [180 sec] and for cancelling the chat is [360 sec]. The job is default configured to 2 min so I believe no tweaking is required here.  Property -   com.glide.cs.idle_chat_reminder_timeout                  com.glide.cs.idle_chat_cancel_timeout Scheduled job   - Idle Chat Timer Task   https://community.servicenow.com/community?id=community_article&sys_id=1453b03bdbaad0109e691ea668961929   (ServiceNow ) 

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){...