Manager name with phone number in alert

Dileep2
Tera Contributor

Hello Everyone, 

 

I have a requirement where I need to populate the Requested by manager with phone number as alert. I have written a client script and script include for that. 

And this needs to be executed based on the drop down of variable in the record producer like 

if(variable == 'dropdown choice') then written all the client script under it and put the alert just before the if. But am getting the alert as Manager and phone number undefined. And they are populating under the alert of the fucntion. 

Can anyone help me in achieving this as in a single alert.

Client script:

if (g_form.getValue('variable_name') == 'No') {
        var manager_name;
        var phone_number;
        var req_user = g_form.getValue('requested_by');
        var req = new GlideAjax('Script include name');
        req.addParam('sysparm_name', 'script include function name');
        req.addParam('sysparm_req_user', req_user);
        req.getXML(getManager);
        function getManager(response) {
            manager_name = response.responseXML.documentElement.getAttribute('answer');
            alert('Manager name is: ' + manager_name); //value is coming
        }
        var req1 = new GlideAjax('Same script include name');
        req1.addParam('sysparm_name', 'Another script include function');
        req1.addParam('sysparm_phone', req_user);
        req1.getXML(getManagerPhone);

        function getManagerPhone(response) {
            phone_number = response.responseXML.documentElement.getAttribute('answer');
            alert('Phone number is :' + phone_number); //value is coming
        }

        alert('Manager is : ' + manager_name + 'Phone number is:' + phone_number); // here coming as undefined

    }
 
Help me to achieve this.

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

I see now that you are getting each value prior to the combined string.  Are the alerts coming in the order expected - Manager name, Phone number, then the one with both?  As a matter of clean coding standards, it is best to initialize variables when they are declared:

var manager_name = '';
var phone_number = '';

It would also be best, and a good habit to get into, to make one trip to the server and back.  In this case, you can do this by combining the functions in the Script Include, and building an object to return both values.  This is an excellent tool to have in your belt for such cases, and simplifies the execution and troubleshooting.  Here's what the Client Script would look like:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

	if (g_form.getValue('did_you_inform_your_direct_manager') == 'No') {
        var req_user = g_form.getValue('requested_by');
        //alert('requested_user is' + req_user);
        var req = new GlideAjax('GetUserManager');
        req.addParam('sysparm_name', 'getUserInfo');
        req.addParam('sysparm_req_user', req_user);
        req.getXML(getManager);

        function getManager(response) {
			var manager_name = '';
        	var phone_number = '';
            var answer = JSON.parse(response.responseXML.documentElement.getAttribute('answer'));
			manager_name = answer.user_name;
			phone_number = answer.phone_num;
            alert('Manager name is: ' + manager_name);
            alert('Phone number is :' + phone_number);
            alert('Manager is : ' + manager_name + 'Phone number is:' + phone_number);
    }
}

And the Script Include:

var GetUserManager = Class.create();
GetUserManager.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    getUserInfo: function() {
        var user = this.getParameter('sysparm_req_user');
        var userGR = new GlideRecord('sn_hr_core_profile');
        userGR.addQuery('sys_id', user);
        userGR.query();
        if (userGR.next()) {
			var mgr = userGR.u_hr_manager;
            var phoneGR = new GlideRecord('sys_user');
            phoneGR.addQuery('sys_id', mgr);
            phoneGR.query();
            if (phoneGR.next()) {
			var results = {
                    "phone_num": phoneGR.getValue("phone"),
                    "user_name": userGR.u_hr_manager.getDisplayValue()
                };
                return JSON.stringify(results);
            }
		} else {     
            return null;
        }	
    },
    type: 'GetUserManager'
});

 

View solution in original post

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

A good way to troubleshoot these issues is to add (more) alerts to the Client Script, so you can see which parts of the script are executing, and gs.addInfoMessage or gs.info lines to the Script Include to confirm that it is running, the value passed in from the client, and how far it is getting - GlideRecord query results, etc.  Ensure the Script Include has the Client callable box checked, and has a unique name.  If you're still having trouble, post the full, actual, Client Script and Script Include using the insert code icon (</>).  

