BR to create multiple RITMS's based on data in variable

rambo1
Tera Guru

HI Team , 

On submittion of catalog item , ritm is created and if its updated, based on values in on one variable names 'uniquereccombo' it should create multiple ritm's.

for example, if variable has key1,key2,key3 values, on update of ritm it should create 2 more ritms.

first ritm variable values should be replaced with key1 other 2 ritm's variable values should be key2, key 3 .

I am using below code in BR , RITM's are getting created but variable value is being key1 in all three.

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

    // Add your code here
    //assuming 'unique rec combo' is variable that holds roles
    var roles = current.variables.u_unique_rec_combo.split(',');


    if (roles.length > 0) {
        current.variables.u_unique_rec_combo = roles[0];
        current.setWorkflow(true);
        current.update();
        for (i = 1; i < roles.length; i++) {
            // Create a new RITM
            var newRITM = new GlideRecord('sc_req_item');
            newRITM.initialize();
            newRITM.parent = current.sys_id;
            newRITM.request = current.request;
            newRITM.cat_item = current.cat_item;
            newRITM.short_description = current.short_description;
            newRITM.description = roles[i];
            newRITM.insert();

            // Copy variable values from original RITM to the new RITM except roles
            var originalVariables = new GlideRecord('sc_item_option_mtom');
            originalVariables.addQuery('request_item', current.sys_id);
            //originalVariables.addEncodedQuery('sc_item_option.item_option_new=!ce559a8bfbcd5a1026f7f2b455efdccc^request_item=' + current.sys_id + current.sys_id);
            originalVariables.query();
            while (originalVariables.next()) {
                var newVariable = new GlideRecord('sc_item_option_mtom');
                newVariable.initialize();
                newVariable.request_item = newRITM.sys_id;
                if (originalVariables.item_option_new != 'ce559a8bfbcd5a1026f7f2b455efdccc') {
                    newVariable.sc_item_option = originalVariables.sc_item_option;
                } else {
                    newVariable.sc_item_option = insertIOL(roles[i]);
                }
                //newVariable.value = originalVariables.value;
                newVariable.insert();
            }


            //copy role variable
            // var RoleVariable = new GlideRecord('sc_item_option_mtom');
            // RoleVariable.addEncodedQuery('sc_item_option.item_option_new=ce559a8bfbcd5a1026f7f2b455efdccc^request_item=' + newRITM.sys_id);
            // RoleVariable.query();
            // gs.addErrorMessage('@@@ query ' + 'sc_item_option.item_option_new=ce559a8bfbcd5a1026f7f2b455efdccc^request_item=' + newRITM.sys_id)
            // while (RoleVariable.next()) {
            //     // var newRoleVariable = new GlideRecord('sc_item_option_mtom');
            //     // newRoleVariable.initialize();
            //     // newRoleVariable.request_item = newRITM.sys_id;
            //     // newRoleVariable.sc_item_option = createNewItemOption(RoleVariable.sc_item_option, roles[i]);
            //     // //newRoleVariable.value = roles[i];

            //     // //gs.addErrorMessage('@@@roles ' + roles[i]);
            //     // newRoleVariable.insert();

            //     updateIOL(RoleVariable.sc_item_option, roles[i]);

            // }
        }
        //update parent RITM with one role

        //    current.variables.u_unique_rec_combo = roles[0];
        //    current.setWorkflow(true);
        //    current.update();
    }

    function insertIOL(v) {
        //gs.addErrorMessage('@@@roles ' + v);
        var iol = new GlideRecord('sc_item_option_list');
        iol.initialize();
        iol.item_option_new = 'ce559a8bfbcd5a1026f7f2b455efdccc';
        iol.value = v;
        var iol_id = iol.insert();
        return iol_id;
    }

    // function updateIOL(id, v) {
    //     gs.addErrorMessage('@@@IOLroles ' + v);
    //     var iol = new GlideRecord('sc_item_option_list');
    //     iol.get(id);
    //     iol.value = v;
    //     //iol.update();
    //}
})(current, previous);
2 REPLIES 2

Aniket Chavan
Tera Sage
Tera Sage

Hello @rambo1 ,

 

Please try the script below and let me know how it works for you:

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

    // Split the 'u_unique_rec_combo' variable value into an array of roles
    var roles = current.variables.u_unique_rec_combo.split(',');

    if (roles.length > 0) {
        // Assign the first role to the current RITM
        current.variables.u_unique_rec_combo = roles[0];
        current.setWorkflow(false); // Optional: Avoid looping
        current.update();

        // Loop through the remaining roles to create additional RITMs
        for (var i = 1; i < roles.length; i++) {
            var newRITM = new GlideRecord('sc_req_item');
            newRITM.initialize();
            newRITM.parent = current.sys_id;
            newRITM.request = current.request;
            newRITM.cat_item = current.cat_item;
            newRITM.short_description = current.short_description;
            newRITM.insert();

            // Copy variable values from the original RITM to the new RITM
            var originalVariables = new GlideRecord('sc_item_option_mtom');
            originalVariables.addQuery('request_item', current.sys_id);
            originalVariables.query();
            while (originalVariables.next()) {
                var newVariable = new GlideRecord('sc_item_option_mtom');
                newVariable.initialize();
                newVariable.request_item = newRITM.sys_id;

                // Set specific role to 'u_unique_rec_combo' variable for each RITM
                if (originalVariables.sc_item_option == 'u_unique_rec_combo') {
                    newVariable.sc_item_option = insertIOL(roles[i]);
                } else {
                    newVariable.sc_item_option = originalVariables.sc_item_option;
                    newVariable.value = originalVariables.value;
                }
                newVariable.insert();
            }
        }
    }

    // Function to create an entry in 'sc_item_option_list' for the role variable
    function insertIOL(roleValue) {
        var iol = new GlideRecord('sc_item_option_list');
        iol.initialize();
        iol.item_option_new = 'ce559a8bfbcd5a1026f7f2b455efdccc';
        iol.value = roleValue;
        return iol.insert();
    }

})(current, previous);

 

If it doesn’t work for you, please let me know. I’ve implemented this functionality before, so I’d be happy to take a closer look at both scripts and help troubleshoot.


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


Regards,
Aniket





Still the same issue .

I have submitted item with key1,key2,key3 as value in 'uniqureccombo' variable , after that updted ritm, it created 3 ritms.

but 'uniquereccombo' variable value is key1 in all three RITM's

I have added one line 

newRITM.description = roles[i];
This is populated key1 in ritm 1, key2 in ritm2 but variable is key1 in all ritm's