For loop repeating multiple times in the record producer script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 03:57 AM
Hi All,
We have one record producer in which we have to generate incident or requested item based on the selection of one variable (abc - List collector type).
Suppose in the list collector if I choose A an incident should get generate and if I select B an RITM will generate. In my scenario if I selected single value (either A or B) it worked fine but when I selected both A & B in list collector it generates multiple RITM's (nearby 90+ RITM). Below is the record producer code.
var list = producer.type_of_problem.toString();
var array = list.split(',');
for (var i=0; i < array.length; i++) {
gs.log("abc" + array[i]);
gs.log("def" + array.length);
//if (producer.type_of_problem == "Add-in buttons are not functioning" || producer.type_of_problem == "Analysis Tool Issue" || producer.type_of_problem == "Add-in buttons are not functioning" || producer.type_of_problem == "Analysis Tool Issue" )
if (array[i] == "e3b273681bd985946dd375561a4bcb86" || array[i] == "8a2b2ca41b19c9546dd375561a4bcb43" || array[i] =="e2c2ff681bd985946dd375561a4bcb21" || array[i] =="45fa6c281bd5c9546dd375561a4bcb24" )
{
current.caller_id = producer.requester_name;
current.urgency = '3';
current.impact = '3';
current.contact_type = 'self-service';
current.category = 'application_software';
current.cmdb_ci = 'd0c524391b9c49104e2ea827bc4bcb87'; //Global OBU Forecasting Solution - Production
current.assignment_group = '6b3a79831bb3d0500e190d0cf94bcb5a'; //AppSrvc-Commercial-DnA-GLBL
current.u_preferred_contact_method = 'email';
current.short_description = 'OBU Forecasting AWS Migration' + 'prb';
current.description = producer.name_of_model.getDisplayValue() + '-' + producer.type_of_problem.getDisplayValue() + '-'+ producer.other.getDisplayValue() + '-'+ producer.detailed_description.getDisplayValue();
}
else if (array[i] == "cba233241bd985946dd375561a4bcbbb" || array[i] == "f13be0a81b19c9546dd375561a4bcb70" )
{
current.setAbortAction(true);
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('e359146c1b5589546dd375561a4bcb39'); //OBU Forecasting AWS Migration
cart.setVariable(item, 'requester_name', producer.requester_name);
cart.setVariable(item, 'location', producer.location);
cart.setVariable(item, 'manager_name_new', producer.manager_name_new);
cart.setVariable(item, 'contact_new', producer.contact_new);
cart.setVariable(item, 'global_local_request', producer.global_local_request);
cart.setVariable(item, 'site_impacted', producer.site_impacted);
cart.setVariable(item, 'request_selection', producer.request_selection);
//cart.setVariable(item, 'short_description', producer.short_description);
cart.setVariable(item, 'request_details', producer.request_details);
cart.setVariable(item, 'name_of_model', producer.name_of_model);
cart.setVariable(item, 'type_of_problem', producer.type_of_problem);
cart.setVariable(item, 'other', producer.other);
cart.setVariable(item, 'detailed_description', producer.detailed_description);
var rc = cart.placeOrder();
var req = new GlideRecord('sc_request');
req.addQuery('sys_id', rc.sys_id);
req.query();
if (req.next()) {
var parentID = RP.getParameterValue('sysparm_parent_sys_id');
//req.parent = producer.request_parent;
req.parent = parentID;
req.update();
var incRec = new GlideRecord("incident");
incRec.addQuery("sys_id", req.parent);
incRec.query();
if (incRec.next()) {
incRec.u_request = req.sys_id;
incRec.state = 8;
incRec.u_cancelled_reason = 'trans_to_sc_req';
incRec.update();}
}
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request', rc.sys_id);
ritm.query();
if (ritm.next()) {
//set your RITM fields here
// ritm.short_description = producer.short_description;
//ritm.description = producer.description;
ritm.user_input = "sysid:" + current.sys_id;
ritm.update();
GlideSysAttachment.copy('incident', current.sys_id, 'sc_req_item', ritm.sys_id);
}
current.number = ritm.number;
current.sys_id = ritm.sys_id;
producer.redirect = "sc_req_item.do?sys_id=" + current.sys_id;
}
}
Regards,
Kumar Nandan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 07:49 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 06:20 AM
I ran the below script in "scripts- background' and it ran fine,
It could be your list variable is the root cause of issue. Try printing it.
list = 'e3b273681bd985946dd375561a4bcb86,f13be0a81b19c9546dd375561a4bcb70';
var array = list.split(',');
for (var i=0; i < array.length; i++) {
gs.print("abc" + array[i]);
}