Populate Email on Name Change

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 reference to my table "Requestor Details", what i want and need is that if i change the name in the "Requests" the "Email" Field will be automatically populated from the value in "Requestor Details"

My code so far

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 Includes
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);
        gr.query();
        if (gr.next()) {
            email = gr.getValue('email'); 
            gs.info('Email found for Name "' + name + '": ' + email); 
        } else {
            gs.info('No record found for Name: ' + name);
        }
        return email;
    },

    type: 'RequestorDetailsHelper'
};
 
 
---------I am getting a null result in my console log
---------I tried to run in Script - Background, and i am getting correct results.
---------I am using PDI only so i don't know if i have restrictions, but so far i made it accessible to admin, users, itil. 
 
see attached images
Thank you!!!
 
##AppEngin
##ClientScrip
##ScriptInclud
1 ACCEPTED SOLUTION

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.

View solution in original post

5 REPLIES 5

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.

Sai Kumar P
Tera Guru

Hi, try the below code

CLIENT SCRIPT::

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        g_form.setValue('email'''); 
        return;
    }
 
    var ga = new GlideAjax('RequestorDetailsHelper'); 
    ga.addParam('sysparm_name''getEmail'); 
    ga.addParam('sysparm_req', 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::
 
var RequestorDetailsHelper = Class.create();
RequestorDetailsHelper.prototype = {
    initialize: function() {},
 
    getEmail: function(name) {
        var req = this.getParameter('sysparm_req');
        var email = '';
        var gr = new GlideRecord('x_1566811_notifi_0_requestor_details'); 
        gr.addQuery('sys_id', req);
        gr.query();
        if (gr.next()) {
            email = gr.getValue('email'); 
            gs.info('Email found for Name "' + name + '": ' + email); 
        } else {
            gs.info('No record found for Name: ' + name);
        }
        return email;
    },

    type: 'RequestorDetailsHelper'
};
 
-----------------------------------------------------------------------------------------------------------------------------------
                               If this is working for you, Accept the solution and mark it as helpful
 

Juhi Poddar
Kilo Patron

Hello @glennjasper0 

There are few points to take care:

  • Script include should be Client callable.
  • Once a script include is created without checkbox the client callable field, changing this later will not make it client callable. A new script include has to be created in that case.
  • From the script it is noticed that script include is not client callable.
  • This is how the script looks like if it is client callableJuhiPoddar_0-1732465152929.png

     

  • An onChange client script to request table with field set to name is created.
  • client script:

 

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('Populate_email_on_name_change'); 
    ga.addParam('sysparm_name', 'getEmail'); 
    ga.addParam('sysparm_requesterId', newValue); //returns sys_id of reference field 'name'
    ga.getXMLAnswer(function(response) {
        console.log('Response from server:', response);
        if (response) {
            g_form.setValue('email', response);
        } else {
            g_form.setValue('email', ''); 
        }
    });
}

 

  • script include:

 

var Populate_email_on_name_change = Class.create();
Populate_email_on_name_change.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getEmail: function() {
        var email = '';
		var sysId = this.getParameter('sysparm_requesterId');
        var gr = new GlideRecord('x_1566811_notifi_0_requestor_details');
        gr.addQuery('sys_id', sysId);
        gr.query();
        if (gr.next()) {
            email = gr.getValue('email');
            gs.info('Email found for Name "' + name + '": ' + email);
        } else {
            gs.info('No record found for Name: ' + name);
        }
        return email;
    },
    type: 'Populate_email_on_name_change'
});​

 

  • Note: Please create a fresh script include with client callable enable. Accordingly call it from client script.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

 

 

Hello @Juhi Poddar ,

 

It gives me this message,

glennjasper0_0-1732497827888.png

 

This is my code and setting looks like in script include, i don't have option "Client callable" like yours

glennjasper0_1-1732497909088.png