Onload client scipt does not work on service portal

Thomas98
Tera Expert

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);
}
}

9 REPLIES 9

didn't work 😞 

Tried it with followings and it's working fine. There's probably something different in the setting that's not mentioned in the question.

1. Variables:

find_real_file.png

2. Client Script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    g_form.getReference('requested_by', function(user) {
        try {
            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);
        } catch (e) {
            alert(e.message);
        }
    });
}

3. Execution. I only have Department and location entries in the user table filled in.

find_real_file.png

Thomas,

I tried it by setting the default value in requested_by field and deleted the "if isLoading" check.

function onLoad() {
    g_form.getReference('requested_by', function(user) {
        try {
            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);
        } catch (e) {
            alert(e.message);
        }
    });
 }

 I'm now getting the fields filled in.

find_real_file.png

Ravi9
ServiceNow Employee
ServiceNow Employee

if its onload then your function definition is wrong , you cant have isLoading return on a onload CS

it should be just this

function onLoad() {
   //Type appropriate comment here, and begin script below
   
}

whatever code you want to write just add in between curly braces

Found the difference. It's because I have it onChange instead of onLoad. Tried it with onLoad and it doesn't work. It may be because "requested_by" field is not filled in.

How is the "requested_by" field is not filled in.

Maybe better to set fields using default values instead of using client script.