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

Hello @glennjasper0 

This script seems fine overall, but there's one issue: you're working within an application scope while the script include is in the global scope.

Here are two ways to resolve this:

  1. Create the script include within the same application scope.
  2. Check the sys_scope_privilege table for a cross-scope privilege request. If it exists, approve the request to allow access.

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