Script help - How to get the value for variable field from the 'current' record

harinalamalapu
Giga Expert

How to get the value of a field on a current record using the 'current' keyword if the field name is a variable? Ex: current.u_description returns the description of the current record, say from a Change Request. but what if 'u_description' is a variable?

var result = current.u_description / This produced the result as "Sample Change Request"

Versus

  var fieldName = u_description;

  var value = current.fieldName     //tried this approach #1 - This produced the result as 'undefined'

  var value = current+'.'+fieldName   //tried this approach #2 - This produced the result as '[object GlideRecord].u_description'

I will have to use the above piece of code from a business rule on change request, so current refers to the current change record.

1 ACCEPTED SOLUTION

ghsrikanth
Tera Guru

  var value = current.fieldName     //tried this approach #1 - This produced the result as 'undefined'


The system is searching for the column with the name fieldName, so it is not preferred to use parameter for the variable. If you want to use parameter its better to use getValue function.



var fieldName = 'u_description';


var value = current.getValue(fieldName); //this will work




Mark if it is helpful or correct


View solution in original post

17 REPLIES 17

Have you tried current.variables.<variable_name> ?



Scriptable Service Catalog Variables - ServiceNow Wiki



Thanks,



Tadz


harinalamalapu
Giga Expert

Yes I have tried that but id didn't work. It produced a result of 'undefined'. Looks like the current.variables.<variable_name> format is only used for Service Catalog variables.


Maybe you have to used GlideRecord in that.



Thanks,



Tadz


harinalamalapu
Giga Expert

Ok. Can you please elaborate on that with an example ?



Thanks,


Hari


ghsrikanth
Tera Guru

  var value = current.fieldName     //tried this approach #1 - This produced the result as 'undefined'


The system is searching for the column with the name fieldName, so it is not preferred to use parameter for the variable. If you want to use parameter its better to use getValue function.



var fieldName = 'u_description';


var value = current.getValue(fieldName); //this will work




Mark if it is helpful or correct