1

I have written a trigger to send email to particular email groups whenever a "Idea" record is created. I have to send emails to groups on the basis of combination of two multi-select picklist fields so I have added those combinations in "Custom Setting Data" and using it to fetch the email id's. Below is my code.

Public Class IdeaTriggerOperations1
{
    Public Static Void NotifyBUonNewDiscussion1(List<Idea> IdeaIds)
    { 
        Set<String> CategoryNameSet = new Set<String>();
        Set<String> ProductNameSet = new Set<String>();

        //Set and clear emailAddresses
        String[] emailAddressess = new String[40];
        emailAddressess.clear();

        //Set Messaging
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        Messaging.reserveSingleEmailCapacity(1);

        //Get Email Template Id
        EmailTemplate et = [SELECT Id FROM EmailTemplate WHERE developerName = 'Idea_New_Discussion_Posted_BU'];


        For(Idea I : IdeaIds)
        {       
            String completeEmailList = '';                                  
            for(String str : I.Categories.split(';'))
            {
                CategoryNameSet.add(str);
                system.debug('-----------CategoryNameSet--------'+CategoryNameSet);
            } 
            If(I.Product__c != NULL)
            {
                for(String str1 : I.Product__c.split(';'))
                {
                    ProductNameSet.add(str1);
                    system.debug('-----------ProductNameSet--------'+ProductNameSet);
                }
            }
            Else{
                ProductNameSet = NULL; }

            // Generating Email List values for BU ... 
            If(i.Include_BU__c == True)
            { 
                If(!ProductNameSet.contains('Automata') && !ProductNameSet.contains('DRAM') && !ProductNameSet.contains('eMCP') && !ProductNameSet.contains('eMMC') && !ProductNameSet.contains('HMC') && !ProductNameSet.contains('MCP') && !ProductNameSet.contains('NAND') && !ProductNameSet.contains('NOR') && !ProductNameSet.contains('SSD') && !ProductNameSet.contains('N/A'))
                    {
                        Idea_Custom_Setting__c nc = Idea_Custom_Setting__c.getValues('Product All');
                        completeEmailList += nc.BU_Email_Id__c+ ';' ;
                    }

                List<Idea_Custom_Setting__c> IdeaEmails = Idea_Custom_Setting__c.getall().values();
                for(Idea_Custom_Setting__c m : IdeaEmails)
                {
                    If(CategoryNameSet.contains(m.Category__c) && (ProductNameSet.contains(m.Product__c)||ProductNameSet==NULL))
                    {
                        completeEmailList += m.BU_Email_Id__c + ';' ;
                    } 
                    If(CategoryNameSet.contains('Automotive') || CategoryNameSet.contains('Connected Home') || CategoryNameSet.contains('Consumer') || CategoryNameSet.contains('IMM'))
                    If(ProductNameSet.contains(m.Combined_Category__c))
                    {
                        completeEmailList += m.BU_Email_Id__c + ';' ;
                    }               
                }  
            }//End of first IF statement

            // Generating Email List values for SME ...
            If( i.LastModifiedDate == i.CreatedDate)
            {         
                List<Idea_Custom_Setting_For_SME__c> SMECustSet = Idea_Custom_Setting_For_SME__c.getall().values();
                for(Idea_Custom_Setting_For_SME__c  N : SMECustSet)
                {
                    If(CategoryNameSet.contains(N.Category_SME__c)){
                    If(ProductNameSet.contains('N/A'))
                    {
                        completeEmailList += N.SME_Email_Id__c + ';' ;
                    }   }                 
                    If(CategoryNameSet!=NULL && ProductNameSet.contains(N.Product_SME__c))
                    {
                        completeEmailList += N.SME_Email_Id__c + ';' ;
                    }
                    If(!ProductNameSet.contains('Automata') && !ProductNameSet.contains('DRAM') && !ProductNameSet.contains('eMCP') && !ProductNameSet.contains('eMMC') && !ProductNameSet.contains('HMC') && !ProductNameSet.contains('MCP') && !ProductNameSet.contains('NAND') && !ProductNameSet.contains('NOR') && !ProductNameSet.contains('SSD') && !ProductNameSet.contains('N/A')){
                    If(ProductNameSet.contains(N.Special_Value__c))
                    {
                        completeEmailList += N.SME_Email_Id__c + ';' ;
                    }}
                }
            }

           system.debug('-----------completeEmailList--------'+completeEmailList);
           for(String str2 : completeEmailList.split(';'))
           {
               emailAddressess.add(str2);
           }

            //Set Message Properties
            mail.setTargetObjectId(i.CreatedById);
            mail.setWhatId(i.Id);
            mail.setTemplateId(et.Id);
            mail.setSenderDisplayName('SFDC Technical Discussion');
            mail.setToAddresses(emailAddressess);
            mail.setUseSignature(false);
            mail.setBccSender(false);
            mail.setSaveAsActivity(false);

            // Update the Post to set the SME Notified field to TRUE
           If(i.Include_BU__c == True){
           List<Idea> nIdea = [SELECT Id, BU_Notified__c FROM Idea WHERE Id IN: Trigger.newMap.keySet()];

                for (Idea o :nIdea){
                    o.BU_Notified__c = True;
                }

                update nIdea; } 

        }
        // Send the email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

When I create an Idea record with my test class I always get error saying :

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, IdeaTrigger1: execution of AfterInsert

caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Invalid to address : : []

Class.IdeaTriggerOperations1.NotifyBUonNewDiscussion1: line 114, column 1 Trigger.IdeaTrigger1: line 9, column 1: []

0

To start with, you are creating your email address list with 40 empty records then adding more so:

change

String[] emailAddressess = new String[40];

to

String[] emailAddressess = new String[]{};

As long as you have proper email addresses elsewhere and the rest of your code works this will fix the issue in your question.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.