The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Getting a null value

CátiaA
Tera Contributor

Hi,

I have this Script include and client script to fulfill the user mobile phone on a form on load.

 

var GetUserPhone = Class.create();
GetUserPhone.prototype = {
    initialize: function() {},

    getMobilePhone: function(userID) {
        try {
            var gr = new GlideRecord('sys_user');
            if (gr.get(userID)) {
                return gr.mobile_phone.toString();
            }
        } catch (error) {
            gs.error("Error in GetUserPhone.getMobilePhone: " + error.message);
            return ''; // Return empty in case of error
        }
    },

    type: 'GetUserPhone'
};

 

 

Client script:

 

function onLoad() {
    var userID = g_form.getValue('u_requested_for');

    if (userID) {
        alert('id found: ' + userID);
        var ga = new GlideAjax('GetUserPhone');
        ga.addParam('sysparm_name', 'getMobilePhone');
        ga.addParam('sysparm_userID', userID);
        ga.getXMLAnswer(handleResponse);
    }
}

function handleResponse(response) {
    var mobilePhone = response;

    alert('Mobile phone found: ' + mobilePhone);

    if (mobilePhone) {g_form.setValue('mobile_swt', mobilePhone);
    } else {
        g_form.clearValue('mobile_swt');
    }
}

 

 

The mobilePhone is returning NULL in the alert, I confirmed that the user ID I selected (and its returning on the first alert) has a value on the field.
Can you find the issue?

Thank you in advance!

1 ACCEPTED SOLUTION

Sanjay191
Tera Sage

Hello @CátiaA 

In the Script Include, use this.getParameter('sysparm_userID') to fetch the parameter sent from the client script.
So please make the required changes as following code then executes ..

Script include --

var GetUserPhone = Class.create();
GetUserPhone.prototype = {
initialize: function() {},

getMobilePhone: function() {
try {
var userID = this.getParameter('sysparm_userID'); // Correctly retrieve the user ID parameter
var gr = new GlideRecord('sys_user');
if (gr.get(userID.toString())) {
return gr.mobile_phone.toString();
}
} catch (error) {
gs.error("Error in GetUserPhone.getMobilePhone: " + error.message);
return ''; // Return empty in case of error
}
return ''; // Return empty if no record is found
},

type: 'GetUserPhone'
};

Client Script --

function onLoad() {
var userID = g_form.getValue('u_requested_for');

if (userID) {
alert('ID found: ' + userID);
var ga = new GlideAjax('GetUserPhone');
ga.addParam('sysparm_name', 'getMobilePhone');
ga.addParam('sysparm_userID', userID); // Ensure this matches the Script Include parameter
ga.getXMLAnswer(handleResponse);
}
}

function handleResponse(response) {
var mobilePhone = response;

alert('Mobile phone found: ' + mobilePhone);

if (mobilePhone) {
g_form.setValue('mobile_swt', mobilePhone);
} else {
g_form.clearValue('mobile_swt');
}
}


If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You

View solution in original post

5 REPLIES 5

Sanjay191
Tera Sage

Hello @CátiaA 

In the Script Include, use this.getParameter('sysparm_userID') to fetch the parameter sent from the client script.
So please make the required changes as following code then executes ..

Script include --

var GetUserPhone = Class.create();
GetUserPhone.prototype = {
initialize: function() {},

getMobilePhone: function() {
try {
var userID = this.getParameter('sysparm_userID'); // Correctly retrieve the user ID parameter
var gr = new GlideRecord('sys_user');
if (gr.get(userID.toString())) {
return gr.mobile_phone.toString();
}
} catch (error) {
gs.error("Error in GetUserPhone.getMobilePhone: " + error.message);
return ''; // Return empty in case of error
}
return ''; // Return empty if no record is found
},

type: 'GetUserPhone'
};

Client Script --

function onLoad() {
var userID = g_form.getValue('u_requested_for');

if (userID) {
alert('ID found: ' + userID);
var ga = new GlideAjax('GetUserPhone');
ga.addParam('sysparm_name', 'getMobilePhone');
ga.addParam('sysparm_userID', userID); // Ensure this matches the Script Include parameter
ga.getXMLAnswer(handleResponse);
}
}

function handleResponse(response) {
var mobilePhone = response;

alert('Mobile phone found: ' + mobilePhone);

if (mobilePhone) {
g_form.setValue('mobile_swt', mobilePhone);
} else {
g_form.clearValue('mobile_swt');
}
}


If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You

Thank you, I eventually found this article and got the script incl working close to what you have here but I removed the initialize function: https://codecreative.io/blog/glideajax-troubleshooting-guide/

yes that is also fine and also you can write your code in initialize function then you did not need to create the new function.

 

Also Just put the instead of var mobilePhone = response;

 var mobilePhone = response.responseXML.documentElement.getAttribute('answer');