cient script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    if (g_form.getValue('did_you_inform_your_direct_manager') == 'No') {
        var manager_name;
        var phone_number;
        var req_user = g_form.getValue('requested_by');
        //alert('requested_user is' + req_user);
        var req = new GlideAjax('GetUserManager');
        req.addParam('sysparm_name', 'getUserInfo');
        req.addParam('sysparm_req_user', req_user);
        req.getXML(getManager);

        function getManager(response) {
            manager_name = response.responseXML.documentElement.getAttribute('answer');
            alert('Manager name is: ' + manager_name);
        }

        var req1 = new GlideAjax('GetUserManager');
        req1.addParam('sysparm_name', 'getPhoneInfo');
        req1.addParam('sysparm_phone', req_user);
        req1.getXML(getManagerPhone);

        function getManagerPhone(response) {
            phone_number = response.responseXML.documentElement.getAttribute('answer');
            alert('Phone number is :' + phone_number);
        }

        alert('Manager is : ' + manager_name + 'Phone number is:' + phone_number);

    }

}

script include:

var GetUserManager = Class.create();
GetUserManager.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    getUserInfo: function() {
        var user = this.getParameter('sysparm_req_user');
        var user_name = new GlideRecord('sn_hr_core_profile');
        user_name.addQuery('sys_id', user);
        user_name.query();
        if (user_name.next()) {
            return user_name.u_hr_manager.getDisplayValue();
        }
    },

    getPhoneInfo: function() {
	var user1 = this.getParameter('sysparm_phone');
        var user_name = new GlideRecord('sn_hr_core_profile');
        user_name.addQuery('sys_id', user1);
        user_name.query();
        if (user_name.next()) {
            var mgr = user_name.u_hr_manager;
            var phone_num = new GlideRecord('sys_user');
            phone_num.addQuery('sys_id', mgr);
            phone_num.query();
            if (phone_num.next()) {
                return phone_num.phone;
            }
        }
    },

    type: 'GetUserManager'
});

This the entire and full code for client script and the script include written. 

Brad Bowman
Kilo Patron
Kilo Patron

I see now that you are getting each value prior to the combined string.  Are the alerts coming in the order expected - Manager name, Phone number, then the one with both?  As a matter of clean coding standards, it is best to initialize variables when they are declared:

var manager_name = '';
var phone_number = '';

It would also be best, and a good habit to get into, to make one trip to the server and back.  In this case, you can do this by combining the functions in the Script Include, and building an object to return both values.  This is an excellent tool to have in your belt for such cases, and simplifies the execution and troubleshooting.  Here's what the Client Script would look like:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

	if (g_form.getValue('did_you_inform_your_direct_manager') == 'No') {
        var req_user = g_form.getValue('requested_by');
        //alert('requested_user is' + req_user);
        var req = new GlideAjax('GetUserManager');
        req.addParam('sysparm_name', 'getUserInfo');
        req.addParam('sysparm_req_user', req_user);
        req.getXML(getManager);

        function getManager(response) {
			var manager_name = '';
        	var phone_number = '';
            var answer = JSON.parse(response.responseXML.documentElement.getAttribute('answer'));
			manager_name = answer.user_name;
			phone_number = answer.phone_num;
            alert('Manager name is: ' + manager_name);
            alert('Phone number is :' + phone_number);
            alert('Manager is : ' + manager_name + 'Phone number is:' + phone_number);
    }
}

And the Script Include:

var GetUserManager = Class.create();
GetUserManager.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    getUserInfo: function() {
        var user = this.getParameter('sysparm_req_user');
        var userGR = new GlideRecord('sn_hr_core_profile');
        userGR.addQuery('sys_id', user);
        userGR.query();
        if (userGR.next()) {
			var mgr = userGR.u_hr_manager;
            var phoneGR = new GlideRecord('sys_user');
            phoneGR.addQuery('sys_id', mgr);
            phoneGR.query();
            if (phoneGR.next()) {
			var results = {
                    "phone_num": phoneGR.getValue("phone"),
                    "user_name": userGR.u_hr_manager.getDisplayValue()
                };
                return JSON.stringify(results);
            }
		} else {     
            return null;
        }	
    },
    type: 'GetUserManager'
});

 

Thank you Brad for your appropriate solution. It worked!!