get reference value from a different table

karan_tarani
Kilo Contributor

Hi everyone,

I am trying to create a field on a category table and want to reference the default value of that field on a HR case table

For example:

Sub Category Value is REP1 for Category called Data/Report Request

find_real_file.png

I would like REP1 to populate on the HR case form when the Category Data/Report Request is selected.

find_real_file.png

Any Ideas on how i can achieve this?

1 ACCEPTED SOLUTION

Hi Karan,



Below script should actually work and shouldn't change the value back to the previous one. Can you put some alert as below?



function onChange(control, oldValue, newValue, isLoading, isTemplate) {



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


return;


}


else if(newValue != oldValue){


alert('newValue is '+newValue);


var category = g_form.getReference('u_category', populateSubCategory);


}



function populateSubCategory(category) {


g_form.setValue("u_sub_category",category.u_sub_category);


}



}



Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

6 REPLIES 6

SanjivMeher
Kilo Patron
Kilo Patron

Hi Karan,



You can make the subcategory field a dependednt field of category, but that will just filter the subcategory related to that category but wont set the category.



To set the category on Selection, you will need an onChange client script to do that.



Please mark this response as correct or helpful if it assisted you with your question.

Thanks Sanjiv. I am getting the subcategory values from a choice field that resides on the category table.



find_real_file.png


find_real_file.png



The only thing i am not able to figure out is the onchange client script, any idea where i can find one for my scenario?


The SN Nerd
Giga Sage
Giga Sage

If you are prototyping this solution, perhaps use the following



var grHRCategory= g_form.getReference('u_category', populateSubCategory);


function populateSubCategory(grHRCategory) {


g_form.setValue('u_sub_category', grHRCategory.u_sub_category);


}



Once you have proved your concept, refactor to use GlideAjax.



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Thanks Paul. This is very helpful! i was able to set the default value for sub category by using this script.



function onChange(control, oldValue, newValue, isLoading, isTemplate) {



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


return;


}


else if(newValue != oldValue){


var category = g_form.getReference('u_category', populateSubCategory);


}



function populateSubCategory(category) {


g_form.setValue("u_sub_category",category.u_sub_category);


}



}




However, i have one challenge. The user needs the ability to change the sub category value and in this scenario it should not get the value from the category table on load. Right now, if i change the sub category value and hit save the value defaults back to the one mentioned in the category table, any ideas to get around that?