Populating one variable based on another variable on a form

Diana27
Tera Guru

Hi,

I have 2 variables in a variable set that I am using on a form.  The first variable is requested_for using the type Requested For:

find_real_file.png

I have another variable manager.

All I need to do is populate that variable (manager) with the manager of the user in Requested For.  This should be easy, however it is taking all day and I still cannot get it to work.  I'm about to go insane.

Thanks in advance for any assistance.

Diana

1 ACCEPTED SOLUTION

no... you need to create a catalog client script, not the regular client script. Instead of table, field you select variable set, variable. it should look more or less like this:

 

find_real_file.png

View solution in original post

18 REPLIES 18

Hello Diana,

Did you try the updated code? That should work.

 

Hi,

I was not able to get it to work.  However I was able to get tomaszienkiewicz's suggestion to work.

Thank you for all your help.

Tom Sienkiewicz
Mega Sage

Hi, as I understand, you don't want to enforce the reference qualifier but rather set the value for the Manager variable dynamically, based on the Requested for variable.

To do this, you can create an onChange catalog client script, for the "Requested for" variable.

Make sure to remove the "isloading" part of the standard stop condition, as your Requested for will be populated on load automatically..

then you can do two things:

1) use the getReference method, e.g.

g_form.getReference('requested_for', callBack);

function callBack(requestor) { 
g_form.setValue('manager', requestor.manager);
}

2. create a client-callable script include and call it from your client script via AJAX, the SI should return the sys_id of the manager which you can then populate in the Manager field same way as above.

The AJAX way is a bit better for performance as you are only pulling the sys_id from server to client, as oposed to getReference wich pulls the entire record, but you could just get away with getReference in this case 😉

Hi,

Like I said I'm new to this.  Does this look correct for the scirpt:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
g_form.getReference('requested_for', callBack);

function callBack(requestor) {
g_form.setValue('manager', requestor.manager);
}

}

More like this - on second thought, keep the "isLoading" part intact as the code might run twice.

 

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

//Type appropriate comment here, and begin script below
g_form.getReference('requested_for', callBack);

function callBack(requestor) {
g_form.setValue('manager', requestor.manager);
}
}

 

This assumes your variables have names requested_for, manager.

Also make sure you have the UI type set to where your script will actually run (desktop / portal)