Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need help with script to pass value from <sn-record-picker> field on custom widget HTML to the server script to be used in a GlideRecord

patricklatella
Mega Sage

hi all,

I'm building a widget to act as a very simple custom record producer.  My form only has 2 fields on it, one of which is a reference field to the [sys_user] table.  The form creates a new record on the [incident] table with a custom "u_affected_user" field which I want to populate with the reference field from my custom form.  But I cannot figure out how to get the value of the reference field to the server script to use in the GlideRecord that is creating the new record.  When I click "Submit" button it creates the record, and the "short_description" field populates fine, but the anyone see what I'm missing?

Here are the scripts:

HTML

<div class="panel panel-default">
<div class="panel-heading">
<div class="form-group">

<!-- Affected user : Reference field: THIS ONE IS NOT PASSING THE VALUE THE SERVER SCRIPT-->
<div class="form-group">
<label>Search for your name:</label>
<sn-record-picker field="c.affected_user" table="'sys_user'" display-field="'name'" value-field="'sys_id'" search-fields="'name'" page-size="100" ></sn-record-picker>
</div>

<div class="form-group">
<label>Description</label>
<input class="form-control" ng-model="c.data.short_description">
</div>

<!--The Submit button-->
<div class="form-group">
<input class="btn btn-primary btn-block" ng-click="c.addItem()" type="submit" value="Submit">
</div>
</div>
</div>

CLIENT SCRIPT

function($scope, spUtil) {

var c = this;
c.addItem = function(){

c.server.update().then(function(response){
spUtil.update($scope);
c.data = {};
$scope.c.affected_user.value = '';
}) }
}

SERVER SCRIPT

(function() {
if(!input)
return;

input.requested_by = gs.userID();//THIS IS TO SET THE [caller_id] FIELD TO THE LOGGED IN USER

var gr = new GlideRecord('incident');
gr.initialize();
gr.short_description = input.short_description;
gr.caller_id = input.requested_by;
gr.u_affected_user = input.affected_user;//THIS IS WHAT IS NOT WORKING
gr.insert()

})();

 

1 ACCEPTED SOLUTION

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,


My HTML:

<sn-record-picker field="data.user"
table="'sys_user'"
display-field="'name'"
value-field="'sys_id'" search-fields="'user_name,name'"
page-size="100">
</sn-record-picker>

 

Client COntroller:

function($scope,spUtil,$rootScope) {
/* widget controller */
var c = this;
c.data.user = {value:'',displayValue:''};

}

 

Server:

We can access as below:

 

input.user.value

 

Thanks,
Ashutosh Munot

View solution in original post

14 REPLIES 14

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,


My HTML:

<sn-record-picker field="data.user"
table="'sys_user'"
display-field="'name'"
value-field="'sys_id'" search-fields="'user_name,name'"
page-size="100">
</sn-record-picker>

 

Client COntroller:

function($scope,spUtil,$rootScope) {
/* widget controller */
var c = this;
c.data.user = {value:'',displayValue:''};

}

 

Server:

We can access as below:

 

input.user.value

 

Thanks,
Ashutosh Munot

perfect, thanks Ashutosh!  

Hi Ashutosh,

  Could you please suggest me to update the <sn-record-picker> field value in server script.I have a field in widget when the value changed i need to show one section and need to populate the data from RITM variables to that section fields.

 

In Below screen shot,when i selected work location as wfh,I am showing the address fields and populated the values from RITM. But state is reference field it's not getting populated.

 

Thanks,

Uma.

Hi @Ashutosh Munot1 ,

 

I really need your help!

I have the following code in my widget:

<span class="watchlist">
<h4 class="light field field-label">${Watchlist}</h4>
<div class="text-muted light field">
<div class="picker">
<sn-record-picker field="username" table="'sys_user'" display-field="'name'" value-field="'sys_id'" search-fields="'name'" page-size="10" multiple="true" default-query="'active=true^user_nameISNOTEMPTY'" ></sn-record-picker>
</div>

</div></span>

 

I have a field in the back-end of my HR Case with the following specifications:

rafas_2703_1-1710856812002.png

 

The question is: How can I auto-populate the back-end with the information that is in my Widget variable when I submit the case?

 

Looking forward for some help!

Kind regards,

Sérgio