I've created a simple lightning web component that i want to use to check if an email already exists based on a comma delimited set of values. But it's not treating my array as separate items in a list. It's just querying it using the first email and that's all. It never checks the others and i can't figure out why.
Here is the HTML:
<template>
<lightning-card title="Contact Email Checker" icon-name="standard:contact">
<p class="slds-p-left_small">
<lightning-textarea name="emails"
label="Copy and paste comma delimeted email addresses"
message-when-value-missing="This field is required."
required>
</lightning-textarea>
</p>
<lightning-button variant="brand" label="Validate" title="Check Email addresses" onclick={handleClick} class="slds-m-left_x-small"></lightning-button>
<div slot="footer">
<p class="slds-p-right_small">{warning}</p>
</div>
</lightning-card>
Here is my js:
/* eslint-disable no-unused-vars */
/* eslint-disable no-console */
import { LightningElement, track } from 'lwc';
import WhoIsContact from @salesforce/apex/ContactCheckCTRL.WhoIsContact';
export default class ContactEmailChecker extends LightningElement {
@track warning;
@track emails;
@track tr;
handleClick(event) {
var inp=this.template.querySelector("lightning-textarea");
var arr = [];
this.warning = '';
if (!inp.value.includes(',')){
this.warning = "Need a comma between emails";
}
arr = inp.value.split(',');
arr = arr.filter(n => n);
console.log(JSON.stringify(arr));
WhoIsContact({emails: JSON.stringify(this.arr)} )
.then(result => {
console.log('returning' + result);
this.warning = result;
})
.catch(error => {
console.log(error);
});
//}
}
}
Here is the Apex Class:
public with sharing class ContactCheckCTRL{
@AuraEnabled
public static string WhoIsContact(string emails) {
List<string> strList = (List<string>) JSON.deserialize(emails, List<string>.class);
Set<string> strEmailSet = new set<string>(strList);
List<string> emailList = new List<string>();
Set<string> emailSet = new Set<string>(strList);
system.debug('>>Main List ' + strList);
List<AggregateResult> groupedResults = [Select COUNT(Id),Email FROM Contact WHERE Email IN: strEmailSet
GROUP BY Email HAVING COUNT(Id) >= 1];
System.debug('>>>'+groupedResults);
emailList.clear();
for(AggregateResult ar : groupedResults){
system.debug((string)ar.get('Email'));
emailList.add((string)ar.get('Email'));
}
emailSet.removeAll(emailList);
//do a call out to the the rest of the details and start creating them
system.debug('>>Filtered list' + emailset);
return string.valueOf(emailset.size());
}
}
Here are the results from debugger: