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

Hello @suuriya ,

Glad to hear that it worked fine for you.🤗

To address your requirement, let me confirm if I understand correctly: you wish to generate multiple RITMs only when the "type" variable in the catalog form is selected as "add" or "modify," and not for the other two options, namely "remove" and "password." If this interpretation is accurate, I can certainly modify the script accordingly.

Please share your thoughts, and I'll proceed accordingly.

Thanks & Regards,
Aniket.

HI @Aniket Chavan ,

 

Exactly.

In catalog form there is select box field called What type of request is this?(add/modify/remove/password) if user selects add or modify and then if he select multiple options in select environment (list collector) then the ritms should split....if user select remove or pass and then if he select multiple options in select environment then only one ritm should be raised (oob expected) 

 

split 

suuriya_0-1709132889695.png

no split

suuriya_1-1709132922591.png

 

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);

 

HI @Aniket Chavan ,

 

Thanks it worked.

Maddysunil
Kilo Sage

@suuriya 

Please try below updated code:

 

var opt = current.variables.select_csos_environment.toString().split(',');

for (var i = 0; i < opt.length; i++) {
    if (opt[i].trim() !== '') { // Check if the option is selected
        createRequest(opt[i].trim());
    }
}

function createRequest(selectedOption) {
    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);
    cart.setVariable(item, 'select_csos_environment', selectedOption); // Set the selected option

    var rc = cart.placeOrder();
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.