Client Script calling Script Include returning null

Community Alums
Not applicable

I want show the user first name and mobile number when caller field changes. I am trying to use a client script to call a script include to do this. I did set Client callable to true .But I am getting null value.

 

client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        g_form.setReadOnly('u_custom_mobile', true);
        g_form.setReadOnly('u_first_name', true);
        return;
    }

    alert('Script Runs '+g_form.getValue('caller_id'));
    var gx = new GlideAjax('userDetails');
    gx.addParam('sysparam_name', 'callernameandphone');
    gx.addParam('sysparam_user', g_form.getValue('caller_id'));
    gx.getXML(getData);
    function getData(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        alert(answer);
        if(answer) {
            var temValue = answer.toString().split('|');
            g_form.setValue('u_custom_mobile', temValue[0]);
            g_form.setValue('u_first_name', temValue[1]);

        }

    }
}
Script Include:
var userDetails = Class.create();
userDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    callernameandphone: function() {
        var usrId = this.getParameter('sysparam_user');
        var gr = new GlideRecord('sys_user');
        if (gr.get(usrId)) {
            return gr.getValue('mobile_phone') + '|' + gr.getValue('first_name');
        }
       
    },
    type: 'userDetails'
});
// Please any one help me

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

When you add Parameters to a Client Script and retrieve them in the Script Include, the name must start with sysparm_, not sysparam_ so your Client Script will look like this:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        g_form.setReadOnly('u_custom_mobile', true);
        g_form.setReadOnly('u_first_name', true);
        return;
    }

    alert('Script Runs '+g_form.getValue('caller_id'));
    var gx = new GlideAjax('userDetails');
    gx.addParam('sysparm_name', 'callernameandphone');
    gx.addParam('sysparm_user', g_form.getValue('caller_id'));
    gx.getXML(getData);
    function getData(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        alert(answer);
        if(answer) {
            var temValue = answer.toString().split('|');
            g_form.setValue('u_custom_mobile', temValue[0]);
            g_form.setValue('u_first_name', temValue[1]);
        }
    }
}

and the Script Include:

var userDetails = Class.create();
userDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    callernameandphone: function() {
        var usrId = this.getParameter('sysparm_user');
        var gr = new GlideRecord('sys_user');
        if (gr.get(usrId)) {
            return gr.getValue('mobile_phone') + '|' + gr.getValue('first_name');
        }
    },
    type: 'userDetails'
});

You can further troubleshoot this by adding more alerts to the Client Script and gs.info lines to the Script Include to confirm it's running, and see the parameter values and how far it is getting.

View solution in original post

7 REPLIES 7

Pratima Kalamka
Kilo Sage

Hello @Community Alums,

Please correct these two line syntax:

 

 gx.addParam('sysparm_name', 'callernameandphone');
gx.addParam('sysparm_user', g_form.getValue('caller_id')); 

 

 

Using getReference():

 

Client Script details:
Type: onChange
Table: Incident
Field Name: Caller
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
          return;
}
    var caller = g_form.getReference('caller_id');
g_form.setValue('department',caller.department);
g_form.setValue('location',caller.location);
g_form.setValue('email',caller.email);
g_form.setValue('name',caller.name);
g_form.setValue('Mobile phone',caller.mobile_phone);
}

 

 

If my answer is helpful please mark it as helpful or correct!!

Using "getReference" is not the best for performance reasons:

  1. you are not using a call back function
  2. it returns a light-weight GlideRecord with all the data in the record instead of just the bits we need
  3. in your example, although NOT required by the OP, setting the Department and Location field would trigger 2 more calls back to the server to get the display values for those records

As I mentioned above, take a look at this post for an alternative method: TNT: Returning Data from GlideAjax Calls

Ramesh Poola
Tera Guru

Hi Nagarjuna,

Please try this once

 

var caller = g_form.getReference('caller_id', getDetails); // getDetails is our callback function  
function getDetails(caller) { //reference is passed into callback as first arguments
        g_form.setValue('u_custom_mobile', caller.mobile_phone);
        g_form.setValue('u_first_name', caller.first_name);
}

 

Regards,
Ramesh