Create multiple RITMs based on list collector variable selected

suuriya
Tera Contributor

HI Community,

 

I have a requirement, in catalog form there is list collector field called select csos environment if i select 3 options then 3 ritms need to be created..so i have written this script in run script activity in workflow it worked but it created 21 ritms for one ritm it shows all 3 options in list collector variable and rest of the ritms there is no value present in list collector field

var opt = current.variables.select_csos_environment.toString().split(',');
for (i=0; i < opt.length; i++) {
    createRequest();
}

    function createRequest() {
       
       
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('9a415a5187e0ca14755b33773cbb3516', 1);


        cart.setVariable(item, 'employee_name', current.variables.employee_name);
       
        cart.setVariable(item, 'what_type_of_request_is_this', current.variables.what_type_of_request_is_this);

var rc = cart.placeOrder();
}
 
can you please let me know how we can achieve this ....each ritm need to have single option selected in list collector field and only 3 need to be created
1 ACCEPTED SOLUTION

Hello @suuriya ,

Got it, please try with the modified code below and see how it works for you, also don't forget to replace the backend name of your variable "What type of request is this?" and also values of the choices for "add or modify"

(function executeRule(current, previous /*null when async*/ ) {
    // Check if "What type of request is this?" is set to "add" or "modify"
    var requestType = current.variables.what_type_of_request_is_this;
    if (requestType == 'add' || requestType == 'modify') {
        var tcmc = '9a415a5187e0ca14755b33773cbb3516';
        var name = current.variables.select_csos_environment.toString();
        var employeeName = name.split(',');

        var name1 = current.variables.select_csos_environment.getDisplayValue();
        var employeeName1 = name1.split(',');
        for (var i = 1; i < employeeName.length; i++) {

            var gr1 = new GlideRecord('sc_req_item');
            gr1.initialize();
            gr1.cat_item = current.cat_item;
            gr1.request = current.request;
            gr1.short_description = "Termination: " + employeeName1[i];
            gr1.requested_for = current.variables.requested_for; //replace variable name per your variable
            gr1.insert();

            GlideSysAttachment.copy('sc_req_item', current.sys_id, 'sc_req_item', gr1.sys_id);

            var newRITM = gr1.sys_id;

            var sc_item_option_mtom = new GlideRecord('sc_item_option_mtom');
            sc_item_option_mtom.addQuery('request_item', current.sys_id);
            sc_item_option_mtom.query();
            while (sc_item_option_mtom.next()) {
                var sc_item_option = new GlideRecord('sc_item_option');
                sc_item_option.addQuery('sys_id', sc_item_option_mtom.sc_item_option);
                sc_item_option.query();
                var j = 0;
                if (sc_item_option.next()) {
                    var sc_item_option_insert = new GlideRecord('sc_item_option');
                    sc_item_option_insert.initialize();
                    sc_item_option_insert.item_option_new = sc_item_option.item_option_new;

                    if (sc_item_option.item_option_new == "7fb719f21b208e9045fc99fa234bcb8b") {  //list collector variable sys_id
                        var names = sc_item_option.value;
                        sc_item_option_insert.value = employeeName[i];

                    } else {
                        sc_item_option_insert.value = sc_item_option.value;
                    }
                    sc_item_option_insert.order = sc_item_option.order;
                    sc_item_option_insert.insert();
                    var sc_item_option_mtom_insert = new GlideRecord('sc_item_option_mtom');
                    sc_item_option_mtom_insert.initialize();
                    sc_item_option_mtom_insert.request_item = newRITM;
                    sc_item_option_mtom_insert.sc_item_option = sc_item_option_insert.sys_id;
                    sc_item_option_mtom_insert.insert();
                    j++;
                }
            }
        }
        current.short_description = "Termination: " + employeeName1[0];
        current.requested_for = current.variables.requested_for; //replace variable name per your variable
        current.update();

        var sc_item_option_mtom1 = new GlideRecord('sc_item_option_mtom');
        sc_item_option_mtom1.addQuery('request_item', current.sys_id);
        sc_item_option_mtom1.query();
        while (sc_item_option_mtom1.next()) {
            var sc_item_option1 = new GlideRecord('sc_item_option');
            sc_item_option1.addQuery('sys_id', sc_item_option_mtom1.sc_item_option);
            sc_item_option1.addEncodedQuery('item_option_new=7fb719f21b208e9045fc99fa234bcb8b');  //list collector variable sys_id
            sc_item_option1.query();
            if (sc_item_option1.next()) {
                sc_item_option1.value = employeeName[0];
                sc_item_option1.update();
            }
        }
    }
})(current, previous);

 

View solution in original post

15 REPLIES 15

HI @Maddysunil ,

 

Thanks for your reply i tried the above script but still multiple ritms are getting triggered like 21 ritms triggered and expect one all other ritms requestod for name is showing my name and not the actual persons name