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

Satish Rana1
Tera Contributor

Hi @suuriya ,

Can you please share the screen shot of the form and you are setting value in two fields which field is list collector type.
 

Thank you

HI @Satish Rana1 ,

at the time while submitting the form 

suuriya_0-1709035831380.png

and in only one ritm selected option is present in list collector

suuriya_2-1709036062863.png

 

rest all are like this no selected option in list collector

suuriya_1-1709035906468.png

 

Hello @suuriya ,

Please see my below response once and let me know if you have any doubts , so that we can discuss those further accordingly.

Thanks & Regards,
Aniket.

Aniket Chavan
Tera Sage
Tera Sage

Hello @suuriya ,

I recently implemented a solution for a similar requirement using an after-insert business rule. The goal was to create multiple RITMs based on choices selected in a list collector variable, ensuring that each RITM displayed only one value in the list collector.

You can adapt the provided code as a reference for your specific use case. Simply replace the variable names and item/sys_id with the corresponding details from your environment. Here's the adjusted code:

 

(function executeRule(current, previous /*null when async*/ ) {
    var tcmc = gs.getProperty('Item_sysID.Employee_Termination');
    var name = current.variables.employee.toString();
    var employeeName = name.split(',');

    var name1 = current.variables.employee.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.u_employee_name_e_term = employeeName1[i];
        gr1.location = current.location;
        gr1.state = current.state;
        gr1.stage = current.stage;
        gr1.watch_list = current.watch_list;
        gr1.u_effective_date = current.u_effective_date;
        gr1.assignment_group = current.assignment_group;
        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 == 'b0f49c73dbb41b407a0fe536ca9619fd') {
                    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.u_employee_name_e_term = employeeName1[0];

    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=b0f49c73dbb41b407a0fe536ca9619fd');
        sc_item_option1.query();
        if (sc_item_option1.next()) {
            sc_item_option1.value = employeeName[0];
            sc_item_option1.update();
        }
    }


})(current, previous);

 

Also you can refer this link as well for your reference : Generate multiple RITMs based on list collector variable

 

Please let me know if you need any further help or have any doubt & please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket