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 ritm variable details to ritm description

busireddy
Tera Contributor

Hi all, 

Could any one help me with script, I want to copy the variables and variable values to ritm description field via background scrip

find_real_file.png

5 REPLIES 5

Jaspal Singh
Mega Patron
Mega Patron

Why background script? Is this for one time activity.

var ritmis=new GlideRecord('sc_req_item');
ritmis.addQuery('number','RITM....');//Replace RITM number correctly
ritmis.query();
gs.print(ritmis.getRowCount());
while(ritmis.next())
{
ritmis.description=ritmis.variables.description1;//replace description1 with variable name for description
ritmis.update();
}

Kartik Sethi
Tera Guru

Hi @busireddy 

 

To accomplish your requirement, you can use the below-provided code in Business Rule:

Name: Copy Variables to Description

When: Before

Insert: True

Condition: Item ► is not empty (or Item ► is ► <your required Catalog item>)

Script:

(function executeRule(current, previous /*null when async*/ ) {
	var desc = current.description;
	var descAppend = '';
        for (var prop in current.variables) {
            if (current.variables.hasOwnProperty(prop)) {
                descAppend += prop + ': ' + current.variables[prop];
            }
        }
	
	desc += '\n\nVariables Data:\n' + descAppend;
})(current, previous);

 


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik

Hi @busireddy 

 

Was I able to provide a solution to your question?

If yes and if my answer helped you out then please mark my answer as correct/helpful, so that other community members facing similar issues might get help.

Thanks and regards,

Kartik

Ankur Bawiskar
Tera Patron
Tera Patron

@busireddy 

so this is for existing RITMs where description field is not having the variable information

You can use this sample script for 1 record and then run it for all

var ritm = new GlideRecord('sc_req_item');
ritm.addEncodedQuery("descriptionISEMPTY");
ritm.setLimit(1); // first test for 1 record and then comment this line
ritm.query();
if(ritm.next()){
	var arr = [];
	var variables = ritm.variables.getElements(); 
	for (var i=0;i<variables.length;i++) { 
		var question = variables[i].getQuestion();
		var label = question.getLabel();
		var value = question.getDisplayValue();
		if(label != '' && value != ''){
			arr.push(label + ' : ' + value + '');
		}
	}
	ritm.description = arr.toString();
	ritm.update();
}

Regards
Ankur

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