2

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:

enter image description here

2 Answers 2

0

Just use a list in your Apex code. There's no need to serialize/deserialize.

@AuraEnabled(cacheable=true)
public static string WhoIsContact(string[] emails) {

And:

WhoIsContact({emails: arr} )
.then(result => {
    console.log('returning' + result);
    this.warning = result;
})
.catch(error => {
    console.log(error);

});

Also, why would you use a String when you're using a number?

@AuraEnabled(cacheable=true)
public static Integer WhoIsContact(string[] emails) {
    // .. snip .. //
    return emailSet.size();
}

The less you need to do with conversion, the easier it will be to maintain your code.

1
  • thank you sir, i need to try and get into the habit of keeping things simple
    – SavvyGoat
    Commented Dec 13, 2019 at 19:55
0

The biggest issue I see is that your email list is all prefixed with \n which makes those email addresses invalid. This would means your query only ever returns the 1 row as seen in your debug logs.

enter image description here

Without going through a massive series of tests, I would say simply removing the \n from the string would be hacky enough to work for you; however, it'd be worth figuring out where they are coming from (are you inserting your csv of emails on multiple lines in the text area?)

1
  • thanks @gNerb but this is the result of when you stringify an array in JavaScript
    – SavvyGoat
    Commented Dec 13, 2019 at 19:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .