Onload client scipt does not work on service portal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2022 03:27 PM
I'm using onLoad client script to auto populate user data. It works in Native UI but not on Portal despite checking "UI type" all. Can someone help me with this?
function onLoad (control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}var user = g_form.getReference('requested_by', populateDetails);
function populateDetails(user) {
g_form.setValue('your_position', user.title);
g_form.setValue('department', user.department);
g_form.setValue('division', user.u_division);
g_form.setValue('department', user.department);
g_form.setValue('location_cbf', user.location);
g_form.setValue('building_name', user.u_building);
g_form.setValue('office_phone', user.phone);
}
}
- Labels:
-
Service Portal Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2022 05:18 PM
Can you post a screenshot of the entire Client Script? I put a condensed version into my instance and it works fine. Below is my example.
To make it easier on my form, I am just doing an onChange on the first name field. When it changes, I look up the Account Manager field and put the user_name into the last name field. It's working fine, and it is the same as your form (just using a single setValue).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2022 05:42 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2022 06:22 PM
The code itself is completely fine in my Service Portal site with an onChange.
Have you looked to see what g_form.getValue('requested_by') returns in your onLoad script (even with just an alert)? There is the possibility of a timing issue with Service Portal that is not present in the desktop UI, and an onLoad causes issues with some sort of default value you are passing in (I am assuming that's how you populate the requested_by field).
Because it is looking to a field on the form, you could simply modify your onLoad to be an onChange (onChange scripts run when the form loads, that's why you notice the isLoading on line 2 of every script), and then see if that works. I would recommend trying the onChange next, you should be able to see that working and can then decide if that will meet your needs.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2022 05:24 PM
Hi Thomas,
Try the following:
function onLoad(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
g_form.getReference('requested_by', function(user) {
g_form.setValue('your_position', user.title);
g_form.setValue('department', user.department);
g_form.setValue('division', user.u_division);
g_form.setValue('department', user.department);
g_form.setValue('location_cbf', user.location);
g_form.setValue('building_name', user.u_building);
g_form.setValue('office_phone', user.phone);
});
}