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.

How to variable name and value in script include

shaik_irfan
Tera Guru

Hi,

 

in the script include i am trying to get the variable details

 

When below code i am able to get the variables values but not the name

 

var user = new GlideRecord('sc_req_item');
		user.addQuery('sys_id', empSysID);
		user.query();
		if(user.next())
		{	
			
			obj.add = user.variables.software_requirements.getValue();
			obj.value = user.variables.value.getValue();
		
			arr.push(obj);
		}

gs.eventQueue('notify.variables', current, arr); // I am trying to send the values to send as email
1 ACCEPTED SOLUTION

@shaik.irfan 

you can use this script to get the question name

If you are having 2 variables

1) requested_user - value is Abel Tuter

2) my_choice - value is Adobe Photoshop

Output would be something like this

[{"name":"requested_user","value":"Abel Tuter"},{"name":"my_choice","value":"Adobe Photoshop"}]

var arr = [];

var gr = new GlideRecord('sc_req_item');
if (gr.get(empSysID)) {
    var variables = gr.variables.getElements();
    for (var i=0;i<variables.length;i++) {
        var question = variables[i].getQuestion();

var obj = {};

obj["name"] = question.getName().toString();
obj["value"] = question.getDisplayValue();
arr.push(obj);
    }
}    

Regards
Ankur

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

View solution in original post

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

@shaik.irfan 

please send the json string

obj.add = user.variables.software_requirements.getDisplayValue();

obj.value = user.variables.value.getDisplayValue();

gs.eventQueue('notify.variables', current, JSON.stringify(arr)); // I am trying to send the values to send as email

then in email script you can access it like this and parse it

var jsonString = event.parm1;

var parser = JSON.parse(jsonString);

gs.info('add' + parser.add);

Regards
Ankur

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

sorry i might have confused in explaining.

 

I am able to get the variable values but i also need a question name

 

Ex: I need Additional software requirments also

 

find_real_file.png

you can use below script to get the name of the variable. 

 

var set = new GlideappVariablePoolQuestionSet();
			set.setRequestID(current.sys_id);
			set.load();
			var vs = set.getFlatQuestions();
			for (var i = 0; i < vs.size(); i++) {
				if (vs.get(i).getDisplayValue() != '' && +vs.get(i).getLabel() != '') {
					
					gs.log("\n" + '<b>' +vs.get(i).getLabel() + " = " + '</b>' + vs.get(i).getDisplayValue() + "\n");
				}
			}

 

 

Reference: 

https://jace.pro/post/2017-09-30-undocumented-scripts/

if you have stored ritm sys_id in empSysID then replace current.sys_id to empSysID