Populate User Mobile number and location OnChange and Onload on catalog Item

SNOW46
Tera Contributor

Hi Everyone,

I have configured one Catalog Item in which I have added some varaibles as below.

1.Mandatory - preloaded with saved dataRequestor - Reference field to User table and I have added the default value as javascript:gs.getUserName() and it is populating onload of the form

2.Phone Number - Single Line Text field and given the default value as javascript:gs.getUser().getRecord().getValue('mobile_phone'). 

3.Site Location - Location field reference to Location Table. Default value is javascript:gs.getUser().getRecord().getValue('location').

4.Requester on Behalf of - Reference field to User table if user wants to request on behalf of any other user.

 

But I have some doubts when the form loads, the Location as well as SIte Location is not populating based on the logged in user.

And when I change the Requester name as well as Request on behalf of name and put some other user's name then the phone number field is not getting clear.

Can anyone suggest me on the same how to achieve this requirement. Is there anything do I need to configure any Catalog Client Scripts to perform the task.

 

Thanks,

SNOW@Das

13 REPLIES 13

Hi Das,

Sample script below:

function onChange(){

var requester = g_form.getReference('<requesterField>', popInfo);

}

function popInfo(requester){

g_form.setValue('locationField', requester.location); // use proper field for location here

g_form.setValue('mobileField', requester.mobile_phone); // use proper field for mobile here

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Das,

Any update on this?
Can you mark my answer as correct, helpful if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps users to learn from your thread. Thanks in advance.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ian Mildon
Tera Guru

The problem with using scripts such as:

javascript:gs.getUser().getRecord().getValue('mobile_phone')

is that they are "per session" so it is entirely possible to get values for someone other than the currently logged in user; as you have experienced.

You would be better served by making the fields "user" fields that feed directly from their profile, which gives the added benefit of allowing real-time updating of the field values between the profile and record

Hi,

Can you please brief me in details so that I can get to know like how to implement the same.