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

Dr Atul G- LNG
Tera Patron
Tera Patron

@Sandeep Rajput  @Viraj Hudlikar  inputs please.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

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 

Ankur Bawiskar
Tera Patron
Tera Patron

@DB1 

you can use this script to get all the variables for RITM, remember to enhance it for MRVS

I assume you are using UI action on RITM for this so current object is for RITM

also inc is the GlideRecord object for incident

var variables = current.variables.getElements();

var str = '';
for (var i=0;i<variables.length;i++) {
var question = variables[i].getQuestion();
var variableLabel = question.getLabel();
var variableValue = question.getDisplayValue();
str = str + variableLabel + ' - ' + variableValue + '\n';
}

inc.short_description = str;

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

I tried but how to use inc object here because I glide it from Script Include and this is what I have on UI Action

 

function convertRecord() {
    var usrResponse = confirm('Are you sure you would like to proceed?');
    if (usrResponse) {

  var variables = current.variables.getElements();
            var str = '';
            for (var i = 0; i < variables.length; i++) {
                var question = variables[i].getQuestion();
                var variableLabel = question.getLabel();
                var variableValue = question.getDisplayValue();
                str = str + variableLabel + ' - ' + variableValue + '\n';
            }

            inc.description = str;

        var ga = new GlideAjax('PH_RITMToINC');
        ga.addParam('sysparm_name', 'convertMyRecord');
        ga.addParam('sysparm_sysId', g_form.getUniqueValue());
        ga.getXMLAnswer(function(answer) {
            var url = "incident.do?sys_id=" + answer;
            g_navigation.open(url, '_blank');
        });
    }
}