onLoad Catalog Client Script - Populate Variables

Casey9
Tera Contributor

I'm trying to write an OnLoad Client Script for a catalog item. On the form we have two reference fields to the sys_user table. One is called Requestor (u_requestor) and the other is On Behalf Of (mrb_on_behalf_of) - this field can be used if someone needs to request access for someone else. 

If this field is populated I want it to populate some fields with that users details (mrb_on_behalf_of), if that field is not filled in then I want it to populate the fields with the Requestors user details (u_requestor). I've got the below script, however it's only working for the Requestors details and not the On Behalf Of. The reason I am using on load is because we're using an order guide and the same variables are on the catalog items.

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

}
var name = g_form.getReference('mrb_on_behalf_of');
if (name ==null){
userObject = g_form.getReference('u_requestor',setUserInfo);}
else {
userObject = g_form.getReference('mrb_on_behalf_of',setUserInfo);}

function setUserInfo(userObject){
g_form.setValue('division',userObject.u_division);
g_form.setValue('position_title_02',userObject.title);
g_form.setValue('department',userObject.department);

}


}

 

Thanks in advance

1 ACCEPTED SOLUTION

Willem
Giga Sage
Giga Sage

Can you try this:

function onLoad(control, oldValue, newValue, isLoading) {
    var name = g_form.getValue('mrb_on_behalf_of');
    if (name) {
        userObject = g_form.getReference('mrb_on_behalf_of', setUserInfo);
    }
    else{
        userObject = g_form.getReference('u_requestor', setUserInfo);
    }

    function setUserInfo(userObject) {
        g_form.setValue('division', userObject.u_division);
        g_form.setValue('position_title_02', userObject.title);
        g_form.setValue('department', userObject.department);
    }
}

View solution in original post

7 REPLIES 7

MrMuhammad
Giga Sage

Try this

function onLoad() {

var userObject;
var name = g_form.getReference('mrb_on_behalf_of');
if (name ==null || name == '' ){
userObject = g_form.getReference('u_requestor',setUserInfo);}
else {
userObject = g_form.getReference('mrb_on_behalf_of',setUserInfo);}

function setUserInfo(userObject){
g_form.setValue('division',userObject.u_division);
g_form.setValue('position_title_02',userObject.title);
g_form.setValue('department',userObject.department);

}


}

 
Regards,
Muhammad

Hi Muhammad, 

Thanks for the suggestion, unfortunately that didn't work. Still only populating the Requestor details.

Willem
Giga Sage
Giga Sage

You should remove the

if (isLoading || newValue == '') {
        return;

    }

This is exiting the script when it is loading, something you do not want to do on an onload client script.

Willem
Giga Sage
Giga Sage

Can you try this:

function onLoad(control, oldValue, newValue, isLoading) {
    var name = g_form.getValue('mrb_on_behalf_of');
    if (name) {
        userObject = g_form.getReference('mrb_on_behalf_of', setUserInfo);
    }
    else{
        userObject = g_form.getReference('u_requestor', setUserInfo);
    }

    function setUserInfo(userObject) {
        g_form.setValue('division', userObject.u_division);
        g_form.setValue('position_title_02', userObject.title);
        g_form.setValue('department', userObject.department);
    }
}