nathanfirth
Tera Guru

One of the very powerful directives available in Service Portal that we will be covering today is the snRecordPicker. This directive generates a field very similar to a reference field in the platform. This is very useful when creating custom widgets that will be interacting with tables and records in ServiceNow.

The Directive:

<sn-record-picker field="location" table="'cmn_location'" display-field="'name'" value-field="'sys_id'" search-fields="'name'" page-size="100" ></sn-record-picker>


It supports the following properties:

PropertyDescription
fielda JavaScript object consisting of "displayValue", "value" and "name"
tablethe table to pull records from
default-querythe query to apply to the table
display-field (or display-fields)the display field
value-fieldthe value field (usually sys_id)
search-fieldsthe fields to search
page-sizenumber of records to display in dropdown


To use the snRecordPicker you will also need to create the "field" object in your controller as well as listen for the "field.change" event.

The Controller:

$scope.location = {

      displayValue: c.data.loc.name,

      value: c.data.loc.sys_id,

      name: 'location'

};

$scope.$on("field.change", function(evt, parms) {

      if (parms.field.name == 'location')

              c.data.setLocation = parms.newValue;

     

      c.server.update().then(function(response) {        

              spUtil.update($scope);

      })

});

The Widget:

snrecordpicker.jpg

I've created a sample address picker widget that allows the user to select a location, and then retrieves the record from the server and populates several other fields with the information. The widget is available for download on Share: https://share.servicenow.com/app.do#/detailV2/86b23f151370e2001d2abbf18144b0aa/overview

58 Comments
saptashwa_chakr
Kilo Expert

Can anyone please tell me how to add reference qualifier for a <sn-record-picker>?


Let us assume I have two <sn-record-picker>. Now one is for Assignment groups and another is for Assigned To. Now I want to filter the "Assigned to" reference field will be filtered from assignment group i.e. when ever we click on that "assigned to" record picker, it will only show the related users who are present in the particular selected group.



Need Help. Please let me know your valuable feedback.


viniciusoliveir
Kilo Expert

Try using the default-query parameter in the sn-record-picker tag.



This post might help you:



Re: HoTwo snRecordPicker's depend on another


johnsang
ServiceNow Employee

Hi all,



    I am using sn-reocrd-picker and it works well.....except under Microsoft IE or Edge.


    Under these browsers, once the user input a character, the search result is displayed....but the cursor is disappeared.   User need to re-click on the data item to continue input next character....and the situation repeat again.   The situation is really annoying.


    Please advise if you have similar situation and if there workaround.


    Thanks.


mark_m
Mega Contributor

Is there an option to multi-select with the sn-record-picker (like the form widget display of the list collector type) ?   If not, is there another directive that can be used to display a multi-select drop down ?


mark_m
Mega Contributor

Solved   by adding multipe="true" atrtibute.



Example:             <sn-record-picker class="firstInput" placeholder="--Select--"     id="software1" field="software_list" table="'sc_cat_item'" multiple="true" display-field="'name'" value-field="'sys_id'" search-fields="'name'" page-size="100" default-query="'category=79e83231f15db400fe86b9f6d3e1e21b^u_available_for_new_hires=true'" ></sn-record-picker>


xiaix
Tera Guru

Is there a "mandatory" property for this?

Inactive_Us2231
Giga Expert

Hi Nathan,

This is a great article and i keep looking at serviceportal.io time to time to learn. 

Related to sn record picker, is there a way to also show the reference field icon, which pop out the record, just like we have it for reference variables on portal?

Not applicable
 

How we can populate the value in location on base of user profile.

<div class="col-md-6"> 
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">User</h4>
</div>
<div class="panel-body">
<sn-record-picker field="user" table="'sys_user'" 
display-field="'user_name'" value-field="'sys_id'" 
search-fields="'user_name'" page-size="10" ></sn-record-picker>
</div> 
</div> 
</div>
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">Location</h4>
</div>
<div class="panel-body">
<sn-record-picker field="location" table="'cmn_location'" 
display-field="'name'" value-field="'sys_id'" 
search-fields="'name'" page-size="10" ></sn-record-picker>
</div> 
</div> 
</div> 
</div>

