Error in fetching the email ids in the code

skt19
Giga Explorer

Hi,

I have a requirement where I have to fetch all the email ids of the customer type "broker". I am able to achieve this requirement but only issue is that it appends undefined at the end of all the emails fetched. Below is the code to fetch email ids:

(function execute(inputs, outputs) {
var emailto = current.EmailTo;
var clientid = current.ClientID;
var getAcc= new GlideRecord('customer_account')
getAcc.addQuery('entity_type',emailto);

if(emailto == 'Broker' )
{  
    getAcc.addQuery('x_acal_orchestra_client_mn',clientid);
  
    getAcc.query();
    if(getAcc.next())
    {  
var conRel = new GlideRecord('sn_customerservice_contact_relationship');
        conRel.addQuery('company',getAcc.sys_id);
        conRel.query();
        while(conRel.next())
        {
var getEmails='';
getEmails= conRel.contact.email+ ',' + getEmails;
   
    gs.info("emailaddresses" + getEmails );
     }
}
}
 
Below is the output coming: 
 
All the email ids are coming but at the end undefined is also coming, not sure why.
Kindly, assist!
Thanks!
2 REPLIES 2

Sarika S Nair1
Kilo Sage

Hi @skt19 

Can you check whether there is  any record in sn_customerservice_contact_relationship with empty contact?

Maddysunil
Kilo Sage

@skt19 

It looks like the issue is related to how you are concatenating the email addresses in the getEmails variable. It seems that you are adding the email address and a comma before the existing value of getEmails, which results in an extra comma at the beginning of the string, and ultimately, an 'undefined' at the end.

You can use below code:

 

(function execute(inputs, outputs) {
    var emailto = current.EmailTo;
    var clientid = current.ClientID;
    var getAcc = new GlideRecord('customer_account');
    getAcc.addQuery('entity_type', emailto);

    if (emailto == 'Broker') {  
        getAcc.addQuery('x_acal_orchestra_client_mn', clientid);
        getAcc.query();

        if (getAcc.next()) {  
            var conRel = new GlideRecord('sn_customerservice_contact_relationship');
            conRel.addQuery('company', getAcc.sys_id);
            conRel.query();
            
            // Initialize getEmails as an empty string
            var getEmails = '';

            while (conRel.next()) {
                // Concatenate email addresses without the extra comma
                getEmails += conRel.contact.email + ',';
            }

            // Remove the trailing comma
            getEmails = getEmails.replace(/,$/, '');

            gs.info("emailaddresses: " + getEmails);
        }
    }
})(inputs, outputs);

 

 

Please mark my answer correct/helpful if it helps you.