scripting issue

SLeelaRaR
Tera Contributor

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 ------

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();

        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', g_form.getValue('caller_id'));
    ga.getXMLAnswer(function(response) {
        var result = JSON.parse(response);
        alert(result);
    });

   

}
I am getting result as [object object] in alert ., in alert I want to get the result.
5 REPLIES 5

Albert13
Kilo Sage

If you want to see the full JSON object you will have to change the alert in the client script to 

alert(resultJSON.stringify(result));.  I suspect that's just for testing so alert(result.fname); or lname, email or manager will give you the values in the object as well
 
Albert

Vishal Jaswal
Giga Sage

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!

AshishKM
Kilo Patron
Kilo Patron

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.

 

AshishKM_0-1743635413212.png

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Thank you. I found it useful. You made my day.

Now, it is my turn to help you, and I would like to share the https://academized.com/do-my-homework website with you. If you want someone to do your homework, then you can take help from Academized professional essay writers.