DineshKumaR6579
Kilo Sage

Hello  nathanfirth

 

I tried to started work on this,when i am login with my HI Account it getting one xml file,in this XMl File you have written HTML And Cliient script also,But When i am trying to use that code i am not getting output,please helpme out ,i will share my Screen Shorts nathanfirth.

 

HTML////

<script><![CDATA[
var numberSysId = "number";
if (input.setNumber)
numberSysId = input.setNumber;
else
numberSysId = gs.getUser().getRecord().getValue('number');

var numberGR = GlideRecord('x_grs_ceff_sample_gsi_feedback_form_headers');

numberGR.get(numberSysId);

data.number = {};
$sp.getRecordDisplayValues(data.number, numberGR,'sys_id,reveiw_period_from,reveiw_period_to,evaluator_firstname,evaluator_lastname');


]]></script>

 

<nav class="navbar navbar-light bg-light">
<div class="row">
<div>
<i class='material-icons'>granite</i>
</div>
<div class="col-sm-3">
<a class="navbar-brand">Granite</a>
</div>
<div class="col-sm-6">
<h2>Crew/Employee Feedback Form</h2>
<p>
This Form is to be Completed By an Internal Granite Evaluator at Project Completion
</p>
</div>
<div class="col-sm-3">
<p>
201 North Franklin St.
</p><p>
Suite 1000
</p><p>
Tampa, FL 33602,USA
</p><p>
Phone:+1(813)242-7400
</p><p>
Fax:+1(813)242-2288
</p>
<p>
Email:CEFF@gsinc.com
</p>
</div>
</div>
<div class="panel-body" >
<div ng-if="data.sys_id != '-1' && data.status == 'success'" class="alert alert-success" role="alert"><strong>Success! </strong>{{data.message}}</div>
<div>
<sp-model form_model="data.f" mandatory="mandatory"></sp-model>
</div>
</nav>
<form name="myForm" ng-submit="submitForm()" class="form-horizontal" >
<!-- <form name="myForm" class="form-horizontal" novalidate> -->

<!-- <p> {{myForm.$valid}}</p> Check for form validity -->
<fieldset ng-disabled="c.disableSubmit()">
<div class="row">
<form>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">Number</h4> </div>
<div class="panel-body">
<sn-record-picker field="number" table="'x_grs_ceff_sample_gsi_feedback_form_headers'"
display-field="'number'" value-field="'sys_id'" search-fields="'number'" page-size="100" ></sn-record-picker>
<form class="m-t form-horizontal" role="form">
<div class="form-group">
<label class="col-sm-2 control-label">Review Period</label>
<div class="col-sm-10">
<input type="text" placeholder="{{data.number.reveiw_period_from}}" class="form-control" readonly>
</div>
</div>
<div class="form-group"><label class="col-sm-2 control-label">To</label>
<div class="col-sm-10"><input type="text" placeholder="{{data.number.reveiw_period_to}}" class="form-control" readonly>
</div>
</div>
<div class="form-group"><label class="col-sm-2 control-label">Evaluator Firstname</label>
<div class="col-sm-4"><input type="text" placeholder="{{data.number.evaluator_firstname}}" class="form-control" readonly>

</div>
<div class="form-group"><label class="col-sm-2 control-label">Evaluator Lastname</label>
<div class="col-sm-4"> <input type="text" placeholder="{{data.number.evaluator_lastname}}" class="form-control" readonly>
</div>
</div>
</form>
</div>
</div>

 

client script :

 

function($scope, spUtil) {
var c = this;
$scope.number = { displayValue: c.data.number.number,
value: c.data.number.sys_id,
name: 'number' };
$scope.$on("field.change",
function(evt, parms) { if (parms.field.name == 'number')
c.data.setNumber = parms.newValue;
c.server.update().then(function(response)
{ spUtil.update($scope); }) }); }
//dinesh

 

 

before  adding script code screenshot capture2

after adding code reaction screenshot capture1

 

please help me out nathanfirth

 

 
saxtonjoshu
Kilo Sage

Why not?