how to query a table in client script in servicenow.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2023 10:02 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2023 10:15 AM
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2023 11:42 AM
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;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2023 12:14 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2023 01:17 PM
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?