Want to populate logged in user name in widget form

Sagar_pawar
Tera Contributor

Hello All,

I want to populate the logged-in user name on the widget form for the requester field. I am new to the service portal please guide me on this thank you.

Thank You.

10 REPLIES 10

-O-
Kilo Patron
Kilo Patron

On server side there is an API that returns the currently logged in user's id:

gs.getUserID()

don't forget to convert it into a string; e.g:

'' + gs.getUserID()

Another API will get you the user's name:

'' + gs.getUser().fullName

 

Use that APIs in the Server script of the widget to add the information to global object data. Than bind those properties to relevant attributes of the directive used for reference field.

This depends on the directive used.

Let us know which is it, so we can say how to bind the default value to it; or look at examples in existing widgets, for how the default value is set.

For example, if you will be using <sn-record-picker />, you can initialize its value by adding properties displayValue and value to the object set as field:

- in Body HTML template:

<sn-record-picker field="loggedInUser"/>

- in Client controller:

$scope.loggedInUser = {
	displayValue: c.data.loggedInUser.displayValue,
	value: c.data.loggedInUser.value
};

- in Server script:

data.loggedInUser = {
	displayValue: '' + gs.getUser().fullName,
	value: '' + gs.getUserID()
};

Sagar_pawar
Tera Contributor

Hello @-O- As you know I am very new to the service portal can you tell me how to bind data?

<input type="text "ng-bind="data.logged-in user">

is the above single-line code correct?If possible please share the script so I can understand better.

Thank You.

 

 

The value of attribute ng-bind must be valid JavaScript (object property).

And data.logged-in user is not valid JavaScript.

You need something like

 

 data.logged_in_user

 

or

 

 data.loggedInUser

 

Sagar_pawar
Tera Contributor

Hello @-O- Your script is not woring please check my script once.

--------------------------------------------------------

HTML Template

<sn-record-picker field="Requester"/>

--------------------------------------------------------

Client Script

$scope.Requester= {
displayValue: c.data.Requester.displayValue,
value: c.data.Requester.value
};

 

--------------------------------

Server Script

data.Requester = {
displayValue: '' + gs.getUser().fullName,
value: '' + gs.getUserID()
};

Please guide.Thank You.