+ Start a Discussion
AddaAdda 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY

I am recieving the following error when I try to deploy my apex class and test class in production.
Error message: TopicAssignmentTriggerHandlerTest.AssignmentTriggerHandlertest(), Details: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TriggeronTopicAssignment: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Class.TopicAssignmentTriggerHandler.sendEmailForTestimonial: line 84, column 1 Trigger.TriggeronTopicAssignment: line 11, column 1: [] Class.TopicAssignmentTriggerHandlerTest.AssignmentTriggerHandlertest: line 47, column 1

How I can fix it? I am attaching my code. Please help
Trigger:

trigger TriggeronTopicAssignment on TopicAssignment (after insert)
{    
    if(Trigger.isAfter && Trigger.isInsert) {
        TopicAssignmentTriggerHandler TopicAssignmentTriggerHandlerInstance = new TopicAssignmentTriggerHandler();
        TopicAssignmentTriggerHandlerInstance.sendEmailForTestimonial(Trigger.New);
    }
}

Apex Class:
/**
 **
 ** This helper class sends mail for the testimonial topic to certain user when any topic assignment on case is created
**/
public class TopicAssignmentTriggerHandler {

    
    public void sendEmailForTestimonial(List<TopicAssignment> topicAssignmentListNew) 
    {
    
        Map<Id, Topic> testimonialTopicIdsMappedTo = new Map<Id, Topic>([select Id,Name from Topic where (Name='Testimonial Feedback' OR 
        Name='Author Feedback' OR Name='Course Feedback' OR Name='General Feedback' OR Name='Course Request' OR Name='Feature Request' OR
        Name='Testimonial' OR Name='Community Relations' OR Name='Careers' OR Name='Legal' OR Name='Remove From Newsletter' OR Name='Piracy' 
        OR Name='PR' OR Name='Employment Verification')]);        
        System.debug('testimonialTopicIdsMappedTo'+testimonialTopicIdsMappedTo  );
        //Create a master list to hold the emails we'll send
        List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
        Map<Id, Id> caseIdMappedToContactId = new Map<Id, Id>();
        Map<Id, Id> caseIdMappedWithTopicAssignmentId = new Map<Id, Id>();
        Map<Id, Contact> contactIdMappedToRecord = new Map<Id, Contact>();
        Set<ID> caseIdSet = new Set<ID>();
       
        if(testimonialTopicIdsMappedTo.size() > 0 ) 
        {
         for(TopicAssignment topicAssignment :topicAssignmentListNew)
         {
          if(testimonialTopicIdsMappedTo.Keyset().contains(topicAssignment.TopicId) && topicAssignment.EntityKeyPrefix=='500') {
            caseIdSet.add(topicAssignment.EntityId);
             if(caseIdMappedWithTopicAssignmentId.keyset() != null && !caseIdMappedWithTopicAssignmentId.keyset().contains(topicAssignment.EntityId)) 
                     {
                         caseIdMappedWithTopicAssignmentId.put(topicAssignment.EntityId,topicAssignment.TopicId);
                     }                     
                }    
            }  
        }
        
        for(case caseRecordForContact : [select contactId, Id from case where ID In:caseIdSet and contactId != null]) 
        {
            caseIdMappedToContactId.put(caseRecordForContact.Id, caseRecordForContact.ContactId); 
        }
        
        for(Contact contactRecord: [select Id, Name, Email from contact where Id In:caseIdMappedToContactId.values()]) 
        {
           contactIdMappedToRecord.put(contactRecord.Id,contactRecord); 
        }
        
        for(Case caseRecord : [select Id, CaseNumber,Description,Subject,ContactId from Case where Id IN:caseIdSet]) 
        {
           contact contactRec = new contact();
           contactRec =contactIdMappedToRecord.get(caseRecord.ContactId);
           System.debug('contactRec'+contactRec);
           Id TopicId = caseIdMappedWithTopicAssignmentId.get(caseRecord.Id);
           Topic TopicRecord = testimonialTopicIdsMappedTo.get(TopicId);
           System.debug('TopicRecord '+TopicRecord);
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();    
           String caselink = System.URL.getSalesforceBaseUrl().toExternalForm() +'/' +caseRecord.id;
           //Set list of people who should get the email
           List<String> sendTo = new List<String>();
           List<String> EmailList= new List<String>();
           //Dummy contact to avoid sending mails to Contact attached to Case
           Contact con = new Contact();
           
           if(TopicRecord.Name.Contains('Testimonial') || TopicRecord.Name.Contains('Testimonial Feedback') || 
           TopicRecord.Name.Contains('Author Feedback') || TopicRecord.Name.Contains('Testimonial') || 
           TopicRecord.Name.Contains('Course Feedback') || TopicRecord.Name.Contains('General Feedback') || 
           TopicRecord.Name.Contains('Course Request') || TopicRecord.Name.Contains('Feature Request')  )
            {    system.debug('###############Topic record'+TopicRecord);   
                 if(TopicRecord.Name.Equals('Testimonial'))
                 {     
                     Testimonial_Topic_Assignment_EmailId__c testimonialEmailIdRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Testimonial');
                     String e=testimonialEmailIdRecord.EmailAddress__c;
                     EmailList=SplitEmail(e);
                     for(String s:EmailList)
                     {
                        sendTo.add(s);
                     }
                     System.debug('sendTo'+sendTo);
                  }
                 else 
                    {
                        Testimonial_Topic_Assignment_EmailId__c FeedbackRequestRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Feedback/Request');
                        String e=FeedbackRequestRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                 
                
                 //Set email contents - you can use variables!
            mail.setSubject(''+ TopicRecord.Name +' Topic Added To Case Number ' + caseRecord.CaseNumber + '');
            
            String body = 'Hi ,' + '<br/><br/>';
            
            body += 'Case topic has been added to the following case.'+'<br/><br/>';
            body += 'Topic Name : ' + TopicRecord.Name + '<br/>';
            body += 'Case Number : ' + caseRecord.CaseNumber + '<br/>';
            
            if(contactRec != null) 
            {
                body += '&nbsp;&nbsp;&nbsp;&nbsp;Case Contact Name : ' + (contactRec.Name==null?'':contactRec.Name) + '<br/>';  
                body += '&nbsp;&nbsp;&nbsp;&nbsp;Contact Email Id : ' + (contactRec.Email==null?'':contactRec.Email) + '<br/>'; 
            }
            
            
          //  body += '&nbsp;&nbsp;&nbsp;&nbsp;Case CaseNumber: ' + (caseRecord.CaseNumber==null?'':caseRecord.CaseNumber) + '<br/>'; 
            body += 'Case Subject : ' + (caseRecord.Subject==null?'':caseRecord.Subject) + '<br/>';
            body += 'Case Description : ' + (caseRecord.Description==null?'':caseRecord.Description) + '</br>';
            body += '<a href="' + caselink  + '">Case Link</a>'  + '<br/><br/>';
            
            
            body += 'Thanks,' + '</br>'; 
            body += 'Lynda.com';
            
             mail.setHtmlBody(body);
             mail.setToAddresses(sendTo);
             mails.add(mail);
            }
          
            else if(TopicRecord.Name.Contains('Community Relations') || TopicRecord.Name.Contains('Legal') 
            || TopicRecord.Name.Contains('Remove From Newsletter') || TopicRecord.Name.Contains('Piracy') 
            || TopicRecord.Name.Contains('PR') || TopicRecord.Name.Contains('Employment Verification') 
            || TopicRecord.Name.Contains('Careers') )
            {    system.debug('###############Topic record'+TopicRecord);    
                 List<Testimonial_Topic_Assignment_EmailId__c> AllEmailIdRecordList= Testimonial_Topic_Assignment_EmailId__c.getall().values();
                 
                 System.debug('AllEmailIdRecordList'+AllEmailIdRecordList);
                    if(TopicRecord.Name.Contains('Community Relations'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c communityRelationRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Community Relations');
                        String e=communityRelationRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Legal'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c LegalRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Legal');
                        String e=LegalRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Remove From Newsletter'))
                    {
                        System.debug('TopicRecord.Name'+TopicRecord.Name);
                        Testimonial_Topic_Assignment_EmailId__c RemovefromnewslettersRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Remove From Newsletter');
                        System.debug('RemovefromnewslettersRecord'+RemovefromnewslettersRecord);
                        String e=RemovefromnewslettersRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Piracy'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c PiracyRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Piracy');
                        String e=PiracyRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('PR'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c PRRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('PR');
                        String e=PRRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Employment Verification'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c EmploymentverificationRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Employment Verification');
                        String e=EmploymentverificationRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    else if(TopicRecord.Name.Contains('Careers'))
                    {
                        Testimonial_Topic_Assignment_EmailId__c CareersRecord= Testimonial_Topic_Assignment_EmailId__c.getvalues('Careers');
                        String e=CareersRecord.EmailAddress__c;
                        EmailList=SplitEmail(e);
                        System.debug('EmailList'+EmailList);
                        for(String s:EmailList)
                        {
                            sendTo.add(s);
                        }
                        System.debug('sendTo'+sendTo);
                    }
                    
                
                 mail.setToAddresses(sendTo);
                
            system.debug(sendTo);  
            //Set email contents
            mail.setSubject('Case Alert from Salesforce');   
            String body = 'Hello ,' + '<br/><br/>'; 
            body += 'Customer Service received the email below, and we wanted to send it your way for review. The case is now closed, but please let us know if we may be of assistance regarding this issue.'+'<br/><br/>' ;
            body += 'Thank you, and have a great day!'+'<br/><br/>'; 
            body += 'Best Regards,'+'<br/>';
            body += 'Customer Service Team '+'<br/><br/>';

            body += 'Topic Name : ' + TopicRecord.Name + '<br/>';
            body += 'Case Number : ' + caseRecord.CaseNumber + '<br/>';
            body += 'Email Subject : ' + caseRecord.Subject + '<br/>';
            body += 'Email Description : ' + caseRecord.Description + '<br/>';
            body += '<a href="' + caselink  + '">Case Link</a>'  + '<br/>';
                              
            mail.setHtmlBody(body);
            //Add your email to the master list
            mails.add(mail);  
        }
        system.debug('total Emails' + mails);
        
        }
        Messaging.sendEmail(mails);
        }
        public List<String> SplitEmail(String Emails){
        List<String> EmailIdList =Emails.split(',');
        System.debug('EmailIdList '+EmailIdList );
        return EmailIdList;
    }
    
}

Apex Test Class:
 
/**}
 ** @author JadeGlobal Inc
 ** @created 4-Feb/2015
 **
 ** Test class for TopicAssignmentTriggerHandler.cls
**/


  

@isTest(SeeAllData=true)
public class TopicAssignmentTriggerHandlerTest{

    public static testmethod void AssignmentTriggerHandlertest(){
        Test.startTest();
          
          List <Topic> TopicList=[Select Id,Name from Topic where (Name='Testimonial Feedback' OR Name='Author Feedback' OR Name='Course Feedback' OR Name='General Feedback' OR Name='Course Request' OR Name='Feature Request' OR Name='Testimonial' OR Name='Community Relations' OR Name='Careers' OR Name='Legal' OR Name='Remove From Newsletter' OR Name='Piracy' OR Name='PR' OR Name='Employment Verification')];          
          List<TopicAssignment> TopicAssignmentList=new List<TopicAssignment>();
          List<Case> CaseList=new List<Case>();
          
         //create test Account
          Account testAcc = new Account(
          Name = 'testAccount',
          BillingCountryCode = 'US',
          BillingState = 'California',
          BillingStreet = '6410 via Real',
          BillingPostalCode = '93013',
          BillingCity = 'Carpinteria',
          Industry = 'Construction',
          LDC_ID__c =  1234);
          insert testAcc ;
          System.Assertequals(testAcc.Name,'testAccount');
          
          //Create contact data
          Contact c1 = new Contact(AccountId = testAcc.id, FirstName = 'Test', LastName = 'Contact', Email='adda@gmail.com');
          c1.MailingState = 'California';
          c1.MailingCountry = 'United States';
          insert c1;
          System.Assertequals(c1.FirstName,'Test');
          System.Assertequals(c1.LastName,'Contact');
          
                   
          //create case
          Case testcas1 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas1;
          TopicAssignment ta1 = new TopicAssignment(Topicid = TopicList.get(0).id, EntityId = testcas1.id);
          insert ta1;
          
          Case testcas2 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas2;
          TopicAssignment ta2 = new TopicAssignment(Topicid = TopicList.get(1).id, EntityId = testcas2.id);
          insert ta2;
          
          Case testcas3 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas3;
          TopicAssignment ta3 = new TopicAssignment(Topicid = TopicList.get(2).id, EntityId = testcas3.id);
          insert ta3;
          
          Case testcas4 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas4;
          TopicAssignment ta4 = new TopicAssignment(Topicid = TopicList.get(3).id, EntityId = testcas4.id);
          insert ta4;
          
          Case testcas5 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas5;
          TopicAssignment ta5 = new TopicAssignment(Topicid = TopicList.get(4).id, EntityId = testcas5.id);
          insert ta5;
         
          Case testcas6 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas6;
          TopicAssignment ta6 = new TopicAssignment(Topicid = TopicList.get(5).id, EntityId = testcas6.id);
          insert ta6;
          
          Case testcas7 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas7;
          TopicAssignment ta7 = new TopicAssignment(Topicid = TopicList.get(6).id, EntityId = testcas7.id);
          insert ta7;
          
          Case testcas8 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas8;
          TopicAssignment ta8 = new TopicAssignment(Topicid = TopicList.get(7).id, EntityId = testcas8.id);
          insert ta8;
          
          Case testcas9 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas9 ;
          TopicAssignment ta9 = new TopicAssignment(Topicid = TopicList.get(8).id, EntityId = testcas9.id);
          insert ta9;
          
          Case testcas10 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas10 ;
          TopicAssignment ta10 = new TopicAssignment(Topicid = TopicList.get(9).id, EntityId = testcas10.id);
          insert ta10;
        
               
        
          
          
         Test.stopTest();
    }
}

 
sandeep sankhlasandeep sankhla
Hi Adda,

You can do a null check here...your method may be returning null..

EmailList=SplitEmail(e);

SplitEmail(e); method will be returning null and causing the issue..do a simply null check to avoid the error...

Thanks
Sandeep
AddaAdda
Sorry sandeep I didn't get that. Can you please reflect the null check in my code?

Thanks
Adda
sandeep sankhlasandeep sankhla
Hi

Can you let me know what reult you are getting in below debug
System.debug('EmailList'+EmailList);

thanks