The CreatorCon Call for Content is officially open! Get started here.

please let me know how we can add comma separated value into addencodedquery into script

mani55
Tera Contributor

please let me know how we can add comma separated value into addencodedquery into script

 

var myString = "Grant Software/Application Elevation Access,Request to Enable USB\Bluetooth for Mass Storage Access,Web domain allowlist,Global Allowed_List (Whitelist) on Email Gateways,Files Folders Exclusion from Endpoint Detection Response/Sophisticated Antivirus Scan,FIDO2 User Group Access, Contextual  Access - GenAI Website";
var myArray = myString.split(","); // Splits the string into an array using the comma as the delimiter

// You can then iterate through the array to process each value
for (var i = 0; i < myArray.length; i++) {
    var singleValue = myArray[i];
    //gs.print('the policys'+singleValue);
    // Perform actions with singleValue, e.g., use it in a GlideRecord query
    var gr = new GlideRecord('sn_compliance_policy');
    gr.addQuery('name', singleValue);
    gr.query();
    while (gr.next()) {

        var grComps = new GlideRecord("sn_compliance_policy_exception");
        grComps.addEncodedQuery("valid_to<javascript&colon;gs.beginningOfToday()^'policy='+gr.name");
        grComps.query();
      
        while (grComps.next()) {
			var policy_exception;
            policy_exception.number= grComps.number + "",
               policy_exception.requestor= grComps.opened_by.getDisplayValue() + "",
               policy_exception.parent=grComps.parent.getDisplayValue() + "",
                policy_exception.ci= grComps.cmdb_ci.getDisplayValue() + "",
                policy_exception.userinput=grComps.user_input.getDisplayValue() + "",
                policy_exception.emailaddress=grComps.opened_by.email.getDisplayValue() + "",
               policy_exception.userfirstname=grComps.opened_by.first_name.getDisplayValue() + "",
                policy_exception.validto=grComps.valid_to + ""
        };
}
}
}
7 REPLIES 7

kaushal_snow
Mega Sage

Hi @mani55 ,

 

Loop through each element, then dynamically build an encoded query string using ^OR between your name=value clauses so you end up with something like name=Value1^ORname=Value2^ORname=Value3, and then pass that full encoded query into addEncodedQuery() so ServiceNow returns records matching any of those values....

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

could you please modify my script and provide me because i didn't understand properly

Rafael Batistot
Kilo Patron

Hi @mani55 

 

In encoded queries, multiple values must be joined with ^OR, not commas.

Example:

 

name=Value1^ORname=Value2^ORname=Value3

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

nityabans27
Kilo Sage

Hi @mani55 ,
Adjusted script :

var myString = "Grant Software/Application Elevation Access,Request to Enable USB\\Bluetooth for Mass Storage Access,Web domain allowlist,Global Allowed_List (Whitelist) on Email Gateways,Files Folders Exclusion from Endpoint Detection Response/Sophisticated Antivirus Scan,FIDO2 User Group Access, Contextual Access - GenAI Website";

// Split the string into an array
var myArray = myString.split(",");

// Escape single quotes and join values with comma for encoded query
var encodedValues = myArray.map(function(item) {
return item.trim().replace(/'/g, "\\'");
}).join(",");

var gr = new GlideRecord('sn_compliance_policy');
// Use IN operator to query multiple names at once
gr.addEncodedQuery("nameIN" + encodedValues);
gr.query();
while (gr.next()) {
var grComps = new GlideRecord("sn_compliance_policy_exception");

// Use policy name dynamically in encoded query
grComps.addEncodedQuery("valid_to<javascript&colon;gs.beginningOfToday()^policy=" + gr.name);
grComps.query();

while (grComps.next()) {
var policy_exception = {};
policy_exception.number = grComps.number + "";
policy_exception.requestor = grComps.opened_by.getDisplayValue() + "";
policy_exception.parent = grComps.parent.getDisplayValue() + "";
policy_exception.ci = grComps.cmdb_ci.getDisplayValue() + "";
policy_exception.userinput = grComps.user_input.getDisplayValue() + "";
policy_exception.emailaddress = grComps.opened_by.email + "";
policy_exception.userfirstname = grComps.opened_by.first_name + "";
policy_exception.validto = grComps.valid_to + "";

// Do something with policy_exception
// e.g., gs.print(JSON.stringify(policy_exception));
}
}