How can I extract Field values?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2023 10:43 PM
How can I extract drop down field values and show them in an alert box?
Like here in the picture, how can I extract all these State field values and also in case of Reference field?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2023 11:28 PM
Hi @Udbhav,
You can use the on Change client scripts as -
var stateValue = g_form.getValue('state_field_name');
if(stateValue == '1'){ // New
[then do this...];
} else if(stateValue == '2'){ // In Progress
[then do this...];
} else if(stateValue == '3'){ // On Hold
[then do this...];
} else if(stateValue == '6'){ // Resolved
[then do this...];
} else if(stateValue == '7'){ // Closed
[then do this...];
} else if(stateValue == '8'){ // Canceled
[then do this...];
}
OR
You can use the Switch as well instead of if-else conditions.
If my response helps you resolve your issue. Kindly mark it as helpful & correct. It will be helpful to future readers! 👍🏻
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2023 06:05 AM
Hi @Udbhav,
Try this sample scripts -
Client scripts:
function onLoad() {
//Type appropriate comment here, and begin script below
var ajax = new GlideAjax('global.SagarIncidentUtils');
ajax.addParam('sysparm_name', 'getINCStateValues');
ajax.getXMLAnswer(getINCStateValues);
}
function getINCStateValues(answer) {
if (answer) {
var choiceData = JSON.parse(answer);
var stateLabel = [];
var stateValues = [];
for(var i = 0; i < choiceData.length; i++){
g_form.addInfoMessage("Label: " + choiceData[i].key + " Value: " + choiceData[i].value);
stateLabel.push(choiceData[i].key);
stateValues.push(choiceData[i].value);
}
alert("stateLabel: " + stateLabel + "\n" + "stateValues: " + stateValues);
}
}
Client callable Script Include:
var SagarIncidentUtils = Class.create();
SagarIncidentUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getINCStateValues: function(){
var Choice = [];
var choiceValues = new GlideRecord('sys_choice');
choiceValues.addQuery('name=incident^element=state^inactive=false^ORDERBYsequence');
choiceValues.query();
while(choiceValues.next()){
var JSONObj = {
key: choiceValues.getValue('label'),
value: choiceValues.getValue('value')
};
Choice.push(JSONObj);
}
return JSON.stringify(Choice);
},
type: 'SagarIncidentUtils'
});
Expected results:
If my response helps you resolve your issue. Kindly mark it as helpful & correct. It will be helpful to future readers! 👍🏻
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2023 06:12 AM
Can you explain your business requirement here?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader