GlideAjax Auto populate field depending on the name selected

glennjasper0
Tera Contributor

Hello 

I created 2 tables in app engine servicenow, table "Requests" and table "Requestor Details"

Now my table "Requests" has Field "Name" that is referenced to my table "Requestor Details", what i want and need is that if i change the "Name" field in the "Requests" Table the "Email" Field will be automatically populated with the value from "Requestor Details" Table

 

Client Script code

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        g_form.setValue('email', '');
        return;
    }
    // GlideAjax call to the Script Include
    var ga = new GlideAjax('RequestorDetailsHelper');
    ga.addParam('sysparm_action', 'getEmail'); 
    ga.addParam('sysparm_name', newValue); 
    ga.getXMLAnswer(function(response) {
        console.log('Response from server:', response); 
        if (response) {
            g_form.setValue('email', response); 
        } else {
            g_form.setValue('email', ''); 
        }
    });
}
 
Script include code
var RequestorDetailsHelper = Class.create();
RequestorDetailsHelper.prototype = {
    initialize: function() {},

    getEmail: function(name) {
        var email = '';
        var gr = new GlideRecord('x_1566811_notifi_0_requestor_details'); 
        gr.addQuery('name', name); // Make sure 'name' is the correct field in your table
        gr.query();
        if (gr.next()) {
            email = gr.getValue('email'); // Ensure 'email' matches your table's email field
            gs.info('Email found for Name "' + name + '": ' + email);
        } else {
            gs.info('No record found for Name: ' + name); 
        }
        return email;
    },

    type: 'RequestorDetailsHelper'
};
 
---- My result in console is NULL
---- But i can see a correct result in Scripts - Background when i run it
---- Please See attached images 
 
Tried everything in my PDI, i don't know if this is a access issue but i included admin, user, itil in access. don't know what i missed.
 
 
1 ACCEPTED SOLUTION

Hi @glennjasper0 ,

 

No still you have not followed the code. I have clearly mentioned that using action you can not call script include

use below in you client script and replace the with actual name.

 

var ga = new GlideAjax('UserUtils');
ga.addParam('sysparm_name', 'checkVipUser');
ga.addParam('sysparm_caller_sys_id', newValue);
ga.getXML(ValidateVipUser);
 
function ValidateVipUser(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
}
}

ga.addParam('sysparm_name', 'getEmail'); 

View solution in original post

4 REPLIES 4

Runjay Patel
Giga Sage

Hi @glennjasper0 ,

 

you have not followed glide ajax syntax.

change below.

ga.addParam('sysparm_name', 'getEmail'); 
    ga.addParam('sysparm_nameSysId', newValue); 
 
in script include use
var name =this.getParameter(‘sysparm_nameSysId’);
 
remove name from your function.
 
note: you can not use sysparam_name to o pass value from client to server side. It is used to call function defined in script include.
 
Accept the solution if it helped.
 
 

Hello @Runjay Patel 

thanks for this, however,

I tried to follow your instruction and guide in your blog, but got no luck, I'm still getting null result, 

I also tried to pass 'sample' string directly from my script include for testing purpose but it is not passing to my client script.

 

don't know if i did it correctly or missed something, check my code in attached file

 

 

Hi @glennjasper0 ,

 

No still you have not followed the code. I have clearly mentioned that using action you can not call script include

use below in you client script and replace the with actual name.

 

var ga = new GlideAjax('UserUtils');
ga.addParam('sysparm_name', 'checkVipUser');
ga.addParam('sysparm_caller_sys_id', newValue);
ga.getXML(ValidateVipUser);
 
function ValidateVipUser(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
}
}

ga.addParam('sysparm_name', 'getEmail'); 

It worked! thank you 😄 i followed everything from your blog