client script g_form.getValue issue on referenced field hr_assignment_group

Christine A1
Mega Expert

 

I am using a onChange client script to clear out values or change values in other fields on the form.

I am struggling with how to get the display value of a referenced field if possible. the her_assignment_group is a reference to the Group table.

The trouble is with the hr_assignment_group. If i use the getValue, i get the sys_id. If i use the getReference, i get Undefined.  How do i get the display value? Else, what are my options to use as a condition?

------------------------------------------------------------

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

alert("Hi "+g_form.getValue('hr_assignment_group'));
if (newValue == 'KM HR') {
g_form.setValue('hr_assigned_to','');
g_form.setValue('kmhr_business_service','');
g_form.setValue('hr_ticket_type','kmhr_mgmt');}
else if (newValue == 'Time') {
g_form.setValue('hr_ticket_type','time_mgmt');
g_form.setValue('hr_assigned_to','');
g_form.setValue('kmhr_business_service','');
}

}

1 ACCEPTED SOLUTION

dvp
Mega Sage
Mega Sage

You can get the name by using getReference with callBack function

Try the below script

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

//Type appropriate comment here, and begin script below

var group = g_form.getReference('hr_assignment_group', callBack);
}

function callBack(group){

var name = group.name;

if (name == 'KM HR') {
g_form.setValue('hr_assigned_to','');
g_form.setValue('kmhr_business_service','');
g_form.setValue('hr_ticket_type','kmhr_mgmt');
}
else if (name == 'Time') {
g_form.setValue('hr_ticket_type','time_mgmt');
g_form.setValue('hr_assigned_to','');
g_form.setValue('kmhr_business_service','');
}


}

View solution in original post

11 REPLIES 11

For better performance its is always best to use call back function .

Here is an link explains more about it

https://www.servicenowguru.com/scripting/client-scripts-scripting/gform-getreference-callback/

Yes, but in your example and what they were trying to do you literally went right in to using the callback...which is literally the same thing I did and suggested.

There is literally no performance save between what you and I did.

I'm merely saying, we said the same thing, which we did.

Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

I know this thread is answered but getReference() is a bit of a pet peeve for me.

it's not best practice as it returns the whole record rather than just the one value you need so it's wildly inefficient. If you just need the display value in the client you can use:

g_form.getDisplayBox('field_name').value

If you need more values you should be using GlideAjax.

Sigh...doesn't work in Client Script...

Which is why I suggested the method that I did.


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Yea, ultimately, this would be better than either of the previous suggestions. So good call out.


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!