Help with copying reference variables from a record producer to work notes

Brendan Hallida
Kilo Guru

Hi all,

I have a record producer that I want to copy some variables over to the work notes.

I found the thread below which shows how and have it working with plain text, however, I am having issues with reference fields.

https://community.servicenow.com/community?id=community_question&sys_id=77ae0761dbdcdbc01dcaf3231f96...

When I add a reference field as a variable, the script does not run and I don't even get the Variables: in the work notes.  Here is the script:

var wn = 'Variables:';
for (var key in producer)
	if (producer[key]) {
	if (key.search('boom') != -1) {
		var v = producer[key];
		wn += '\n' + v.getGlideObject().getQuestion().getLabel() + ': ' + v.getDisplayValue();
	}
}
current.work_notes = wn;

Any ideas?

Thanks, Brendan

1 ACCEPTED SOLUTION

Ok. I thought you were using the question Label. Lets try this

 

(function executeRule(current, previous /*null when async*/) {
	
	var desc = '';
	var qtype = 0;
	var q = new GlideRecord('question_answer');
	q.addQuery('table_sys_id',current.sys_id);
	q.orderBy('order');
	q.query();
	
	while (q.next())
		{
		if (q.value!='' && q.question.name.indexOf('boom')>-1 && q.value!='false' || q.question.type==11)
			{
			
			if (q.question.type==11)
				desc = desc+'\n';
			
			
			if (q.question.type!=7)
				{
				if (desc=='')
					desc = q.question.getDisplayValue()+': '+getRefValue(q.question.type,q.question.reference,q.value);
				else
					desc = desc+'\n'+q.question.getDisplayValue()+': '+getRefValue(q.question.type,q.question.reference,q.value);
			}
			else
				{
				if (qtype==7)
					desc = desc+','+q.question.getDisplayValue();
				else
					desc = desc+' '+q.question.getDisplayValue();
			}
			
			qtype = q.question.type;
		}
		
		if (q.question.type==19)
			desc = desc+'\n';
	}
	
	if (desc!='')
		current.description = desc;
	
})(current, previous);

function getRefValue(type,table,value)
{
	if (type==8)
		{
		var ref = new GlideRecord(table);
		ref.get(value);
		
		return ref.getDisplayValue();
	}
	else
		{
		return value;
	}
}

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

18 REPLIES 18

Hi Ankur,

Not yet, havent yet worked out how to fix the script that I have.

Any ideas?

Instead of adding it to record producer, I added the following BR script to my target table which runs on onInsert. I have done few additional formatting, which can be removed if required.

 

(function executeRule(current, previous /*null when async*/) {
	
	var desc = '';
	var qtype = 0;
	var q = new GlideRecord('question_answer');
	q.addQuery('table_sys_id',current.sys_id);
	q.orderBy('order');
	q.query();
	
	while (q.next())
		{
		if (q.value!='' && q.value!='false' || q.question.type==11)
			{
			
			if (q.question.type==11)
				desc = desc+'\n';
			
			
			if (q.question.type!=7)
				{
				if (desc=='')
					desc = q.question.getDisplayValue()+': '+getRefValue(q.question.type,q.question.reference,q.value);
				else
					desc = desc+'\n'+q.question.getDisplayValue()+': '+getRefValue(q.question.type,q.question.reference,q.value);
			}
			else
				{
				if (qtype==7)
					desc = desc+','+q.question.getDisplayValue();
				else
					desc = desc+' '+q.question.getDisplayValue();
			}
			
			qtype = q.question.type;
		}
		
		if (q.question.type==19)
			desc = desc+'\n';
	}
	
	if (desc!='')
		current.description = desc;
	
})(current, previous);

function getRefValue(type,table,value)
{
	if (type==8)
		{
		var ref = new GlideRecord(table);
		ref.get(value);
		
		return ref.getDisplayValue();
	}
	else
		{
		return value;
	}
}

Please mark this response as correct or helpful if it assisted you with your question.

Hi Sanjiv,

 

Cheers for that,  I seem to be getting a fair few entries, but they are all undefined.

I am running this on the sn_sm_marketing_request table, and have altered yours as below:

(function executeRule(current, previous /*null when async*/) {
	
	var desc = '';
	var qtype = 0;
	var q = new GlideRecord('sn_sm_marketing_request');
	q.addQuery('table_sys_id',current.sys_id);
	q.orderBy('order');
	q.query();
	
	while (q.next())
		{
		if (q.value!='' && q.value!='false' || q.question.type==11)
			{
			
			if (q.question.type==11)
				desc = desc+'\n';
			
			
			if (q.question.type!=7)
				{
				if (desc=='')
					desc = q.question.getDisplayValue()+': '+getRefValue(q.question.type,q.question.reference,q.value);
				else
					desc = desc+'\n'+q.question.getDisplayValue()+': '+getRefValue(q.question.type,q.question.reference,q.value);
			}
			else
				{
				if (qtype==7)
					desc = desc+','+q.question.getDisplayValue();
				else
					desc = desc+' '+q.question.getDisplayValue();
			}
			
			qtype = q.question.type;
		}
		
		if (q.question.type==19)
			desc = desc+'\n';
	}
	
	if (desc!='')
		current.description = desc;
	
})(current, previous);

function getRefValue(type,table,value)
{
	if (type==8)
		{
		var ref = new GlideRecord(table);
		ref.get(value);
		
		return ref.getDisplayValue();
	}
	else
		{
		return value;
	}
}

 

find_real_file.png

 

You don't need to change the table name. Just use the script and it should work


Please mark this response as correct or helpful if it assisted you with your question.

oh sweet, that worked well 🙂

Do you think we can filter it to only include variables that contain the word 'boom' (an example, not my naming convention haha)

 

Cheers, Brendan