+ Start a Discussion
Ayyagari RameshAyyagari Ramesh 

multiple if conditions to be executed. But the last condition is not executing

An email has to be triggered based on below code to but its not working. Something to do with last condition which I am going wrong.
Can anyone point out the approach to define multiple conditions
trigger CreditMemoTrigger on Credit_Memo__c (after update) 
{
    
    for(Credit_Memo__c SC : trigger.new)
    {
        Credit_Memo__c oldSC = Trigger.oldMap.get(SC.Id);
        if(SC.Send_Attachment__c == true &&
        (SC.Record_Approval__c == true || SC.Auto_Approved__c == true) &&
        (SC.Reason_Code_1__c == '1-Invoice Correction - Pricing Error - No impact on inventory'||
        SC.Reason_Code_2__c == '1-Invoice Correction - Pricing Error - No impact on inventory'||
        SC.Reason_Code_3__c == '1-Invoice Correction - Pricing Error - No impact on inventory'||
        SC.Reason_Code_4__c == '1-Invoice Correction - Pricing Error - No impact on inventory'||
        SC.Reason_Code_5__c == '1-Invoice Correction - Pricing Error - No impact on inventory'))
        {
            
            EmailTemplate et=[Select id,subject,body from EmailTemplate where name='KHI Credit Memo VF temp'];
            system.debug('et '+et);
            
            //Create email list
            List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();            
            
            //Create message
            Messaging.SingleEmailMessage singlemail = new Messaging.SingleEmailMessage();
            
            //add template
            singlemail.setTargetObjectId(SC.Id);
            singlemail.setTemplateId(et.Id);
            singlemail.setWhatId(SC.Id);
         
            //set target object for merge fields
            singlemail.setTargetObjectId(SC.OwnerId);
             
            //set to save as activity or not
            singlemail.setSaveAsActivity(false);
            
            //add address's that you are sending the email to
            List<String> sendTo = new List<String>();
            String owneremail = [select Email from User where Id = :SC.OwnerId].Email;
            sendTo.add(Label.KHI_Pricing_Email);
            sendTo.add(owneremail);
            // Step 4. Set email contents - you can use variables!
            //singlemail.setSubject('Attachments of Service Complaint-'+SC.Name+' Request by ');
            
            //set addresses
            singlemail.setToAddresses(sendTo);
            
            //add mail
            emails.add(singlemail);
        
            //Set email file attachments
         List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
         // List<Attachment> attList = [select Id, Name, Body, BodyLength, ContentType from Attachment where ParentId = : SC.Id];
         for (ContentDocumentLink a : [select Id, LinkedEntityId, ContentDocumentId from ContentDocumentLink where LinkedEntityId = : SC.Id])
          
     
         {
              for (ContentVersion b : [select id,FileType,FileExtension,contentBodyId,ContentLocation,PathOnClient,versionData, ContentUrl,Title from ContentVersion where ContentDocumentId= :a.ContentDocumentId])
              {
         // Add to attachment file list
         Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
         efa.setFileName(b.Title+ '.' + b.FileExtension);
         blob WOCFbody = b.versiondata;
         efa.setBody(WOCFbody);
         // efa.setContentType(b.PathOnClient);
         fileAttachments.add(efa);
              system.debug('SC.Attachment '+fileAttachments);
         }
         }
         singlemail.setFileAttachments(fileAttachments);

         //Send email
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{singlemail});
        } 
        
        Else if(SC.Send_Attachment__c == true &&
        (SC.Record_Approval__c == true || SC.Auto_Approved__c == true) &&
        (SC.Reason_Code_1__c == '6-Other'||
        SC.Reason_Code_2__c == '6-Other'||
        SC.Reason_Code_3__c == '6-Other'||
        SC.Reason_Code_4__c == '6-Other'||
        SC.Reason_Code_5__c == '6-Other'))
        {
            
            EmailTemplate et=[Select id,subject,body from EmailTemplate where name='KHI Credit Memo VF temp'];
            system.debug('et '+et);
            
            //Create email list
            List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();            
            
            //Create message
            Messaging.SingleEmailMessage singlemail = new Messaging.SingleEmailMessage();
            
            //add template
            singlemail.setTargetObjectId(SC.Id);
            singlemail.setTemplateId(et.Id);
            singlemail.setWhatId(SC.Id);
         
            //set target object for merge fields
            singlemail.setTargetObjectId(SC.OwnerId);
             
            //set to save as activity or not
            singlemail.setSaveAsActivity(false);
            
            //add address's that you are sending the email to
            List<String> sendTo = new List<String>();
            String owneremail = [select Email from User where Id = :SC.OwnerId].Email;
            sendTo.add(Label.KFI_Credit_Email);
            system.debug('KFI_Credit_Email ');
            sendTo.add(owneremail);
            // Step 4. Set email contents - you can use variables!
            //singlemail.setSubject('Attachments of Service Complaint-'+SC.Name+' Request by ');
            
            //set addresses
            singlemail.setToAddresses(sendTo);
            
            //add mail
            emails.add(singlemail);
        
            //Set email file attachments
         List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
         // List<Attachment> attList = [select Id, Name, Body, BodyLength, ContentType from Attachment where ParentId = : SC.Id];
         for (ContentDocumentLink a : [select Id, LinkedEntityId, ContentDocumentId from ContentDocumentLink where LinkedEntityId = : SC.Id])
          
     
         {
              for (ContentVersion b : [select id,FileType,FileExtension,contentBodyId,ContentLocation,PathOnClient,versionData, ContentUrl,Title from ContentVersion where ContentDocumentId= :a.ContentDocumentId])
              {
         // Add to attachment file list
         Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
         efa.setFileName(b.Title+ '.' + b.FileExtension);
         blob WOCFbody = b.versiondata;
         efa.setBody(WOCFbody);
         // efa.setContentType(b.PathOnClient);
         fileAttachments.add(efa);
              system.debug('SC.Attachment '+fileAttachments);
         }
         }
         singlemail.setFileAttachments(fileAttachments);

         //Send email
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{singlemail});
        }
        
        Else if(SC.Send_Attachment__c == true &&
        (SC.Record_Approval__c == true || SC.Auto_Approved__c == true) &&
        (SC.reason_check__c == true ))      
        {
            
            EmailTemplate et=[Select id,subject,body from EmailTemplate where name='KHI Credit Memo VF temp'];
            system.debug('et '+et);
            
            //Create email list
            List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();            
            
            //Create message
            Messaging.SingleEmailMessage singlemail = new Messaging.SingleEmailMessage();
            
            //add template
            singlemail.setTargetObjectId(SC.Id);
            singlemail.setTemplateId(et.Id);
            singlemail.setWhatId(SC.Id);
         
            //set target object for merge fields
            singlemail.setTargetObjectId(SC.OwnerId);
             
            //set to save as activity or not
            singlemail.setSaveAsActivity(false);
            
            //add address's that you are sending the email to
            List<String> sendTo = new List<String>();
            String owneremail = [select Email from User where Id = :SC.OwnerId].Email;
            sendTo.add(Label.KHI_Claims_Email);
            sendTo.add(owneremail);
            // Step 4. Set email contents - you can use variables!
            //singlemail.setSubject('Attachments of Service Complaint-'+SC.Name+' Request by ');
            
            //set addresses
            singlemail.setToAddresses(sendTo);
            
            //add mail
            emails.add(singlemail);
        
            //Set email file attachments
         List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
         // List<Attachment> attList = [select Id, Name, Body, BodyLength, ContentType from Attachment where ParentId = : SC.Id];
         for (ContentDocumentLink a : [select Id, LinkedEntityId, ContentDocumentId from ContentDocumentLink where LinkedEntityId = : SC.Id])
          
     
         {
              for (ContentVersion b : [select id,FileType,FileExtension,contentBodyId,ContentLocation,PathOnClient,versionData, ContentUrl,Title from ContentVersion where ContentDocumentId= :a.ContentDocumentId])
              {
         // Add to attachment file list
         Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
         efa.setFileName(b.Title+ '.' + b.FileExtension);
         blob WOCFbody = b.versiondata;
         efa.setBody(WOCFbody);
         // efa.setContentType(b.PathOnClient);
         fileAttachments.add(efa);
              system.debug('SC.Attachment '+fileAttachments);
         }
         }
         singlemail.setFileAttachments(fileAttachments);

         //Send email
            Messaging.sendEmail(new Messaging.SingleEmailMessage[]{singlemail});
        }
    
    }
}
AbhinavAbhinav (Salesforce Developers) 
Hi Ayyagari,

Could please be more specific with your issue as your trigger size too big to find the issue.

Thanks!