Announcing the Global SNUG Board of Directors. Learn more here

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
restevao
Mega Expert

Have you ever looked into a ServiceNow instance and thought...

 

"What the hell have they done! and where have they done it (who knows why)".

 

Well now you will be able to easily find and identify the 'business process' the developer decided to implement at the point of writing the code.

 

This is the most useful script for any Admin starting with a new instance that was developed by someone else. This script should be executed in DEV but is safe to be used in PROD.

 

This script takes less then 1min to execute with how it is currently set up. (searching HTML type fields will take a long time.)

 

This Script:

 - Searches up all fields in the dictionary that relates to a field Type.

 - Enables lookup of specific tables and fields based on custom entry.

 - spits out a URL of the record.

 

Example scenario: (currently set)

Commenting out all the useless log statements. This will be beneficial to a systems performance as well as making it easier to debug your own issues.

 

FIX SCRIPT: (can be adapted to any server side script) 

var SEARCHSTRING = "gs.log";

function searchRecords(tableName,fieldName,SRCH)

{

    var result = "";

    var x = new GlideRecord(tableName);

    x.addQuery(fieldName, 'CONTAINS', SRCH);

    x.query();

    while (x.next())

    {

        result = result +"\n" + tableName + "." + fieldName + " " + x.sys_id;

        //result = result +"\n" + "https://" + gs.getProperty('instance_name') + ".service-now.com/nav_to.do?uri=" +tableName + ".do?sys_id=" + x.sys_id;

        result = result +"\t" + "https://" + gs.getProperty('instance_name') + ".service-now.com/" +tableName + ".do?sys_id=" + x.sys_id;

    }

    return result;

}

function main(SEARCH)

{

    var message = "";

    //list of recommended field types to search

    //'html' field type requires a lot of time to query.

    var fieldTypes = ["conditions" , "script", "script_plain" ,"properties","xml"];

    //["conditions", "css", "html", "json", "properties", "script", "script_plain", "url", "variables", "wide_text", "workflow", "workflow_conditions", "xml","html_template"]

   

    //enables the searching of specific tables and fields that may vary from instance to instance or might be missed. i.e text fields storing scripts to be eval();

    //syntax: "{table_name}.{field_name}"

    var customTableElementSearch = ["sysrule_assignment.condition", "ecc_agent_script_file.script","sysrule_assignment.script", "matching_rule.script"];

   

    //creates encoded query that takes the form of 'internal_type=css,script,...'

    var encodedQuery = "internal_type.nameIN";

    for (var i = 0; i < fieldTypes.length; i++)

    {

        encodedQuery = encodedQuery + fieldTypes[i] + ",";

    }

    encodedQuery = encodedQuery.substring(0, encodedQuery.length - 1);

    var y = new GlideRecord("sys_dictionary");

    y.addEncodedQuery(encodedQuery);

    y.addQuery("name", "DOES NOT CONTAIN", "var__m_");

    y.orderBy("name");

    //y.setLimit(100);

    y.query();

    //gs.log(y.getRowCount());

    while (y.next())

    {

        //if (y.name.indexOf("var__m_") == -1)//filtering

        //{

            message = message + searchRecords(y.name,y.element,SEARCH);

        //}

    }

   

    message = message + "\n-----CUSTOM RECORD SEARCH-----";

   

    for (var j = 0; j < customTableElementSearch.length; j++)

    {

        var tableField  = customTableElementSearch[j].split(".");

        message = message + searchRecords(tableField[0],tableField[1],SEARCH); 

    }

    return message;

}

gs.log(main(SEARCHSTRING));

/*

//List of Data Types in SNOW

//sys_glide_object

//Can be used to brute force all fields in all tables.

var x = new GlideRecord("sys_glide_object");

x.addQuery("","");

x.query();

gs.log(x.getRowCount());

​​

["conditions",

"condition_string",

"css",

"data_array",

"data_object",

"data_structure",

"email_script",

"formula",

"glide_var",

"GUID",

"html",

"html_script",

"html_template",

"json",

"mask_code",

"multi_small",

"multi_two_lines",

"properties",

"script",

"script_plain",

"script_server",

"sysevent_name",

"translated",

"translated_field",

"translated_html",

"translated_text",

"tree_code",

"tree_path",

"url",

"user_roles",

"variables",

"variable_conditions",

"variable_template_value",

"wide_text",

"wiki_text",

"workflow",

"workflow_conditions",

"xml"]

*/

//https://dev57934.service-now.com/sys_dictionary_list.do?sysparm_query=internal_type.nameINdata_object%2Cdata_structure%2Cemail_script%2Cformula%2Cglide_var%2CGUID%2Chtml_script%2Chtml_template%2Cmask_code%2Cmulti_small%2Cmulti_two_lines%2Csysevent_name%2Cscript_server%2Ctranslated%2Ctranslated_field%2Ctranslated_html%2Ctranslated_text%2Ctree_code%2Ctree_path%2Cuser_roles%2Cvariable_conditions%2Cvariable_template_value%2Cwiki_text

 

Version history
Last update:
‎11-18-2018 09:11 PM
Updated by: