Error in fetching email ids in the code

SK41
Giga Guru

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!
4 REPLIES 4

Sonam_Tiwari
Kilo Sage

If this is double quote then initialize this properly and keep it out of the while loop

 

var getEmails = ''; // Initialize getEmails

 

 while (conRel.next()) {

getEmails += conRel.contact.email + ','; // Concatenate

 }

 

 

Consider indicating the response as helpful and marking it as correct if it meets your needs.

After doing the above, the undefined is coming at the start of the output now.

Below is the output coming: 

 

Sonam_Tiwari
Kilo Sage

Interesting.


 

The undefined value can also come if the contact.email doesn't have a value or not defined for some records in the  sn_customerservice_contact_relationship table, concatenating it directly if it doesn't exist may also lead to undefined being appended.

 

Can you check that?

 

 

Consider indicating the response as helpful and marking it as correct if it meets your needs.

Mohith Devatte
Tera Sage
Tera Sage

Hello @SK41 ,

try using the below code .

Instead of appending it in a string or concatenating it in a string i used an array and tried to query it .

Also please check  conRel.contact.email has a value in the target table which is resulting in undefined value for a particular record.

 



(function execute(inputs, outputs) {
var getEmails =[];
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())
        {
getEmails.push(conRel.contact.email.toString());

   
    gs.info("emailaddresses" + getEmails );
     }
}
}

Hope this helps 

Mark the answer correct if this helps you 

Thanks