Flow is triggered for custom RITM

dhivyal94299399
Tera Contributor

Requirement:

 

I have a Service Catalog form that contains an MRVS (Multi Row Variable Set). Once the user submits the form, for each MRVS row, a separate RITM should be created under a single Request.

 

Problem:

 

The RITMs are being generated successfully using a custom Flow Designer action for each MRVS row. However, the issue is that the Flow Context from the parent/original RITM is getting attached to the newly created custom RITMs as well. Because of this, the flow gets retriggered for the custom RITMs, approvals and catalog tasks are generated again, and the process goes into an infinite loop.

  • However, after completion, the Flow Context from the parent/original RITM gets attached to the custom RITMs automatically. Due to this, the approvals and catalog tasks start triggering again from the beginning for the custom RITMs, causing the flow to loop continuously.

 

Expectation:

 

  • One Request should contain multiple RITMs (one per MRVS row)
  • Newly created custom RITMs should not inherit or attach to the original Flow Context

 

script custom action script:

(function execute(inputs, outputs) {
 
    var mrvs = inputs.mrvs;
 
    var ritm = new GlideRecord('sc_req_item');
    ritm.get(inputs.ritm);
 
    var createdRITMs = [];
 
    for (var i = 0; i < mrvs.length; i++) {
 
        var row = mrvs[i];
 
       
 
        var applicationName =
            row.select_module;
 
        var requestedForName =
            row.name;
 
        var roleName =
            row.select_role;
 
        var accessAction =
            row.select_access_type;
 
        // DYNAMIC SHORT DESCRIPTION
        var shortDescription = '[Offering] ' + applicationName +
    ' | [Role] ' + roleName +
    ' | [Action] ' + accessAction;
 
 
        // GET SERVICE OFFERING
     
 
        var so = new GlideRecord('service_offering');
 
        so.addQuery(
            'name',
            applicationName
        );
 
        so.query();
 
        var serviceOfferingSysId = '';
        var businessServiceSysId = '';
        var assignmentGroup = '';
 
        if (so.next()) {
 
            serviceOfferingSysId =
                so.sys_id.toString();
 
            businessServiceSysId =
                so.parent.toString();
 
           
            assignmentGroup =
                so.parent.change_control.toString();
        }
 
        // UPDATE EXISTING OOB RITM
   
 
        if (i == 0) {
 
            ritm.short_description =
                shortDescription;
 
            ritm.business_service =
                businessServiceSysId;
 
            ritm.service_offering =
                serviceOfferingSysId;
 
            ritm.assignment_group =
                assignmentGroup;
 
            ritm.stage =
                'Waiting for Approval';
 
            ritm.update();
 
            // UPDATE VARIABLES
         
 
            var existingMtom =
                new GlideRecord(
                    'sc_item_option_mtom'
                );
 
            existingMtom.addQuery(
                'request_item',
                ritm.sys_id
            );
 
            existingMtom.query();
 
            while (existingMtom.next()) {
 
                var existingOption =
                    new GlideRecord(
                        'sc_item_option'
                    );
 
                if (existingOption.get(
                    existingMtom.sc_item_option
                )) {
 
                    var variableName =
                        existingOption
                        .item_option_new
                        .name
                        .toString();
 
                    // UPDATE NAME VARIABLE
                    if (variableName == 'name') {
 
                        existingOption.value =
                            requestedForName;
 
                        existingOption.update();
                    }
                }
            }
 
            createdRITMs.push(
                ritm.sys_id.toString()
            );
        }
 
        // CREATE NEW CUSTOM RITMS
       
 
        else {
 
            var newRITM =
                new GlideRecord(
                    'sc_req_item'
                );
 
            newRITM.initialize();
 
            newRITM.request =
                ritm.request;
 
            newRITM.cat_item =
                ritm.cat_item;
 
            newRITM.requested_for =
                ritm.requested_for;
 
            newRITM.opened_by =
                ritm.opened_by;
 
            newRITM.due_date =
                ritm.due_date;
 
            newRITM.stage =
                'Waiting for Approval';
 
            newRITM.short_description =
                shortDescription;
 
            newRITM.business_service =
                businessServiceSysId;
 
            newRITM.service_offering =
                serviceOfferingSysId;
 
            newRITM.assignment_group =
                assignmentGroup;
 
            var newSysId =
                newRITM.insert();
 
            // COPY NORMAL VARIABLES
         
 
            var oldMtom =
                new GlideRecord(
                    'sc_item_option_mtom'
                );
 
            oldMtom.addQuery(
                'request_item',
                ritm.sys_id
            );
 
            oldMtom.query();
 
            while (oldMtom.next()) {
 
                var oldOption =
                    new GlideRecord(
                        'sc_item_option'
                    );
 
                if (oldOption.get(
                    oldMtom.sc_item_option
                )) {
 
                    var newOption =
                        new GlideRecord(
                            'sc_item_option'
                        );
 
                    newOption.initialize();
 
                    newOption.item_option_new =
                        oldOption.item_option_new;
 
                    var variableName =
                        oldOption
                        .item_option_new
                        .name
                        .toString();
 
                    // UPDATE NAME VARIABLE
                    if (variableName == 'name') {
 
                        newOption.value =
                            requestedForName;
 
                    } else {
 
                        newOption.value =
                            oldOption.value;
                    }
 
                    var newOptionSysId =
                        newOption.insert();
 
                    var newMtom =
                        new GlideRecord(
                            'sc_item_option_mtom'
                        );
 
                    newMtom.initialize();
 
                    newMtom.request_item =
                        newSysId;
 
                    newMtom.sc_item_option =
                        newOptionSysId;
 
                    newMtom.insert();
                }
            }
 
           
            // COPY MRVS VALUES
         
 
            var mrvsGR =
                new GlideRecord(
                    'sc_multi_row_question_answer'
                );
 
            mrvsGR.addQuery(
                'parent_id',
                ritm.sys_id
            );
 
            mrvsGR.query();
 
            while (mrvsGR.next()) {
 
                var newMrvs =
                    new GlideRecord(
                        'sc_multi_row_question_answer'
                    );
 
                newMrvs.initialize();
 
                newMrvs.parent_id =
                    newSysId;
 
                newMrvs.parent_table_name =
                    mrvsGR.getValue(
                        'parent_table_name'
                    );
 
                newMrvs.question =
                    mrvsGR.getValue(
                        'question'
                    );
 
                newMrvs.variable_set =
                    mrvsGR.getValue(
                        'variable_set'
                    );
 
                newMrvs.item_option_new =
                    mrvsGR.getValue(
                        'item_option_new'
                    );
 
                newMrvs.row_index =
                    mrvsGR.getValue(
                        'row_index'
                    );
 
                newMrvs.value =
                    mrvsGR.getValue(
                        'value'
                    );
 
                newMrvs.insert();
            }
 
            createdRITMs.push(
                newSysId.toString()
            );
        }
    }
 
    outputs.created_ritms = createdRITMs;
    gs.log("rimt number"+createdRITMs);
 
})(inputs, outputs);

 

5 REPLIES 5

@dhivyal94299399 

the RITMs you are creating while iterating MRVS are for which Catalog item?

If same then yes the same flow will get attached to the newly created RITMs, that's OOTB behavior

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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