How to pass the reference value from the HTML to the client controller in the widget
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2023 08:33 AM
Hi,
I want to pass the reference value from the HTML to the client controller in the widget.
Here is my code:
HTML:
<sn-record-picker field="name" table="'sys_user'" id="reference_field" display-field="'name'" display-fields="'name,user_name'" value-field="'sys_id'" search-fields="'name'" page-size="50" ng-change="c.referChange()" ></sn-record-picker>
Client controler:
api.controller=function($location,$scope) {
/* widget controller */
var c = this;
c.referChange=function(){
$location.url('sp?id=portal&table=u_user_table&filter=u_user_delegateLIKE'+(what will be here? how to add the value of reference));
}
};
Please help in this.
Thanks,
Samiksha

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2023 09:26 AM
Hi,
I'd recommend checking out this documentation for assistance: https://www.servicenow.com/community/developer-blog/reference-fields-with-the-snrecordpicker-directi...
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2023 10:37 AM
To pass the value of the reference field to the client controller in ServiceNow, you can use the ng-model directive to bind the value to a variable in the scope of your controller.
Here is an updated HTML code with an added ng-model directive:
<sn-record-picker field="name" table="'sys_user'" id="reference_field" display-field="'name'" display-fields="'name,user_name'" value-field="'sys_id'" search-fields="'name'" page-size="50" ng-model="c.selectedReference" ng-change="c.referChange()"></sn-record-pIn the client controller, you can access the value of the reference field using the $scope object and the variable name that you bound the field to in the HTML (c.selectedReference in this case). Here's an updated referChange function that includes the reference value in the URL:icker>
api.controller = function($location, $scope) {
var c = this;
c.referChange = function() {
$location.url('sp?id=portal&table=u_user_table&filter=u_user_delegateLIKE' + $scope.c.selectedReference);
};
};
With these changes, the referChange function should now receive the value of the reference field when it is changed by the user in the sn-record-picker.
Please mark my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2023 10:50 AM
Hi @Syedmd08 ,
Thank you for reply. But after adding ng-model="c.selectedReference" reference field is coming as text box.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2023 10:54 AM
If you want to keep the default behavior of the sn-record-picker widget, which displays a dropdown list of records to select from, and also pass the selected record's sys_id value to the client controller, you can modify the ng-change directive to pass the selected record as an argument to the referChange function.
Here's an updated HTML code that uses the ng-change directive to pass the selected record to the referChange function:
<sn-record-picker field="name" table="'sys_user'" id="reference_field" display-field="'name'" display-fields="'name,user_name'" value-field="'sys_id'" search-fields="'name'" page-size="50" ng-change="c.referChange(selectedRecord)" ></sn-record-picker>
In the client controller, you can access the sys_id value of the selected record from the selectedRecord argument. Here's an updated referChange function that extracts the sys_id value from the selected record and includes it in the URL:
api.controller = function($location, $scope) {
var c = this;
c.referChange = function(selectedRecord) {
var selectedSysId = selectedRecord.sys_id;
$location.url('sp?id=portal&table=u_user_table&filter=u_user_delegateLIKE' + selectedSysId);
};
};
With these changes, the sn-record-picker widget will still display a dropdown list of records to select from, and the referChange function will receive the selected record's sys_id value as an argument when the user selects a record.