The CreatorCon Call for Content is officially open! Get started here.

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

@Wayne Miller 

you can iterate over those records and use the similar logic.

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

OK, so we have a field called 'case variable summary' and this will pull the variables using your business rule.

 

var gd = new GlideRecord('case');
var a = gd.addNullQuery('case_variable_summary');
a.addCondition('active','true');
gd.query();
while(gd.next())
{
    var varSum = current.varSum.getElements();
    var str = '';
    for (var i=0;i<varSum.length;i++) {
    var question = varSum[i].getQuestion();
    var variableLabel = question.getLabel();
    var variableValue = question.getDisplayValue();
    str = str + variableLabel + ' - ' + variableValue + '\n';
    }
current.case_variable_summary = str;}
gd.update();

 

 

Applying the logic in your BR to this the top line returns a NULL value, so that section of the JS doesn't work.  Baring in mind my JS skills are novice at best, I'm hitting a brick wall at the moment.

@Wayne Miller 

for existing records you need to have scheduled job

Could you post a new question and tag me there so that we can continue discussion there as this is an older thread?

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

What if want to copy variables from variable set in RITM description?

Did you find a solution for copying variables from variable set to RITM description? I am trying to do the same, but the scripts above are not working for me.