For loop repeating multiple times in the record producer script

Evan2
Kilo Guru

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

6 REPLIES 6

Weird
Mega Sage

Phew, that code is hard to read. For future reference if you hard code sys_id's make them variables since it's easier to read the conditions.

The starting if doesn't make sense if you can have multiple options.
You'd need to use gliderecord insert if you're creating new records. Now all you do is set the current values (before insert) loop through and set new values and with good chance you'll also abort the current action on one of the loops which results in no record being created.

So replace everything in the first if to be a new gliderecord and use gr.initialize with it.

You also say

		current.number = ritm.number;
		current.sys_id = ritm.sys_id;
		producer.redirect = "sc_req_item.do?sys_id=" + current.sys_id;

Why? Just say "sc_req_item.do?sys_id=" + ritm.sys_id; instead.
Also if you're creating multiple records you're just changing that value again and again.
It would make more sense to have a a variable record the latest ritm sys_id and then outside of the if else you should change the redirect based on the variable 

To clarify the first part here's a simplified script to show why it's wrong

for (var i=0; i < array.length; i++) {
	if (array[i] =="45fa6c281bd5c9546dd375561a4bcb24" ){
		current.caller_id = producer.requester_name; //Setting current

	}else if (array[i] == "cba233241bd985946dd375561a4bcbbb"){
		current.setAbortAction(true); //Aborting previous current
	}

}

//If no setAbortAction was used then here we will create a record based on current values. If we looped through the first if 4 times, only the last loops values are stored and one record is created.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Script looks fine to me.

Did you add gs.info() to debug if it is iterating well?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

I have applied gs.log("def" + array.length) after the for loop but in logs I can see 2nd array only  is getting repeating multiple times and that is not as expected. PFB the screenshot. 

find_real_file.png

 

Regards,

Kumar Nandan

before for loop what's the array length?

did you print the list variable?

var list = producer.type_of_problem.toString();

gs.info("Data is"+ list);

var array = list.split(',');
for (var i=0; i < array.length; i++) {

    gs.log("def" + array.length);
    
    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,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader