please let me know how we can add comma separated value into addencodedquery into script
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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: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
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
above script i am using in rest api explorer but still i didn't get proper output
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
try {
// Check Policy
var resp = {};
//multiple polices checking
var policyName = request.queryParams.policy;
var myArray = policyName.split(",");
// Escape single quotes and join values with comma for encoded query
var encodedValues = myArray.map(function(item) {
return item.trim().replace(/'/g, "\\'");
}).join(",");
// Perform actions with singleValue, e.g., use it in a GlideRecord query
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");
grComps.addEncodedQuery("valid_to<javascript:gs.beginningOfToday()^'policy.name='+gr.name");
grComps.query();
resp.services = [];
while (grComps.next()) {
var policy_exception = {};
policy_exception.number = grComps.number;
policy_exception.requestorDisplayname = grComps.opened_by.getDisplayValue() + "",
policy_exception.requestor = grComps.opened_by + "",
policy_exception.policy = grComps.policy.getDisplayValue() + "",
policy_exception.shortdescription = grComps.short_description + "",
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 + "";
policy_exception.duration = grComps.duration + "";
resp.services.push(policy_exception);
}
}
response.setStatus(200);
return {
ci: resp.services
};
} catch (error) {
response.setError(new sn_ws_err.BadRequestError(script.message));
}
})(request, response);
after status showing 200 but in out put is empty
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
try this optimized one
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, trim whitespace from each entry
var myArray = myString.split(",").map(function(val) { return val.trim(); });
// Recombine as comma-separated for IN operator (addEncodedQuery)
var encodedList = myArray.join(',');
// Query policies using the IN operator
var gr = new GlideRecord('sn_compliance_policy');
gr.addEncodedQuery('nameIN' + encodedList);
gr.query();
while (gr.next()) {
gs.print('Policy name: ' + gr.name);
var grComps = new GlideRecord("sn_compliance_policy_exception");
grComps.addEncodedQuery("valid_to<javascript: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 + "";
gs.print(JSON.stringify(policy_exception));
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards,
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi Ankur,
i try with above script but still won't working please find below screesnots.
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
try {
// Check Policy
var resp = {};
//multiple polices checking
var policyName = request.pathParams.policy;
var myArray = policyName.split(",").map(function(val) {
return val.trim();
});
// Recombine as comma-separated for IN operator (addEncodedQuery)
var encodedList = myArray.join(',');
// Perform actions with singleValue, e.g., use it in a GlideRecord query
var gr = new GlideRecord('sn_compliance_policy');
// Use IN operator to query multiple names at once
gr.addEncodedQuery('nameIN' + encodedList);
gr.query();
while (gr.next()) {
var grComps = new GlideRecord("sn_compliance_policy_exception");
grComps.addEncodedQuery("valid_to<javascript:gs.beginningOfToday()^policy=" + gr.name);
grComps.query();
resp.services = [];
while (grComps.next()) {
var policy_exception = {};
policy_exception.number = grComps.number;
policy_exception.requestorDisplayname = grComps.opened_by.getDisplayValue() + "",
policy_exception.requestor = grComps.opened_by + "",
policy_exception.policy = grComps.policy.getDisplayValue() + "",
policy_exception.shortdescription = grComps.short_description + "",
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 + "";
policy_exception.duration = grComps.duration + "";
resp.services.push(policy_exception);
}
}
response.setStatus(200);
return {
ci: resp.services
};
} catch (error) {
response.setError(new sn_ws_err.BadRequestError(script.message));
}
})(request, response);
input :
output: