Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Dynamically populate dropdown selection in Portal widget

mballinger
Mega Guru

Hello,

I have a portal widget, and in my portal widget, I have a dropdown field that needs to dynamically display choices from a field in ServiceNow. How can I do this? 

For Example: On the Incident form, I would like to display the choices in the state field in a dropdown through my widget on the portal. 

My portal experience is new so anything helps. 

Thanks!

1 ACCEPTED SOLUTION

Michael Jones -
Giga Sage

Here is a very simplified example that you can examine: 

HTML

<div>
${States}
<sn-choice-list field="snChoice" sn-model="c.state" sn-options="data.states" sn-value-field="value" sn-text-field="label" sn-items="data.states"></sn-choice-list>
</div>

 

Server Script: 

(function() {

	//create a place in the data object to hold your values
    data.states = [];
	
	//query your values
    var inc_states = new GlideRecord('sys_choice');
    inc_states.addEncodedQuery('element=state^nameSTARTSWITHincident');
    inc_states.query();
	
    while (inc_states.next()) {
		//push your values to the data object
        var stateObj = {};
        stateObj.label = inc_states.label.toString();
        stateObj.value = inc_states.value.toString();
        data.states.push(stateObj);

    }

})();

 

That should be all you need 🙂

I hope this helps!

If this was helpful or correct, please be kind and remember to click appropriately!

Michael Jones - Proud member of the CloudPires team

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

View solution in original post

2 REPLIES 2

Michael Jones -
Giga Sage

Here is a very simplified example that you can examine: 

HTML

<div>
${States}
<sn-choice-list field="snChoice" sn-model="c.state" sn-options="data.states" sn-value-field="value" sn-text-field="label" sn-items="data.states"></sn-choice-list>
</div>

 

Server Script: 

(function() {

	//create a place in the data object to hold your values
    data.states = [];
	
	//query your values
    var inc_states = new GlideRecord('sys_choice');
    inc_states.addEncodedQuery('element=state^nameSTARTSWITHincident');
    inc_states.query();
	
    while (inc_states.next()) {
		//push your values to the data object
        var stateObj = {};
        stateObj.label = inc_states.label.toString();
        stateObj.value = inc_states.value.toString();
        data.states.push(stateObj);

    }

})();

 

That should be all you need 🙂

I hope this helps!

If this was helpful or correct, please be kind and remember to click appropriately!

Michael Jones - Proud member of the CloudPires team

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

This worked. Thanks!