We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

snChoiceList Directive snOnChange issue

Thomas Wright
Giga Expert

Hi All,

Need some Service Portal widget expertise please. I'm having trouble getting the value out of a sn-choice-list field with an onchange. Here's my HTML:

<sn-choice-list

        sn-on-change="c.valueSelected(attribute)"

        sn-model="attribute.value"

        sn-items="attribute.choices"

        sn-value-field="value"

        sn-text-field="display">

</sn-choice-list>

And my client script:

c.valueSelected = function(attribute) {

        alert(attribute.value);

};

alert(attribute.value) is giving me the oldValue rather than newValue - guessing it's some asynchronous thing. Anyone know how to get the newValue directly on a change?

11 REPLIES 11

nic_2017
Mega Contributor

Thanks, but that link does not explain how to use sn-on-change with a choice list.


<sn-record-picker field="c.manager" table="'sys_user'" display-field="'name'" display-fields="'email'" value-field="'sys_id'" search-fields="'name'" page-size="100" placeholder="Manager" default-query="'name=Stefan Lutz'"></sn-record-picker>



This populates from "sys_user" table the "sn-record-picker". When a record is selected the script populates:


// Manager


  c.manager = {


  displayValue: "",


  value: "",


  name: 'managerfield'


  };



You just have to take the "c.manager" and do at the client a c.server.update() and push the value to the server. Use a server script to save it.



Not sure what else is there.


Thomas Wright
Giga Expert

I am using a workaround at the moment, using setTimeout to look up the value of my field after it has been updated works, not ideal if you need to do something synchronously.



c.valueSelected = function(sys_id) {


        setTimeout(function(){


                  //Look up my attribute object by sys_id


                  var attribute = searchArr(sys_id, c.data.docObj.attributes, "sys_id");


                  //attribute now has correct answer


                  alert(attribute.value);


        }, 10);


};