function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Khalid Khan 28Khalid Khan 28 

Process Builder Issue on Bounced Email

Hi Support,

I am having an issue with process builder rule.

My process builder rule is calling the apex class on the basis of stages on opportunity and  sends email alerts and create task but it is breaking due to bounced email. 

Can you please suggest me how can I fix it so that it can neglect the bounced email and send email notifications and create task.

 @InvocableMethod
    public static void SendEmail(List<ID> opportunityId)
    {
    /* Addeed try and catch block by Zumzum khalid as per case# 00009569 */
       //try
        //{
            runningInASandbox();
            
            
            system.debug('SEnd Email'+opportunityId);
            
            for(Opportunity Opp : [Select id , createdDate,AccountId, stageName , RecordType.Name ,ownerId, Student_Email_Workflow__c 
                                    from Opportunity where Id IN: OpportunityId AND stageName IN: OppStages])
            {                        
                //if(Opp.CreatedDate >= Date.newInstance(2016, 2, 26) )
                //{
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    String[] toAddresses = new String[]{Opp.Student_Email_Workflow__c};
                    //mail.setReplyTo('SMaster@gmail.com');
                    mail.setToAddresses(toAddresses);
                    mail.setSenderDisplayName('Qobolak Support');
                    mail.setBccSender(false);
                    //mail.setUseSignature(false);
                    Id conId = getPrimaryContact(Opp.Id , Opp.AccountId);
                    mail.setTargetObjectId(conId);
                    mail.setWhatId(Opp.Id);
                    
                    Id TemplateId = getTemplateID(Opp.StageName , Opp.RecordType.Name);
                    
                    EmailTemplate et = GetTemplate(TemplateId);
                        
                    if(conId == null)
                    {
                        Map<string,string> results = ResolveTemplate(mail,TemplateId , Opp.Student_Email_Workflow__c);
                        
                        mail.setHtmlBody(results.get('html'));
                        mail.setPlainTextBody(results.get('plain'));
                        mail.setSubject(results.get('subject'));
                    }
                    else
                    {
                        mail.setTemplateId(TemplateId );
                    }
                    mail.saveAsActivity = false;    
                    
                    //Set email file attachments
                    system.debug('Get File Name: '+getFileType(Opp.StageName));
                    List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
                    
                    List<Id> ContentIds = new List<Id>();
                
                    for(OpportunityFeed  opfd : [Select RelatedRecordId From OpportunityFeed where ParentId =: opportunityId])
                    {
                        ContentIds.add(opfd.RelatedRecordId);
                    }
                    
                    for(ContentVersion  cv: [Select VersionNumber, Title, VersionData ,Attachment_Type__c , Id, FileType, Description, 
                                            ContentDocumentId ,  ContentDocument.ParentId
                                            From ContentVersion
                                            where Id IN: ContentIds  AND 
                                            Attachment_Type__c IN:getFileType(Opp.StageName) ])
                    {
                        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                                
                        //efa.setContentType(a.ContentType);
                        efa.setFileName(cv.Title);
                        efa.setBody(cv.VersionData);
                        efa.setinline(false);
                        
                        fileAttachments.add(efa);
                    } 
                    
                    
                    /*for (Attachment a : [select Id, Name, Body, BodyLength , ContentType
                                        from Attachment 
                                        where ParentId IN: opportunityId ])//AND Name IN: getFileName(Opp.StageName)])
                    {
                        system.debug('in attachment: ');
                        
                        // Add to attachment file list
                        
                        for(string s: getFileType(Opp.StageName))
                        {
                            if(a.Name.startsWithIgnoreCase(s))
                            {
                                Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                                
                                efa.setContentType(a.ContentType);
                                efa.setFileName(a.Name);
                                efa.setBody(a.Body);
                                efa.setinline(false);
                                
                                fileAttachments.add(efa);
                            }
                        }
                    }*/
                    if(fileAttachments.size() >0)
                    {
                        mail.setFileAttachments(fileAttachments);
                        //Send email
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                        
                        //create Task
                        Task t = new Task (Status = 'Completed', Subject = 'Email Alert: '+et.Name, Priority = 'Normal',ActivityDate = system.today(),
                                            OwnerId = Opp.OwnerId , WhatId = Opp.Id );
                        insert t;
                    }
                    
                }
            //}//end date condition  
       // }
        
       /* catch (exception e)
        {
            e.getMessage();
        }*/
        
        
    }


Regards,
Khalid
 
snehal surti 3snehal surti 3
When you send the email it returns the object of type SendEmailResult. This object has methods to geterrors and return whether email send was successful or failure.You can give a try and see if it works.
 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_sendemailresult.htm

You should also enable email bounce management which might also resolve your problem..here is link to enable bounce management

https://help.salesforce.com/HTViewHelpDoc?id=emailadmin_deliverability.htm