You need to sign in to do that
Don't have an account?
Timothy Smith
I get 100% test coverage but receiving error on setup method:
`System.NullPointerException: Attempt to de-reference a null object
(TestCasehandlerAlert.setup: line 17, column 1)`
The email address fields are formula fields. The formula fields are `Client_Advisor__r.Email` and `Resource_Coordinator__r.Email.` The trigger will send an email message if an account has 8 cases or more created within 7 days. If the value of any of three specified fields is "Live - Closed Project", the email is sent to a single email address. If any of the three fields is equal to "Live - Closed Project", the Else block is fired and email is sent to two other email addresses.
I am trying to create two types of projects; one will run through the IF statement, the other will run through the Else statement.
Trigger:
Test Class:
Test Case - Attempt to de-reference a null object (Formula Field)
I get 100% test coverage but receiving error on setup method:
`System.NullPointerException: Attempt to de-reference a null object
(TestCasehandlerAlert.setup: line 17, column 1)`
The email address fields are formula fields. The formula fields are `Client_Advisor__r.Email` and `Resource_Coordinator__r.Email.` The trigger will send an email message if an account has 8 cases or more created within 7 days. If the value of any of three specified fields is "Live - Closed Project", the email is sent to a single email address. If any of the three fields is equal to "Live - Closed Project", the Else block is fired and email is sent to two other email addresses.
I am trying to create two types of projects; one will run through the IF statement, the other will run through the Else statement.
Trigger:
trigger CaseHandlerCountAlert on Case (after insert) { //Case trigger that will send email alert when 8 cases are created within 7 days. String messageToSend; List <String> ListOfMessages = new List <String>(); Set <Id> AcctIds = new Set <Id>(); String messageBody; List < AggregateResult > AggregateResultList = [SELECT AccountId, Account.Name name, COUNT(Id) co FROM Case WHERE CreatedDate = LAST_N_DAYS:7 AND Id IN :Trigger.New GROUP BY AccountId, Account.Name HAVING COUNT(Id) >= 8 ]; Map < Id, String > accountIdEmailmessageMap = new Map < Id, String > (); for (AggregateResult aggr: AggregateResultList) { String messageToSend = 'Account name: ' + aggr.get('name') + ' has ' + (Integer) aggr.get('co') + ' cases opened in the last 8 days.'; Id accId = (Id) aggr.get('AccountId'); accountIdEmailmessageMap.put(accId, messageToSend); AcctIds.add(accId); } List < Case > caseList = [SELECT Id, AccountId, Account.Name, Parent_Project_if_applicable__r.Implementation_status__c, Parent_Project_if_applicable__r.PM_Implementation_Status__c, Parent_Project_if_applicable__r.RCM_Implementation_Status__c, Parent_Project_if_applicable__r.Resource_Coordinator_Email__c, Parent_Project_if_applicable__r.Client_Advisor_Email__c FROM Case WHERE AccountId IN: AcctIds]; List<Messaging.SingleEmailMessage> lstASingleEmailMessage = new List<Messaging.SingleEmailMessage>(); List<Messaging.SingleEmailMessage> lstBSingleEmailMessage = new List<Messaging.SingleEmailMessage>(); for (Case cl: caseList) { if (cl.Parent_Project_if_applicable__r.Implementation_status__c == 'Live - Closed Project' || cl.Parent_Project_if_applicable__r.PM_Implementation_Status__c == 'Live - Closed Project' || cl.Parent_Project_If_Applicable__r.RCM_Implementation_Status__c == 'Live - Closed Project') { String messageBody = accountIdEmailmessageMap.get(cl.AccountId); List<String> emailaddr = new List<String>(); emailaddr.add('CustomerSuccessManagers@test.com'); Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setSenderDisplayName('Support'); mail.setToAddresses(emailaddr); mail.Subject = 'Multiple cases created alert message'; mail.setPlainTextBody(messageBody); lstASingleEmailMessage.add(mail); }else{ String messageBody = accountIdEmailmessageMap.get(cl.AccountId); List<String> emailAdds = new List<String>(); emailAdds.add(cl.Parent_Project_if_applicable__r.Resource_Coordinator_Email__c); emailAdds.add(cl.Parent_Project_if_applicable__r.Client_Advisor_Email__c); Messaging.SingleEmailMessage amail = new Messaging.SingleEmailMessage(); amail.SetSenderDisplayName('Support'); amail.setToAddresses(emailAdds); amail.Subject = 'Multiple cases created alert message'; amail.setPlainTextBody(messageBody); lstBSingleEmailMessage.add(amail); System.debug('messageBody: ' + messageBody); System.debug('email message: ' + amail); } } Messaging.SendEmailResult[] r = Messaging.sendEmail(lstASingleEmailMessage); Messaging.SendEmailResult[] rb = Messaging.sendEmail(lstBSingleEmailMessage); }
Test Class:
@isTest private class TestCaseHandlerAlert { @isTest static void setup(){ Account a = new Account(); Account b = new Account(); a.name = 'testacct21'; b.name = 'testacct31'; // Create Milestone Milestone1_Project__c project = new Milestone1_Project__c(); project.Name = 'triggertest21'; project.RCM_Implementation_Status__c = 'RETURNED SOFTWARE'; project.PM_Implementation_Status__c= 'RETURNED SOFTWARE'; project.Implementation_Status__c= 'RETURNED SOFTWARE'; Milestone1_Project__c project1 = new Milestone1_Project__c(); //project.RecordTypeId = '01214000001RYp2AAG'; project1.Name = 'triggertest31'; project1.RCM_Implementation_Status__c = 'LIVE - CLOSED PROJECT'; project1.PM_Implementation_Status__c= 'RETURNED SOFTWARE'; project1.Implementation_Status__c= 'RETURNED SOFTWARE'; //Insert Account insert a; insert b; //Insert Project insert project; insert project1; //Create 10 Cases associated with Project List<Case> casesToInsert = new List<Case>(); List<Case> casesToInsert1 = new List<Case>(); for (Integer i=1; i<12; i++){ Case c1 = new Case(); c1.AccountId = b.id; c1.Origin = 'Phone'; c1.Impact__c = 'Low'; c1.Severity__c = 'Minor'; c1.Type = 'Bridge'; c1.Parent_Project_if_applicable__c = project.id; casesToInsert1.add(c1); } for (Integer i=1; i<12; i++){ Case c = new Case(); c.AccountId = a.id; c.Origin = 'Phone'; c.Impact__c = 'Low'; c.Severity__c = 'Minor'; c.Type = 'Bridge'; c.Parent_Project_if_applicable__c = project1.id; casesToInsert.add(c); } insert casesToInsert; insert casesToInsert1; } @isTest static void DontSendNotLimit (){ // Do not send, not enough Cases need 8 List<Case> testCases = [SELECT Id FROM Case WHERE Account.Name='testacct21' LIMIT 5]; List<Case> insertCases = new List<Case>(); Test.startTest(); insert insertCases; System.debug(Limits.getEmailInvocations()); Test.stopTest(); } @isTest static void SendElseBlock (){ // Else BLock Fires - 2 emails sent List<Case> testCases = [SELECT Id FROM Case WHERE Account.Name='testacct21' LIMIT 8]; List<Case> insertCases = new List<Case>(); Test.startTest(); insert insertCases; System.debug(Limits.getEmailInvocations()); Test.stopTest(); } @isTest static void SendRCMiFBlock (){ // Fires the If BLock 1/3 List<Case> testCases = [SELECT Id FROM Case WHERE Account.Name='testacct31' LIMIT 8]; List<Case> insertCases = new List<Case>(); for(Case cas :testCases ){ insertCases.add(cas); } Test.startTest(); insert insertCases; Test.stopTest(); } @isTest static void SendPMiFBlock (){ // Fires the If BLock 2/3 List<Case> testCases = [SELECT Id FROM Case WHERE Account.Name='testacct31' LIMIT 8]; List<Case> insertCases = new List<Case>(); for(Case cas :testCases ){ cas.Parent_Project_if_applicable__r.PM_Implementation_Status__c = 'LIVE - CLOSED PROJECT'; insertCases.add(cas); } Test.startTest(); insert insertCases; Test.stopTest(); } @isTest static void SendIMPiFBlock (){ // Fires the If BLock 3/3 List<Case> testCases = [SELECT Id FROM Case WHERE Account.Name='testacct31' LIMIT 8]; List<Case> insertCases = new List<Case>(); for(Case cas :testCases ){ cas.Parent_Project_if_applicable__r.Implementation_Status__c= 'LIVE - CLOSED PROJECT'; insertCases.add(cas); } Test.startTest(); insert insertCases; Test.stopTest(); } }