Copy variable details to RITM description

Nitya2
Kilo Guru

Hello all,

Please correct me the code for variable details copy to RITM description , we are using below script.

workflow run script:

var variablesGR = new GlideRecord('sc_req_item');
variablesGR.get('sys_id', current.sys_id);
current.description = JSON.stringify(new GlideRecordToObject().toObject(variablesGR.variable_pool));

 

Script include :

var GlideRecordToObject = Class.create();
GlideRecordToObject.prototype = {
	initialize: function() {

	},
	/**
 	* @summary toObject function returns a gliderecord as an object. This
	* is used to prevent saving the place in memory. 
	* https://community.servicenow.com/community/develop/blog/2015/09/24/community-code-snippets--gliderecord-to-object-array-conversion
 	* @param {object} recordToPackage - The gliderecord object
 	* @return {object} JSON object that can be used. 
 	*/
	toObject : function(recordToPackage){
		var packageToSend = {}; 
		for (var property in recordToPackage) {
			try {
				if(property == 'work_notes' || property == 'comments'){
					packageToSend[property] = recordToPackage[property].getJournalEntry(1);
				} else {
					packageToSend[property] = recordToPackage[property].getDisplayValue();
				}
			}
			catch(err){}
		}
		return packageToSend;
	},
	
	type: 'GlideRecordToObject'
};

 

 

its working fine but  when i got variables information like below.

{"requested_by":"Self","requested_for":"","business_service_name":"Test","business_owner_name":"Nitya","short_description":"test-short description","description":""}

Its taking backend variable names

But we need like below ( variable , next line another variable)

Requested by  - Self

Requested for -

Business service name - Test

Business Owner - Nitya

Short description -  est-short description

 

please correct me the code.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Nitya,

please try this below script

you can add this code to before insert BR on sc_req_item table

Condition: current.cat_item == 'Your Catalog Item SysId'

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';
}

current.short_description = str;

Regards
Ankur

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

View solution in original post

13 REPLIES 13

Hello @Elizabeth10 ,

You can use below logic to copy variable set(multi row variable set variables) to copy in description.

 

var mar = gr.variables.fill_the_below_information;//multirow variable set name
    var description = "";
 
    mar = JSON.parse(mar);
    for (var j = 0; j < mar.length; j++) {
        var process = mar[j].please_choose_process_DAB;//variable inside the (variable set)
        var emailaddress = mar[j].user_email_address;//variable inside the (variable set)
var dABRequestNumber = mar[j].dab_request_number;//variable inside the (variable set)
        var descriptionRequest = mar[j].description_of_request_dab;//variable inside the (variable set)
        description = description + '\n Please choose process : \t' + process + " \n Email address :\t' " + emailaddress +  "\n DAB request number: \t'" + dABRequestNumber + "\n Description of request : \t'" + descriptionRequest + '\n...............\n';
 
}
 
    current.description = str + description;//you can remove str .It was used for single line variabler set.
    
}

Hi - 
I managed to get it to work with this:

var reqItemSysId = current.sys_id.toString(); // Get current RITM sys_id
var RITM = new GlideRecord('sc_req_item'); // Query the sc_req_item table
if (RITM.get(reqItemSysId)) { // Check if the record is found
    var summaryStr = ''; // Initialize an empty string for the summary
    // Define an array of labels or keys to exclude from the description
    var excludeLabels = [
        'Task Ref - Reserved',
        'static business service',
    ];
    var variables = RITM.variables.getElements(); // Get all variables
    for (var i = 0; i < variables.length; i++) { // Loop through the variables
        var varElement = variables[i];
        var label = varElement.getLabel(); // Get the variable label
        // Check if the label is in the exclude list
        if (excludeLabels.indexOf(label) !== -1) {
            continue; // Skip this variable if it is excluded
        }
        // If label is not excluded append it to the summary string
        summaryStr += label + " " + varElement.getDisplayValue() + "\n";
    }
    current.description = summaryStr; // Set the description field to the filtered string
}

Hello @Elizabeth10 ,

You can use below logic to copy variable set(multi row variable set variables) to copy in description.

 

var mar = gr.variables.fill_the_below_information;//multirow variable set name
    var description = "";
 
    mar = JSON.parse(mar);
    for (var j = 0; j < mar.length; j++) {
        var process = mar[j].please_choose_process_DAB;//variable inside the (variable set)
        var emailaddress = mar[j].user_email_address;//variable inside the (variable set)
var dABRequestNumber = mar[j].dab_request_number;//variable inside the (variable set)
        var descriptionRequest = mar[j].description_of_request_dab;//variable inside the (variable set)
        description = description + '\n Please choose process : \t' + process + " \n Email address :\t' " + emailaddress +  "\n DAB request number: \t'" + dABRequestNumber + "\n Description of request : \t'" + descriptionRequest + '\n...............\n';
 
}
 
    current.description = str + description;//you can remove str .It was used for single line variabler set.
    
}
 
//Kindly mark if helpful..

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Nitya,

Hope you are doing good.

Did you get a chance to check on the solution provided to resolve your query?

If your query is resolved please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

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