Error in fetching email ids in the code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2024 04:19 AM - edited ‎02-12-2024 04:29 AM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2024 04:24 AM - edited ‎02-12-2024 04:27 AM
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
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2024 04:36 AM
After doing the above, the undefined is coming at the start of the output now.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2024 04:48 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2024 05:37 AM
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