Going inside loop so many times

indrajeetma
Tera Guru
(function executeRule(current, previous /*null when async*/) {
    var acc = current.u_account.toString();
    var exp = new GlideDate(current.u_expiration_date);
    var loc = current.u_location;
    var listt = current.u_list_product_family;
    gs.log("indraa " + listt);
    var array = listt.split(",");
    gs.log("indraa " + array + " " + array.length);
    var length = array.length;
    for(var i =0 ; i<length; i++)
    {
        gs.log("hi");
        var flag = false;
        var gr = new GlideRecord('u_test');
        gr.addQuery('u_account', acc);
        gr.addQuery('u_expiration_date', exp);
        gr.addQuery('u_location', loc);
        gr.addQuery('u_product_fam', array[i]);
        gr.setLimit(1);
        gr.query();
        if(gr.next())
        {
            flag = true;
           

        }
        gs.log("indra " + flag);
        if(flag == false)
        {
            var insert1 = new GlideRecord('u_test');
            insert1.initialize();
            insert1.u_account = acc;
            insert1.u_location = loc;
            insert1.u_expiration_date = exp;
            insert1.u_product_fam = array[i];
            insert1.u_list_product_family = listt;
            insert1.insert();
        }
    }



    //current.setAbortAction(true);
})(current, previous);
5 REPLIES 5

Mark Manders
Mega Patron

If it is this you are looking to achieve and you are moving away from client side, why not just create a flow for this, instead of all of this scripting? You can easily put validations in there and prevents all kind of loops. 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark