Populate variables from sys user table based on 'Requested for'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2022 08:21 AM
Hi all,
Iam working on a catalog item. The requirement is to autopopulate 6 fields (Fname, Lname, Title, Department, Email,Phone,) based on Requested For field.
In the catalog item variables, I have set the default value for all the 6 variables like below, so when the form loads all these 6 fields are populated. So no issues on that part.
javascript:(gs.getUser().getRecord().getValue("first_name"));
javascript:(gs.getUser().getRecord().getValue("last_name"));
javascript:(gs.getUser().getRecord().getValue("title")); .......etc
If the 'requested_for' value is changed then all these values should be populated based on the user data. I have written an onchange client script but that doesnt seems working. Below is the script. Any guidance would be much appreciated.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gr = g_form.getReference('requested_for', getUser);
}
function getUser(gr){
g_form.setValue('dccr_forename', gr.first_name);
g_form.setValue('dccr_surname', gr.last_name);
g_form.setValue('dccr_work_email', gr.email);
g_form.setValue('dccr_jobtitle', gr.title);
g_form.setValue('dccr_service_dpt', gr.department);
g_form.setValue('dccr_contact_number', gr.phone);
}
Thanks,
Rini
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2022 02:38 AM
Hi Jaspal,
Followed this post https://community.servicenow.com/community?id=community_question&sys_id=196b8b21db9cdbc01dcaf3231f96... by Chuck Tomasi, to create a script inlude and client script. But it doesn't work , and it throws an error 'There is a javascript error in your browser console'.
Script Include:
Client Script:
Thanks,
Rini
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2022 07:06 AM
Its working now, Dont ask me how....I have no idea..I have deleted all the scripts and started fresh and tada It worked!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2022 09:35 AM
Hi,
Your scripts is correct. No issue in that.
May be you can put some alerts and see where it is getting broken.
Regards,
Sumanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-25-2022 04:26 AM
Hi Sumanth,
I was using a variable set, and now I have removed that and created new variables for Requester/COmpany/Requested For and deactivated all the client scripts and script include. Just wrote a basic onload client script and fields are still not populating. Its totally weird and I dunno why? Any thoughts on this?
function onLoad() {
//Type appropriate comment here, and begin script below
var user = g_form.getReference('dccr_requested_for', populateInfo);
function populateInfo(user) {
g_form.setValue('dccr_forename', user.first_name);
g_form.setValue('dccr_surname', user.last_name);
}
}
Thanks,
Rini

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2022 07:06 PM
Your script looks alright, can you confirm if your onCHange script is even working or not?
If it is set for requested for, add some alerts to verify if the CS is triggering also verify the name of fields
Aman Kumar