Multiple RITM from a catalog

lakshmiprasuna6
Mega Contributor

 

Requirement is, we have a list collector variable referencing User table. When the catalog is submitted it should create a seperate RITM for each user selected in the list collector as requested for.

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

Hi @lakshmiprasuna6 

You can create an onBefore - Insert business rule on sc_req_item table like the one below to achieve this. I tried to create a separate RITM based on the users present in the List Collector variable and make the current RITM as parent for the individual.

 

(function executeRule(current, previous /*null when async*/ ) {

    var users = current.variables.req_for_users + ''; //Change variable name to your List collector variable name
    users = users.split(',');
    var parent_variables = current.variables;

    for (var i = 0; i < users.length; i++) {
        var ritmGr = new GlideRecord("sc_req_item");
        ritmGr.initialize();
        for (key2 in current) {
            if (key2 != 'number' && key2 != 'sys_id') {
                ritmGr[key2] = current[key2];
            }
        }

        ritmGr.setValue('requested_for', users[i]);
        ritmGr.setValue('parent', current.getUniqueValue());
        var ritm_sys_id = ritmGr.insert();
		
		copyVariables(ritm_sys_id, current.getValue('cat_item'), parent_variables);
    }

    function copyVariables(ritm, cat_item, variables) {
        var grCatItemVar = new GlideRecord("item_option_new");
        grCatItemVar.addQuery("cat_item", cat_item);
        grCatItemVar.query();
        while (grCatItemVar.next()) {
            var grRitmVar = new GlideRecord("sc_item_option");
            grRitmVar.initialize();
            grRitmVar.item_option_new = grCatItemVar.sys_id + "";
            grRitmVar.order =grCatItemVar.order + "";
            if (variables[grCatItemVar.name]) {
                grRitmVar.value = variables[grCatItemVar.name];
            } else {
                grRitmVar.value = grCatItemVar.default_value + "";
            }
            grRitmVar.insert();
            var grRitmVarM2M = new GlideRecord("sc_item_option_mtom");
            grRitmVarM2M.initialize();
            grRitmVarM2M.request_item = ritm;
            grRitmVarM2M.sc_item_option = grRitmVar.sys_id + "";
            grRitmVarM2M.insert();
        }

    }

})(current, previous);

 

AnveshKumarM_0-1702281965270.png

 

AnveshKumarM_1-1702281987823.png

 

AnveshKumarM_2-1702282008047.png

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

 

Thanks,
Anvesh

View solution in original post

6 REPLIES 6

AnveshKumar M
Tera Sage
Tera Sage

Hi @lakshmiprasuna6 

You can create an onBefore - Insert business rule on sc_req_item table like the one below to achieve this. I tried to create a separate RITM based on the users present in the List Collector variable and make the current RITM as parent for the individual.

 

(function executeRule(current, previous /*null when async*/ ) {

    var users = current.variables.req_for_users + ''; //Change variable name to your List collector variable name
    users = users.split(',');
    var parent_variables = current.variables;

    for (var i = 0; i < users.length; i++) {
        var ritmGr = new GlideRecord("sc_req_item");
        ritmGr.initialize();
        for (key2 in current) {
            if (key2 != 'number' && key2 != 'sys_id') {
                ritmGr[key2] = current[key2];
            }
        }

        ritmGr.setValue('requested_for', users[i]);
        ritmGr.setValue('parent', current.getUniqueValue());
        var ritm_sys_id = ritmGr.insert();
		
		copyVariables(ritm_sys_id, current.getValue('cat_item'), parent_variables);
    }

    function copyVariables(ritm, cat_item, variables) {
        var grCatItemVar = new GlideRecord("item_option_new");
        grCatItemVar.addQuery("cat_item", cat_item);
        grCatItemVar.query();
        while (grCatItemVar.next()) {
            var grRitmVar = new GlideRecord("sc_item_option");
            grRitmVar.initialize();
            grRitmVar.item_option_new = grCatItemVar.sys_id + "";
            grRitmVar.order =grCatItemVar.order + "";
            if (variables[grCatItemVar.name]) {
                grRitmVar.value = variables[grCatItemVar.name];
            } else {
                grRitmVar.value = grCatItemVar.default_value + "";
            }
            grRitmVar.insert();
            var grRitmVarM2M = new GlideRecord("sc_item_option_mtom");
            grRitmVarM2M.initialize();
            grRitmVarM2M.request_item = ritm;
            grRitmVarM2M.sc_item_option = grRitmVar.sys_id + "";
            grRitmVarM2M.insert();
        }

    }

})(current, previous);

 

AnveshKumarM_0-1702281965270.png

 

AnveshKumarM_1-1702281987823.png

 

AnveshKumarM_2-1702282008047.png

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

 

Thanks,
Anvesh

Thanks Anvesh!

 

I'll try this.

@AnveshKumar M Thanks it worked for me.

Ankur Bawiskar
Tera Patron
Tera Patron

@lakshmiprasuna6 

you can try to use Flow designer for this and use Submit Catalog Item request

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