scripting issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2025 02:40 AM
Hi ,Here I wrote the server side & client script to auto populate the caller values when caller is changed ., but i am not getting perfect result ., so please correct my code to achieve the result.
----script include ------
------------client script--------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2025 03:51 PM
If you want to see the full JSON object you will have to change the alert in the client script to
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2025 03:59 PM - edited 04-02-2025 04:00 PM
Hello @SLeelaRaR
Minor Correction made:
Script include:
var PopulateCallerData = Class.create();
PopulateCallerData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getcaller: function() {
var callerObj = {};
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', this.getParameter('sysparm_user_id'));
gr.query();// you can also use setLimit() for faster processing
if (gr.next()) {
callerObj.fname = gr.getValue('first_name');
callerObj.lname = gr.getValue('last_name');
callerObj.email = gr.getValue('email');
callerObj.manager = gr.getValue('manager');
}
return JSON.stringify(callerObj);
},
type: 'PopulateCallerData'
});
Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('PopulateCallerData');
ga.addParam('sysparm_name', 'getcaller');
ga.addParam('sysparm_user_id', newValue); // Corrected above to use “newValue” directly
ga.getXMLAnswer(function(response) {
var result;
try {
result = JSON.parse(response); // Added error handling for JSON parsing
if (result) {
g_form.setValue('first_name', result.fname || '');
g_form.setValue('last_name', result.lname || '');
g_form.setValue('email', result.email || '');
g_form.setValue('manager', result.manager || '');
}
} catch (error) {
console.error("Error parsing response: ", error);
}
});
}
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2025 04:11 PM
Hi @SLeelaRaR
You don't need to write GlideAjax, instead use the Auto-populate feature and get the desire column value of selected user record.
If you have 4 different field (firstName,lastName,email,manager) on form, open each field definition page and select the Auto-Populate tab and select the User Field name in Dependent Question, first two field will be same for all 4 fields, only change the dot walk path for each one.
-Thanks,
AshishKM
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2025 05:13 AM
Thank you. I found it useful. You made my day.