How can I get the value of a sn-choice-list on a service portal?

Michael Lovell
Tera Guru

I have a <sn-choice-list> field on a service portal widget where the field is defined as follows:

html:

<sn-choice-list
field="c.mychoice"
sn-model="c.mychoice"
sn-options="c.mychoicelistoption"
sn-value-field="myChoiceValue"
sn-text-field="myChoiceDisplay"
sn-items="c.mychoiceoptions"
sn-on-change="c.valueSelected(selectedValue)">
</sn-choice-list>

 

client script:

c.mychoiceoptions = [{
myChoiceDisplay: "Personal",
myChoiceValue: 'personal'
},
{
myChoiceDisplay: "Work Related",
myChoiceValue: 'work-related'
}];
c.mychoicelistoption = {
hideSearch: false
};
c.mychoice = "personal";

 

if I call

c.valueSelected = function(selectedValue){ alert(selectedValue); }

in the client script, then "personal" or "work-related" are returned as I would expect, but I have another <sn-choice-list> field defined as follows so I can get the options from sys_choice table in the server script:

html:

<sn-choice-list
field="c.wbsDefault"
sn-model="c.wbsDefault"
sn-options="c.wbsListOption"
sn-value-field="value"
sn-text-field="label"
sn-items="c.wbs"
sn-on-change="c.wbsSelected(wbsValue)">
</sn-choice-list>

client script:

c.wbs = [];
for(i = 0; i < c.data.wbsLabels.length; ++i){
c.wbs.push({
label: c.data.wbsLabels[i].toString(),
value: c.data.assetsValues[i]
});
}
c.wbsListOption = {
hideSearch: false
};
c.wbsDefault = "00000";

server script:

data.wbsLabels = [];
data.wbsValues = [];
var wbsGR = new GlideRecord("sys_choice");
wbsGR.addQuery('element', 'u_wbs');
wbsGR.query();
while(wbsGR.next()){
data.wbsLabels.push(wbsGR.getDisplayValue());
data.wbsValues.push(wbsGR.getValue());
}

 

if I call

c.wbsSelected = function(wbsValue){alert(wbsValue);}

in the client script, then 'undefined' is returned when I select a value for that field.  How can I get the value of this field?

 

Thank you

1 ACCEPTED SOLUTION

DrewW
Mega Sage
Mega Sage

Based on the code you just posted that the sn-model='<WHATEVER>' will put the data into the <WHATEVER>.

 

 

View solution in original post

7 REPLIES 7

Why would the server script run when you did not call a server update and I'm going to assume you did not include all of the server code that would actually update the record.

 

Below script i have modified. Can you please check?

 

CoworkerConcern is the variable on portal form . u_action is the field under incident table.

Body HTML template:

<div class="panel-body">
<label class="col-sm-2 control-label">CoworkerConcern</label>
<div class="col-sm-10">
<sn-choice-list
field="c.myCoworkerConcern"
sn-model="c.myCoworkerConcern"
sn-options="c.myCoworkerConcernlistoption"
sn-value-field="myChoiceValue"
sn-text-field="myChoiceDisplay"
sn-items="c.myCoworkerConcernoptions"
sn-on-change="c.valueSelected(selectedValue)">
</sn-choice-list>
</div>
</div>

 

Server script 

if (input && input.update_incident) {

var actioncoworker = new GlideRecord('incident');
actioncoworker.addQuery("u_action", actioncoworker);
actioncoworker.query();
if(actioncoworker.next()){
actioncoworker.incident = input.CoworkerConcern;
actioncoworker.update();
}

 

Client script 

c.CoworkerConcern.get({
            'selected': c.valueSelected,
            'update_incident': true
        }).then(function (response) {
            if (response) {
                //some code to treat response
            }
        });

Looks ok but I do not know the detail of what you are doing so all I can say is if it works go with it.

You should open a new question of your own if you have further questions on your code.