How can I extract Field values?

Udbhav
Tera Contributor

How can I extract drop down field values and show them in an alert box?

Udbhav_0-1699857426857.png

Like here in the picture, how can I extract all these State field values and also in case of Reference field?

7 REPLIES 7

Sagar Pagar
Tera Patron

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

The world works with ServiceNow

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:

Screenshot 2023-11-13 at 7.34.31 PM.png

 

Screenshot 2023-11-13 at 7.34.42 PM.png

 

If my response helps you resolve your issue. Kindly mark it as helpful & correct. It will be helpful to future readers! 👍🏻
Thanks,
Sagar Pagar

The world works with ServiceNow

Ankur Bawiskar
Tera Patron
Tera Patron

@Udbhav 

Can you explain your business requirement here?

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