Error in fetching the email ids in the code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2024 04:13 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 06:34 AM
Hi @skt19
Can you check whether there is any record in sn_customerservice_contact_relationship with empty contact?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-12-2024 07:19 AM
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.