Client Script to auto populate field based on another field not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2024 05:34 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2024 06:17 AM
you will have to use GlideAjax as you cannot dot walk 2 levels in client script
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2024 06:27 AM - edited 06-18-2024 06:30 AM
Hi @User393503 ,
I tried your problem in my PDI it works for me please refer below solution
Client script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var budget1 = g_form.getValue('parent');
var ga = new GlideAjax('AutoPopulate_CallerInfo');
ga.addParam('sysparm_name', 'getCallerInfo');
ga.addParam('sysparm_value', budget1);
ga.getXML(callback);
function callback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert('Name = ' + answer);
}
}
Create Script Include which is client callable and below code
getCallerInfo: function() {
var ids = this.getParameter('sysparm_selectedIds');
gs.log("sysparm_selectedIds" + ids);
var incGr = new GlideRecord('yourTable');
incGr.addQuery('parent', ids); //<your query>
incGr.query();
if (incGr.next()) {
var name = incGr.getDisplayValue("fieldBackendName");
return name
}
}
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2024 06:42 AM
Hi @User393503 ,
You can achieve this through getReference method and dot walking,
Please check the below example which gives me alert of the caller if changed, in your case you can add setvalue instead of alert.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var caller = g_form.getReference('caller_id', currentUser1); // add your reference field here
function currentUser1(caller) {
alert(caller.name); // i have added alert for testing instaed of this you can set value based your requirement.
}
}
Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Swathi Sarang