how to query a table in client script in servicenow.

Community Alums
Not applicable

Based on requested for how to populate the values such as (city, country code, employee number, gender, street, zip code) from the custom table (u_uc_divya) to the service catalog.

 

PRITHVIRAMESH_1-1684429256943.pngPRITHVIRAMESH_2-1684429278028.png

 

 

16 REPLIES 16

Prince Arora
Tera Sage
Tera Sage

@Community Alums 

 

Please create a onChange client script on requested_for and mentioned the below script:

 

var data = g_form.getReference("requested_for",myData);

function myData(data){

g_form.setValue("city",data.city);

g_form.setValue("city",data.city);

g_form.setValue("city",data.city);

g_form.setValue("country_code",data.country_code);

g_form.setValue("employee_number",data.employee_number);

g_form.setValue("gender",data.gender);

g_form.setValue("street",data.street);

g_form.setValue("zip_code",data.zip_code);

}

 

Note: Please check the backend name of fields according to your instance

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.
 

Community Alums
Not applicable

Actually referring to your code i have written one code can u check and tell me where i am wrong?

function onLoad() {
var data = g_form.getReference("requested_for", myData);

var gr = getUserInfo(data);
g_form.setValue("city", data.city);
g_form.setValue("country_code", data.country_code);
g_form.setValue("employee_number", data.employee_number);
g_form.setValue("gender", data.gender);
g_form.setValue("street", data.street);
g_form.setValue("zip_postal_code", data.zip_postal_code);

function myData(sysID) {
var gr = new GlideRecord('u_uc_divya');
gr.addQuery('sys_id', sysID);
gr.query();
gr.next();
return gr;
}
}

You need to replace your code with the code Prince provided.  He is using a callback function to populate the data and you are still using a GlideRecord which you shouldn't be doing in a client script.

So something like this:

function onLoad() {
var data = g_form.getReference('requested_for',myData);

function myData(data){
g_form.setValue('city',data.city);
g_form.setValue('country_code',data.country_code);
g_form.setValue('employee_number',data.employee_number);
g_form.setValue('gender',data.gender);
g_form.setValue('street',data.street);
g_form.setValue('zip_code',data.zip_code);

 }
}

Just make sure the name of the fields (city for example...might be u_city since you are using a custom table)


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

Community Alums
Not applicable

I tried your same code but after running the script in the field it shows undefined. Can you help me with that is to why it is showing like that?