ServiceNow portal sn-record-picker - what is loc

litchick10
Tera Guru

Super basic question but I need some help

I'm trying to add a sn-record-picker to a portal widget.  I was reviewing the sn-record-picker documentation and I'm hoping someone can help me out when adding a record picker you are advised to add the code below to the client controller.  what is the loc? is it part of spUtils or is it a variable that was declared but not specified in the instructions?

$scope.location = {
 displayValue: c.data.loc.name,
 value: c.data.loc.sys_id,//what is loc?
 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);
 })
});

what I really want to do is just set the value to the chosen sys id from the sys_user table

1 ACCEPTED SOLUTION

Josh Virelli
Tera Guru

Oh god, the above reply got horribly formatted lol. So if all you're trying to do is create a Record Picker that looks at the Sys User table, and stores the value, then I think it should be as simple as this:

HTML

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

CLIENT SCRIPT

$scope.user = {
 displayValue: c.data.user.name,
 value: c.data.user.sys_id,
 name: 'user'
};
 
$scope.$on("field.change", function(evt, parms) {
 if (parms.field.name == 'user'){
 c.data.setUser = parms.newValue;
}
 
//Only do an update here if you need something to happen immediately on the Server side
//Otherwise whenever you do the next c.server.update(), you'll have input.setUser available to you
//It would make sense to wait if you are building a form you want to submit at the end for example.
 c.server.update().then(function(response) {
 spUtil.update($scope);
 })
});

SERVER SCRIPT

data.user = {
'name': '',
'sys_id':''
}

/* If you want it to default to the logged in user, you could do this:
data.user = {
'name', gs.getUserDisplayName(),
'sys_id', gs.getUserID()
}
*/

 

Hopefully that makes more sense! 😄 Let me know if you still have questions.

Please mark my answer as helpful or correct if this worked for you!

Thanks,

Josh

View solution in original post

7 REPLIES 7

litchick10
Tera Guru

Thank you!!

Rishabh Jha
Mega Guru

Hi @litchick10 ,

data variables are the server side variables used to store the values from any queried table, or whatever else you want to hold, and access on the client side or on the HTML markup using expressions.

So in your example above, data.loc would be some location field's value queried from the servicenow table.

If you want a record picker to be able to pick users, here's what you can use:

HTML:

<sn-record-picker field="user" table="'sys_user'" display-field="'name'" value-field="'sys_id'" default-query="'active=true'" multiple="false" search-fields="'name'"></sn-record-picker>

<button type="submit" class="btn btn-primary" ng-click="saveUser()" id="btnSubmit">${Submit}</button>

Client Controller:

$scope.user = {
displayValue: $scope.data.user_dv , //This should not be null
value: $scope.data.user, //This should not be null
name: 'user'
}

$scope.$on("field.change", function(evt, parms) {
if (parms && parms.field) {
if(parms.field.name == 'user') {
$scope.data.user = parms.newValue;
$scope.data.user_dv = parms.displayValue;
}
}
});


$scope.saveUser= function() {

c.data.action = "saveUser";


c.data.selected_user_id = $scope.data.user;

c.data.selected_user_name = $scope.data.user_dv;


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

c.data.action = '';

c.data.selected_user_id = '';

c.data.selected_user_name = '';

}

 

Server Script:

data.user = '';

data.user_dv = '';

if (input && input.action == "saveUser") {

//here you cad use the selected user id & name from the input variables

// and save or process your business logic

gs.info('Selected user id: '+input.selected_user_id );

gs.info('Selected user name: '+input.selected_user_name );

}

 

Thanks & Regards,

Rishabh Jha

Aavenir (https://www.aavenir.com/)

Alex94
Tera Contributor

Litchik10,

Thank you for posting valuable articles on modifying new Idea Portal.

I used your information to add Submitter field to the form. The problem that I'm running into, is when I edit the Idea that was created and try to change the Submitter field to somebody else from the user list, the record does not update to the changed value after it is saved (the submitter stays the same).

I noticed that there is a different widget (IM Idea Detailed View) that is wrapping the form. I guess the $scope.$on function in Idea Create Widget client is not executing and binding the changed value to the field.

Does anybody have any suggestions on how to resolve this issue?

Thank you,

Alex B.