How to get column's data type (not internal_type) from gliderecord object?

Nisar2
Mega Guru

Hi experts,

I've a script like below:

var gr = new GlideRecord('incident');
gr.setLimit(10);
gr.orderByDesc('sys_created_on');
gr.query();
var columns = ['state', 'assingment_group', 'number'];
while(gr.next())
{
   var value = "":
   for(var i =0; i < columns.length; i++)
   {
       var type = gr.getElement(columns[i]).getED().getInternalType();
       if(type == 'string')
       {
           value = gr[columns[i]];
       }
       else if(type == 'reference')
       {
           value = gr[columns[i]].getDisplayValue();
       }
       else if(type == 'integer')
       {
           // check here if it contains choice list
           // if yes, get it's display value
       }
   }
}

In case of assignment_group, the type is fine. But in case of state, how would I know I determine if it's a choice list field?


UPDATE: Updated the code with comments to explain the use case.

1 ACCEPTED SOLUTION

try now. 

 

Updated Code:

 


	var gr = new GlideRecord('incident');
	gr.setLimit(10);
	gr.orderByDesc('sys_created_on');
	gr.query();
	var columns = ['state', 'assingment_group', 'number'];
	while(gr.next())
	{
		var value = "";
		for(var i =0; i < columns.length; i++)
		{
			var type = gr.getElement(columns[i]).getED().getInternalType();
			if(type == 'string')
			{
				value = gr[columns[i]];
			}
			else if(type == 'reference')
			{
				value = gr[columns[i]].getDisplayValue();
			}
			else if(type == 'integer')
			{
				var clm = gr[columns[i]].getLabel() ;
				var gr = new GlideRecord('sys_choice');
				gr.addEncodedQuery('nameSTARTSWITHtask^elementSTARTSWITH'+clm);
				gr.query();
				if(gr.getRowCount()>0){
					value = gr[columns[i]].getDisplayValue();
					gs.log('it has choice');
				}
				else{

					gs.log('it has not choice');
					//here you can show the label 

				}
			}
		}
	}

View solution in original post

6 REPLIES 6

hvrdhn88
Giga Patron

state : it's integer type field . this is OOTb field, you can add the choices there. 

your script is giving you correct output. what are you expecting ? 

Yes, that's right. It's coming with the correct type (as I already mentioned in the code as comment). However, its value is coming back as 2 (which is also expected).

However, what I would want to do is determine if a field has choices associated to it and if yes, get the label of that selected value, which in this case would be "In Progress"

 

I know that I can do

gr.state.getDisplayValue();

but I was hoping to do it dynamically, something like this

if(type == 'integer')
{
   // check if it has choice list options defined for it
   // and then get the display value of that selected choice
}

Hi Nisar,

The state type is Integer in the dictionary level out of the box; there you can add choice values

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

something like below sample code , you can use to validate the choice. 

 

 

if(type == 'integer')
{
   

var gr = new GlideRecord('sys_choice');
gr.addEncodedQuery('nameSTARTSWITHtask^elementSTARTSWITHstate');
gr.query();
if(gr.getRowCount()>0){
gs.log('it has choice');
}
else{

gs.log('it has not choice');
//here you can show the label 

}

}