Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Copy all the variables and its values while converting from RITM to INC

DB1
Tera Contributor

Hi Team,

 

There is a requirement for me to create an UI Action which converts RITM to INC.

So, I created an UI Action on RITM table and a Script Include.

Now the ask is to copy all the variables of the RITM to the description of the Incident.

 

I am trying something below:

 var allVARS = new global.GlobalServiceCatalogUtil().getVariablesForTask(ritm, false);
            var varlist = JSON.stringify(allVARS);
           
            gs.log("All vars " +varlist);
 
It returns the value of all the variables but 
 
All vars [{"label":"Open on behalf of this user","display_value":"BB","visible_summary":true,"multi_row":false,"type":8,"value":"8d9b33fa4718919075211711e36d43c3"},{"label":"Product","display_value":"abcd","visible_summary":true,"multi_row":false,"type":8,"value":"8244edba9756dd503334fdffe153af2f"}]
 
I am looking for to set the variable to description of the Incident in a better way. Need help on the same.
 
Thanks,
1 ACCEPTED SOLUTION

sunil maddheshi
Tera Guru

@DB1 

Steps:

  1. Extract the label and display_value from allVARS.
  2. Format them as a readable string.
  3. Set this formatted string as the description of the Incident.

 

var allVARS = new global.GlobalServiceCatalogUtil().getVariablesForTask(ritm, false);
var descriptionText = '';

for (var i = 0; i < allVARS.length; i++) {
    var label = allVARS[i].label;  
    var value = allVARS[i].display_value;  
    descriptionText += label + ': ' + value + '\n';  // Formatting for better readability
}

// Assuming 'incident' is the newly created incident record
incident.description = descriptionText;
incident.update();

 

Please mark helpful/ Accept solution if this helps you 

View solution in original post

6 REPLIES 6

Viraj Hudlikar
Tera Sage
Tera Sage

Hello @DB1 

 

Check this article 

(1) Convert an RITM to an Incident - ServiceNow Community

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Siddhesh Gawade
Mega Sage
Mega Sage

Hello @DB1 , 

 

You can below code to show RITM variables on description of incident:

 var Num = current.sys_id;
    var gr = new GlideRecord('sc_req_item');
    gr.addQuery('sys_id', Num);
    gr.query();
    if(gr.next()) {
        //store the ritm number and variable information if required
        var ritmNum = gr.getValue('number');
        var itemName = gr.getDisplayValue('cat_item');
        var names = Object.keys(gr.variables);
        var string = '';
        for (var i = 0; i < names.length; i++) {
            //get the all variables and store.
            var variable = gr.variables[names[i]];
            if (variable.getLabel() && variable.getDisplayValue()) {

                //store the all variables in formated form.
                string += variable.getLabel() + ": " + variable.getDisplayValue() + '<br>';
            }


        }
        //set the ritms and variable information in description field of incident
        inc.description+= '<p style="font-size:11pt;"><b>' + 'Requested Item: ' + ritmNum + ' - ' + itemName + '</b>' + '<br><br>';
		inc.description+= string + '<br><br><br></p>';
    }