0

I have the following code which sends email to the list of recipients by click of a button. It pulls the recipients based on the parameters defined in the UI. What is happening is that it is sending duplicate emails even though debug is it returning two User Id (I was testing only with two recipients). I am one of the recipient and with other collegue. I am always receiving email two times on the click of the button. What am I doing wrong here?

public PageReference sendEmail(){
    System.debug('BRP Id is'+theController.getId());
    Business_Rule_Processor__c brp = [SELECT Id,Email_Subject__c,Email_Template__c,Input_Text1__c,Email_From_Address__c,LOB__c,Email_Bcc_Address__c,Email_Cc_Address__c FROM Business_Rule_Processor__c WHERE Id =: theController.getId()];
    fromAddr = brp.Email_From_Address__c;
    brpId = brp.Id;
    emailTemplate = brp.Email_Template__c;
    lob = brp.LOB__c.split(';');
    System.debug('LOB vlaue'+lob);

    List<Messaging.SingleEmailMessage> sfEmailAlert = new List<Messaging.SingleEmailMessage>();
    Messaging.SingleEmailMessage emailAlert = new Messaging.SingleEmailMessage();
    List<Messaging.Emailfileattachment> efaList = new List<Messaging.Emailfileattachment>();


    if(brp.Email_Cc_Address__c != null){
        cc.add(brp.Email_Cc_Address__c);
    }

    if(fromAddr != null){
        owe = [SELECT ID,DisplayName,Address FROM OrgWideEmailAddress WHERE DisplayName =: fromAddr];
        System.debug('Owe Id is' +owe.Id);
    }
    if(emailTemplate !=null){
        et = [SELECT Id, Subject, HtmlValue, Body from EmailTemplate WHERE Name =: brp.Email_Template__c];
        System.debug('Template Id is' +et.Id);
    }
    numAtt = [Select count() From Attachment where ParentId =: brpId];
    emailAtt = [Select Id,Body,Name,ContentType From Attachment where ParentId =: brpId];
    if(numAtt >0){
        for(Integer i=0; i< numAtt; i++){
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
            efa.setFileName(emailAtt[i].Name);
            efa.setBody(emailAtt[i].Body);
            efa.setContentType(emailAtt[i].ContentType);
            efaList.add(efa);
        }
        uploadDocumentToContent();
    }
    if(lob.get(0) == 'ALL'){
        for( User  targetUsers :[SELECT Id FROM User WHERE IsActive = true AND  UserType NOT IN('CsnOnly')]){
            emailAlert.setOrgWideEmailAddressId(owe.ID);
            emailAlert.setSaveAsActivity(false);
            emailAlert.setTargetObjectId(targetUsers.Id);
            emailAlert.setCcAddresses(cc);
            emailAlert.setBccAddresses(bcc);
            emailAlert.setTemplateId(et.Id);
            emailAlert.setSubject(brp.Email_Subject__c);
            if(efaList.size()>0){
                emailAlert.setFileAttachments(efaList);
            }
            // process the merge fields
            String subject = et.Subject;
            subject = subject.replace('{!Business_Rule_Processor__c.Email_Subject__c}', brp.Email_Subject__c);
            String htmlBody = et.HtmlValue;
            System.debug('htmlbody is' +htmlBody);
            htmlBody = htmlBody.replace('{!Business_Rule_Processor__c.Input_Text1__c}', brp.Input_Text1__c);
            String plainBody = et.Body;
            plainBody = plainBody.replace('{!Business_Rule_Processor__c.Input_Text1__c}', brp.Input_Text1__c);

            emailAlert.setSubject(subject);
            emailAlert.setHtmlBody(htmlBody);
            emailAlert.setPlainTextBody(plainBody);
            sfEmailAlert.add(emailAlert);
            System.debug('Email Alert is' +sfEmailAlert);
            System.debug('User Id is>>>>'+targetUsers.Id); 
        }
    }else{
        for(User targetUsers : [SELECT Id FROM User WHERE IsActive = true AND  UserType NOT IN('CsnOnly') AND Division IN : lob]){
            emailAlert.setOrgWideEmailAddressId(owe.ID);
            emailAlert.setSaveAsActivity(false);
            emailAlert.setTargetObjectId(targetUsers.Id);
            emailAlert.setCcAddresses(cc);
            emailAlert.setBccAddresses(bcc);
            emailAlert.setTemplateId(et.Id);
            emailAlert.setSubject(brp.Email_Subject__c);
            if(efaList.size()>0){
                emailAlert.setFileAttachments(efaList);
            }
            // process the merge fields
            String subject = et.Subject;
            subject = subject.replace('{!Business_Rule_Processor__c.Email_Subject__c}', brp.Email_Subject__c);
            String htmlBody = et.HtmlValue;
            System.debug('htmlbody is' +htmlBody);
            htmlBody = htmlBody.replace('{!Business_Rule_Processor__c.Input_Text1__c}', brp.Input_Text1__c);
            String plainBody = et.Body;
            plainBody = plainBody.replace('{!Business_Rule_Processor__c.Input_Text1__c}', brp.Input_Text1__c);

            emailAlert.setSubject(subject);
            emailAlert.setHtmlBody(htmlBody);
            emailAlert.setPlainTextBody(plainBody);
            sfEmailAlert.add(emailAlert);
            System.debug('Email Alert is' +sfEmailAlert);
            System.debug('User Id is>>>>'+targetUsers.Id);   
        }
    }
    Messaging.sendEmail(sfEmailAlert,false);
    System.debug('You have made ' + Limits.getEmailInvocations() + ' email calls out of ' + Limits.getLimitEmailInvocations() + ' allowed');
    PageReference pageRef = new PageReference('/' + theController.getId());
    pageRef.setRedirect(true);
    return pageRef;
}

enter image description here

3
  • But I should receiving only one email as I am one of the user. I am getting two email instead.
    – user28452
    Commented Mar 17, 2017 at 8:41
  • Yes sorry. Could it because of the cc and bcc addresses? Commented Mar 17, 2017 at 9:20
  • Both have null value during my testing.
    – user28452
    Commented Mar 17, 2017 at 10:24

1 Answer 1

0

I had submitted a case with Salesforce which got escalated to Tier 1 and no issues found with the code or on the server logs. I refreshed the dev sandbox and tried with same code and I am not seeing duplicate emails. I am not sure what is the root cause but refreshing the sandbox helped.

You must log in to answer this question.

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