How to print(or show info msg) an array using getXMLAnswer method through client script?

KM SN
Tera Expert

I just want to send two details of caller to an array and bringing them back to client script and there using Client script i need to show the array whenever the caller field changes. but i am getting msg as below and i am adding script too.

ManiHouse1_0-1715323490467.png

 



Client Script:

var cid = g_form.getValue('caller_id');
var ga = new GlideAjax('gettingcallerdetails');
ga.addParam('sysparm_name','callerdetails');
ga.addParam('sysparm_caller', cid);
ga.getXMLAnswer(details);

function details(response){

        //var res = JSON.parse(response);
        var res = [response.toString()];
        g_form.addInfoMessage('Hi'+res);
        }
 
 
Script Include:


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

    callerdetails: function() {

        var abc = this.getParameter('sysparm_caller');
        //var obj = {};
        var obj = [];
        var utable = new GlideRecord('sys_user');
        if (utable.get(abc)) {

            //obj.phone = utable.getDisplayValue('mobile_phone');
            //obj.email = utable.getDisplayValue('email');
            obj.push(utable.getDisplayValue('mobile_phone'));
            obj.push(utable.getDisplayValue('email'));
            gs.addInfoMessage('SC :' + obj);
        }

        // return JSON.stringify(obj);

        return obj;


    },

    type: 'gettingcallerdetails'
});
6 REPLIES 6

Weird
Mega Sage

Well, there's some things that could be cleaned up, but I guess your biggest problems are related to the array you're using and you're trying to handle the response in a weird way on client side.

You could consider using your object logic instead

 

callerdetails: function() {

        var callerId = this.getParameter('sysparm_caller');
		var userObj = {};
        var utable = new GlideRecord('sys_user');
		utable.get(callerId);

        if (utable.isValidRecord()) {
            obj.phone = utable.getValue('mobile_phone');
            obj.email = utable.getValue('email');
            return JSON.stringify(obj);
        }
        return null;


    },

 

Then handle the response in your client script:

 

function details(answer) {
    if (answer) {
         var responseData = JSON.parse(answer);
         g_form.setValue("phone", responseData.phone);
         g_form.setValue("email", responseData.email);
    }
}

 


You should be able to use your array logic as well if you want.
Just end your script include with return obj.toString(); so that you're returning a string.
In your client script use 
var res = response.split(","); to get an array of the values.


Your
var res = [response.toString()];
Is at the very least creating an array that has both values in the same index. Logging array you'd then have to separately add .toString() to it:
g_form.addInfoMessage('Hi'+res.toString());


Edit:
The last part probably answers your question, but I'd recommend using objects rather than arrays since they're more logical to handle and it's easy to access the values.

Can i return two variables/ two arrays like below i am returning ? is there a way or else we always need to store them in array need to send as one.??
 
Script Include:
 
callerdetails: function() {

        var abc = this.getParameter('sysparm_caller');
        //var obj = {};
       // var obj = [];
        var utable = new GlideRecord('sys_user');
        if (utable.get(abc)) {

            //obj.phone = utable.getDisplayValue('mobile_phone');
            //obj.email = utable.getDisplayValue('email');
            var def = utable.mobile_phone;
            var xyz = utable.email;
            gs.addInfoMessage('SC :' + obj);
        }

        return def,xyz;

Ahmmed Ali
Mega Sage

Hello @KM SN 

 

You can adding alert for Array and not the value in it. modify below lines

 

var res = [response.toString()];
        g_form.addInfoMessage('Hi'+res);
 
to
        g_form.addInfoMessage('Hi'+response);
 
and in script include,
replace 
return obj;
with
return obj.join(",");
 
Above you are currently returning array object and not values in it, so it will just return address of Array object. So you need to return string version of array which you will get when you join() and it will be comma separated string.
Same string will be shown in message in form.
 
Thank you,
Ali
 
 
If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

Can i return two variables/ two arrays like below i am returning ? is there a way or else we always need to store them in array need to send as one.??
 
Script Include:
 
callerdetails: function() {

        var abc = this.getParameter('sysparm_caller');
        //var obj = {};
       // var obj = [];
        var utable = new GlideRecord('sys_user');
        if (utable.get(abc)) {

            //obj.phone = utable.getDisplayValue('mobile_phone');
            //obj.email = utable.getDisplayValue('email');
            var def = utable.mobile_phone;
            var xyz = utable.email;
            gs.addInfoMessage('SC :' + obj);
        }

        // return JSON.stringify(obj);

        return def,xyz;