List collector to populate email addresses and phone numbers

Community Alums
Not applicable

Hi,

The code is correctly working when I used it just for email addresses, but for phone numbers, it is printing email address. Kindly help.

 

Glide Ajax

 

 

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

    var ga = new GlideAjax("listCollectorEmailClass");
    ga.addParam("sysparm_name", "listCollectorEmailFunction");
    ga.addParam("sysparm_id", newValue);
    ga.getXML(callBackFuntion);

    function callBackFuntion(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		alert(answer);
        g_form.setValue("s_email", answer);
		g_form.setValue("s_phone", answer);
    }



}

 

 

ScriptIncludes

 

 

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

    listCollectorEmailFunction: function() {
        var userArr = this.getParameter('sysparm_id').toString().split(',');
        var emailArr = [];
        var emailArr1 = [];

        for (var i = 0; i < userArr.length; i++) {
            var grUser = new GlideRecord('sys_user');

            if (grUser.get(userArr[i].toString())) {
                var jsonObj = {};
                emailArr.push(grUser.getValue('email'));
                emailArr1.push(grUser.getValue("phone"));
            }
        }
        return emailArr.toString();

    },
    type: 'listCollectorEmailClass'
});

 

contacts.PNG

 

 

Regards

Suman P.

1 ACCEPTED SOLUTION

Hi @Community Alums, 

Please create an object in the script include. Instead, emailArr = [] create retObj = {} object and then validate once.

Thanks and Regards
Anshul

View solution in original post

4 REPLIES 4

anshul_goyal
Kilo Sage

Hello @Community Alums,

In the script include code you made a mistake, you are returning only emailArr and pushing phone in a different array (i.e., emailArr1).

Please use the below code for your reference: 

Script Include:

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

listCollectorEmailFunction: function() {
var userArr = this.getParameter('sysparm_id').toString().split(',');
var retObj = {};

for (var i = 0; i < userArr.length; i++) {
var grUser = new GlideRecord('sys_user');

if (grUser.get(userArr[i].toString())) {
retObj.email = grUser.getValue('email');
retObj.phone = grUser.getValue("phone");
}
}
return JSON.stringify(retObj);

},
type: 'listCollectorEmailClass'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
 
    var ga = new GlideAjax("listCollectorEmailClass");
    ga.addParam("sysparm_name", "listCollectorEmailFunction");
    ga.addParam("sysparm_id", newValue);
    ga.getXML(callBackFuntion);
 
    function callBackFuntion(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
        var retVal = JSON.parse(answer);
g_form.setValue("s_email", retVal.email);
g_form.setValue("s_phone", retVal.phone);
    }
 
}

I hope this will help you solve your problem. Please mark it as Accepted and Helpful.
Thanks and Regards
Anshul

 

 

Community Alums
Not applicable

Hi @anshul_goyal ,

I tried this, but it is not working. It is showing blank for both email and phone field.

 

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

    listCollectorEmailFunction: function() {
        var userArr = this.getParameter('sysparm_id').toString().split(',');
        var emailArr = [];

        for (var i = 0; i < userArr.length; i++) {
            var grUser = new GlideRecord('sys_user');

            if (grUser.get(userArr[i].toString())) {
                emailArr.email = grUser.getValue('email');
                emailArr.phone = grUser.getValue("phone");
            }
        }
		return JSON.stringify(emailArr);
    },

    type: 'listCollectorEmailClass'
});

 

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

    var ga = new GlideAjax("listCollectorEmailClass");
    ga.addParam("sysparm_name", "listCollectorEmailFunction");
    ga.addParam("sysparm_id", newValue);
    ga.getXML(callBackFuntion);

    function callBackFuntion(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var retVal = JSON.parse(answer);

        alert(answer);
        g_form.setValue("regions_email", retVal.email);
        g_form.setValue("regions_phone", retVal.phone);
    }

}

 

Regards

Suman P.

 

Hi @Community Alums, 

Please create an object in the script include. Instead, emailArr = [] create retObj = {} object and then validate once.

Thanks and Regards
Anshul

Rhodri
Tera Guru

In your script include you are storing the phone number in a separate array "emailArr1" which you are never returning to your client script, you are only returning "emailArr" - your email address array.

 

You might want to store both the email and phone number in an object, then push that into the one array.

 

(you have an obj variable defined already jsonObj, so you could store the phone number in jsonObj.phone and email in jsonObj.email then push that into the array)