Creating Ritm Based on String filed value

vishalAg20
Tera Contributor

Hi All,
Around one requirement we need to create ritm based on string filed value like user can add multi user ids on this filed 

(hr234;hr235;hr236), based on particular hr id create separate ritm for each Hr id.

So How can I achieve this requirement and Please help me on this.

Thank you

1 ACCEPTED SOLUTION

Deepak Shaerma
Kilo Sage

Hi @vishalAg20 
you can use Business Rule or Flow Designer as well for the requirement.
Create a business rule
Apply to Service Catalog Request [sc_request] or Service Catalog Item [sc_req_item]), based on where you’re capturing the user IDs.
When to run: After Insert or After Update (According to your convinience)

// Assuming ā€˜user_ids_field’ is the field that holds the ID string
    var userIds = current.user_ids_field.toString();
    var idsArray = userIds.split(';');
    
    idsArray.forEach(function(id) {
        // Logic to create a RITM for each ID
        var ritmGR = new GlideRecord('sc_req_item');
        ritmGR.initialize();
        ritmGR.short_description = "RITM for HR ID: " + id; // Customize based on your requirements
        // Set other necessary fields
        ritmGR.insert();
    });


Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ā€Œā€Œ
Thanks & Regards 
Deepak Sharma 

View solution in original post

6 REPLIES 6

vishalAg20
Tera Contributor

Hi @Deepak Shaerma 
Thanks for solution
Ritm getting generate but Variable section not getting attach in Ritm and workflow also not getting . Pls let me know how to map variable section or workflow.

Please verify below code.

 var ritmGR = new GlideRecord('sc_req_item');      
        ritmGR.initialize();
          ritmGR.variable = idsArray[x];
        //ritmGr.cat_item = currentContext.cat_item;
        ritmGR.short_description = 'User  - User Id :'+ idsArray;
        ritmGR.opened_by =currentContext.requested_for;
        ritmGR.due_date = currentContext.due_date;
        ritmGR.description= 'User ID' + ' : ' + idsArray +'\n' + ' Requested By' + ':' + currentContext.requested_for + '\n' ;
      ritmGR.request = currentContext.request;
     
        
       
       
        ritmGR.insert();
               
    });
 },

    
 




vishalAg20
Tera Contributor

Hi @Deepak Shaerma 
I have write this script ritm is generating and workflow also getting but variable section not attached on ritm ho wto fix it.

(function executeRule(current, previous /*null when async*/) {
    // Splitting data by comma from the List Collector field
    var listCollectorData = current.variables.server.getDisplayValue();
    var values = listCollectorData.split(",");
    gs.log("List Collector Data: " + listCollectorData);
    gs.log("Number of Values: " + values.length);

    // Loop through each item in the List Collector field data
    for (var i = 0; i < values.length; i++) {
        var currentItem = values[i].trim(); // Remove leading/trailing spaces
        gs.log("Processing item " + (i + 1) + ": " + currentItem);
       
        // Check if a similar RITM already exists
        var existingRITM = new GlideRecord('sc_req_item');
        existingRITM.addQuery('short_description', 'CONTAINS', "Apple iPhone 6s");
        existingRITM.addQuery('request', current.request);
        existingRITM.query();
       
        if (existingRITM.next()) {
            gs.log("Similar RITM already exists for " + currentItem + ", skipping creation.");
            continue;
        }
       
        // Initialize a new RITM record
        var ritmGr = new GlideRecord('sc_req_item');
        ritmGr.initialize();
       
        // Populate RITM short description with data from List Collector field
        ritmGr.short_description = "Testing :" + currentItem;
        ritmGr.description = "Test -" + currentItem;
        ritmGr.request = current.request;
        ritmGr.cat_item = "d0b15e33d7033100a9ad1e173e24d49e"; // Replace with the correct catalog item ID
        gs.log("RITM Number with Link: " + current.number);
       
        // Insert the new RITM record
        var ritmSysId = ritmGr.insert();
        gs.log("Created RITM with Sys ID: " + ritmSysId);
       
       
       
        // Attach catalog item variable data to the RITM
var catalogItemVariables = new GlideRecord('sc_item_option_mtom');
catalogItemVariables.addQuery('request_item', current.sys_id); // Querying for variables related to the original request
catalogItemVariables.query();
gs.log("Number of catalog item variables retrieved: " + catalogItemVariables.getRowCount());
gs.log("GlideRecord: " + JSON.stringify(catalogItemVariables));
// Inside the while loop for processing catalog item variables
while (catalogItemVariables.next()) {
    // Retrieve variable name and value
    var variableName = catalogItemVariables.variable.getDisplayValue();
    var variableValue = catalogItemVariables.value.getDisplayValue();

    // Log the entire record to inspect its fields
    gs.log("Record: " + JSON.stringify(catalogItemVariables));

    // Check if variable name or value is undefined
    if (!variableName || !variableValue) {
        gs.log("Variable name or value is undefined. Skipping setting variable.");
    } else {
        // Set the variable value on the RITM
        ritmGr[variableName] = variableValue;
        gs.log("Set variable " + variableName + " to " + variableValue);

        // Log the set variable
        gs.log("RITM after setting variable: " + JSON.stringify(ritmGr));
    }
}

        ritmGr.update(); // Update the RITM record to save the variable values
       
        // Attach workflow to the created RITM
        var workflowId = "f5164a9037023000158bbfc8bcbe5d0a"; // Replace with the ID of your workflow
        var wfGr = new GlideRecord('wf_workflow');
        if (wfGr.get(workflowId)) {
            var context = new GlideScriptExecutionContext();
            context.setSecurityEnabled(true); // Enable security to run the workflow action
            var engine = new Workflow();
            var vars = {};
            var response = engine.startFlow(wfGr, ritmSysId, vars, context);
            gs.info("Workflow attached to RITM " + ritmSysId);
        } else {
            gs.error("Workflow not found with ID: " + workflowId);
        }
    }
})(current, previous);