We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to get the field value using script include

Hari1
Mega Sage

Hi,

I need to get the field value of type "Translated Field" i need to pass the sys_id of the question to get the question but i am getting the output as null.

REF::

find_real_file.png

var ResponseValues = Class.create();
ResponseValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	Response:function() 
	{
	gs.log("Inside ResponseValues");
	var getResponse = this.getParameter('sysparm_response'); // value passed from the client script
	gs.log("getResponse Value is: " + getResponse);	
		
	var questions = {};
	var a,b,c,d,e,f;
	var aa,bb,cc;
	var gr1 = new GlideRecord('question'); // table name question
	var questionText = gr1.getValue('question_text');
	if(questionText == '8cdb154adb094810bda4d602ca961984')
		{
			aa = questionText;
		}
	if(questionText == 'f3d6fd460c14240078a05606db9a1b0e')
		{	
			bb = questionText;		
		}
	if(questionText == '940bd50adb094810bda4d602ca9619ca')
		{
			cc = questionText;	
		}
	var gr = new GlideRecord('survey_response');
	gr.query();
	if(gr.next())
		{
			a = gr.setValue('question', aa);
			b = gr.setValue('question', bb);
			c = gr.setValue('question', cc);
			d = gr.setValue('response',getResponse);
			e = gr.setValue('response',getResponse);
			f = gr.setValue('response',getResponse);
			
			gr.initialize();
			
			gr.question = a;
			gr.question = b;
			gr.question = c;
			gr.response = d;
			gr.response = e;
			gr.response = f;
			
			gr.insert();
	
			questions.var1 = a;
			questions.var2 = b;
			questions.var3 = c;
			questions.var4 = d;
			questions.var5 = e;
			questions.var6 = f;
			
			data = JSON.stringify(questions);
			return data;
		}
	
		}
	});
2 REPLIES 2

Naveen20
ServiceNow Employee

gr1.getValue('question_text'is returning the actual string value and not the sys_id, so the script might need correction.

Chuck Tomasi
Tera Patron

The first thing that strikes me is this section:

var gr1 = new GlideRecord('question'); // table name question
var questionText = gr1.getValue('question_text');
	

You are trying to retrieve a value from gr1's question_text field, but you don't have anything in gr1 yet. It's just an instantiated object. Nothing is brought back from the database until you do a query() and next(), which you do later. Your three if blocks have nothing to go by.

Also, hardcoding sys_ids like this is bad practice. It makes the code very brittle. Consider using display values or properties or something to make the code more configurable than hard-coded. Plus, sys_ids are just hard to read. Just glancing at this code I have no idea what they correlate to.

Technical Best Practices