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

Abdul Azeez
Mega Guru

Hi Nitya,

 

variable_pool gets you the object of database names, i would suggest you to use GlideappVariablePoolQuestionSet() and getFlatQuestions().

 

 

var string='';
var item = new GlideRecord('sc_req_item');
item.get('c9c60ba92f765010209857892799b65e');
item.query();
if(item.next()){
          var glidePool = new GlideappVariablePoolQuestionSet();
          glidePool.setRequestID(item.sys_id);// requested item sys_id
          glidePool.load();
          var getQuestion = glidePool.getFlatQuestions();
          for (var i=0; i < getQuestion.size(); i++) {
              if(getQuestion.get(i).getLabel() != '' && JSUtil.notNil(getQuestion.get(i).getDisplayValue()) ) {
                          string+= getQuestion.get(i).getLabel() + ": " + getQuestion.get(i).getDisplayValue() + "\n";

              }
}

gs.info(string);

}

 

Result:

*** Script: AnswerOne: test
AnswerTwo: 12345


Please hit helpful / Mark it as Correct based on the impact
Abdul Azeez

Mark Roethof
Tera Patron
Tera Patron

Hi there,

If still searching for a summary of all your variables, this article which I wrote a while ago might help. Contains a full script that generates a summary of all variables (in English).
Generate Catalog Item Variables summary

Copy/paste:

	var catitemSysId = current.getValue('request_item');

	var grRITM = new GlideRecord('sc_req_item');
	grRITM.get(catitemSysId);

	var summaryStr = '';
	for(var key in grRITM.variables) {
		summaryStr += grRITM.variables[key].getLabel() + ": " + grRITM.variables[key].getDisplayValue() + "\n";
	}

	gs.info(summaryStr);

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

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

Hi Ankur,

 

This solution was perfect for us, and we applied this business rule to copy variables into a field in a Record Producer record, and it worked perfectly.

 

Question:  How would one apply this to a fix script for existing RP records?

 

Many thanks!