- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2019 01:29 AM
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.
Solved! Go to Solution.
- Labels:
-
Best Practices
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2019 01:59 AM
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
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2019 01:33 AM
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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2019 01:38 AM
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
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2019 01:45 AM
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
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2019 01:46 AM
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
}
}
