Creating multiple RITMs from Multi row variable set

sree42
Tera Contributor

Hi Team,

I have one requirement to generate multiple RITM based on the multi row variable set selected in the catalog item. Please anyone let me know how can I achieve this?

4 REPLIES 4

Vishal Birajdar
Giga Sage

Hi @sree42 

 

Below link might help you to achieve this if you are using the workflow :

 

https://www.servicenow.com/community/developer-forum/create-multiple-ritm-under-single-request-depen...

 

I have done this using List collector , you can adjust your code accordingly to use MRVS.

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Hi Vishal,

Thanks a lot for this code. I am still confused how can I achieve this in multi row variable set. Based on the row, I need to crete the RITM. Could you please guide me on this?

Hi @Neeraja2 

 

1. You can get the MRVS in workflow run script activity by using - "current.variables.<your_mrvs_name>"

2. As its a object we need to parse it -  JSON.parse();

3.Then we can loop through rows using for loop

 

 

var mrvs = current.variables.<your_mrvs_name>
var parseMrvs = JSON.parse(mrvs);
for (var i = 0; i < parseMrvs.length; i++) {
    
    /* here you will get the fields from MRVS  & can be used to set variables on RITM*/
   var v1 = parseMrvs[i].field1.toString();
   var v2 = parseMrvs[i].field2.toString();

  /* follow the step 4 from link mentioned in previous reply */
    
    
}

 

 

Hope this helps...!!

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Hi @Vishal Birajdar 

 

Thank you for the post.  I am able to create multiple RITMs based on the rows in MRVS, but the varaibels are getting copied only to the first RITM and not to others, for e.g there are three rows in MRVS it creates 3 RITMs but copies the variables in only the first RITM and not the others.

Is it possible to map the first row to the first RITM and second row to second RITM?

 

Below is the sample code I am using...

 

 

 

/* ============Start of run script activity===============*/
/* 1. Get values from user list variable */
var list = current.variables.name;
 
/* 2. Get current RITM’s request - this will be used to set the request on multiple RITMs to be created */
var request = current.request;
 
/* 3. Store sys_id of our catalog item */
var catalogItem = '4ef3c2dd83241210e70a92b6feaad3bb'; // Not to hardcode (use system property)
 
/* 4. Loop through the MRVS and create RITMs */
for (var i = 0; i < list.getRowCount(); i++) {
    var ritmSysId;
 
    // For the first iteration, use the current RITM
    if (i === 0) {
        ritmSysId = current.sys_id;
    } else {
        // Create new RITM
        var grRitm = new GlideRecord("sc_req_item");
        grRitm.initialize();
        grRitm.cat_item = catalogItem;
        grRitm.price = current.price;
        grRitm.request = request;
        grRitm.requested_for = list[i].user;
        grRitm.due_date = current.due_date;
        ritmSysId = grRitm.insert();
       
        gs.log("Created new RITM with sys_id: " + ritmSysId, "MRVS Script");
    }
 
    // Create and associate variables with the RITM
    var itemVar = new GlideRecord('item_option_new');
    itemVar.addEncodedQuery('cat_item=' + catalogItem + '^variable_set=c4240add83241210e70a92b6feaad375');
    itemVar.query();
   
    while (itemVar.next()) {
        var itemOption = new GlideRecord('sc_item_option');
        itemOption.initialize();
        itemOption.order = itemVar.order;
        itemOption.item_option_new = itemVar.sys_id;
 
        if (itemVar.name == '2abc7695832c1210e70a92b6feaad374') {
            itemOption.value = 'No'; // Set the value to "No" so the flow will divert and not get into a loop
        }
        if (itemVar.name == '8e74021183641210e70a92b6feaad38a') {
            itemOption.value = list[i].user;
        }
 
        var optSysID = itemOption.insert();
       
        gs.log("Created variable " + itemVar.name + " with sys_id: " + optSysID + " for RITM: " + ritmSysId, "MRVS Script");
 
        // Create a relationship between the RITM and the variable value
        var ritmM2M = new GlideRecord('sc_item_option_mtom');
        ritmM2M.initialize();
        ritmM2M.request_item = ritmSysId; // Parent Item
        ritmM2M.sc_item_option = optSysID; // Dependent Value
        ritmM2M.insert();
       
        gs.log("Created relationship for variable with sys_id: " + optSysID + " to RITM: " + ritmSysId, "MRVS Script");
    }
 
    // Start the workflow for the new RITM
    if (i !== 0) {
        startWorkflow('c0cfe60783109a10b03b9ea6feaad3b8', ritmSysId);
    }
}
 
/* Attach workflow to RITM */
function startWorkflow(workflowSysId, ritmSysId) {
    var grRitm = new GlideRecord("sc_req_item");
    if (grRitm.get(ritmSysId)) {
        var w = new Workflow();
        var context = w.startFlow(workflowSysId, grRitm, grRitm.operation(), getVars(grRitm));
        if (context != null) {
            grRitm.context = context.sys_id;
            grRitm.update();
            gs.log("Started workflow " + workflowSysId + " for RITM: " + ritmSysId + " with context: " + context.sys_id, "MRVS Script");
        }
    }
}
 
/* Send the RITM variables to workflow */
function getVars(grRitm) {
    var vars = {};
    for (var n in grRitm.variables) {
        vars[n] = grRitm.variables[n];
    }
    return vars;
}
 
/*====================End of run script Activity===============================*/