You need to sign in to do that
Don't have an account?
Mike Reynolds 6
Creating a test class for trigger that updates case with emailMessge data after case insert
Hey,
I have a simple trigger that puts basic emailMessage data on the related parent case after the parent case is inserted. See the relevant trigger snippet below:
The trigger is great, but I can't get good code coverage because I need to insert the case with a related emailMessage record and I have no idea how to make that happen. Here's what I have now:
I know that if I create the case first, i can populate the emailMessage.ParentId as the newCase.id, but at that point, the trigger will have already fired because the case has already been inserted. Does anyone know how to create the child emailMessage first, and then associate it to the case at insert? Any help will be greatly appreciated!
Thanks, Mike.
I have a simple trigger that puts basic emailMessage data on the related parent case after the parent case is inserted. See the relevant trigger snippet below:
if (Trigger.isAfter) { List<case> cases=new List<Case>(); List<EmailMessage> email=[select ToAddress, FromAddress, CcAddress, FromName from EmailMessage where ParentId IN : trigger.newMap.keyset() and Incoming = true and Parent.Number_of_Messages_Received__c = 0]; map<Id,EmailMessage> mapCaseId_Email = new map<Id,EmailMessage>(); for(EmailMessage objEmail : email){ mapCaseId_Email.put(objEmail.ParentId, objEmail); } if(!mapCaseId_Email.isEmpty()){ for(Case c:Trigger.new){ if(mapCaseId_Email.containsKey(c.Id)){} c.Email_To_Address__c=email[0].ToAddress; c.From_Address__c=email[0].FromAddress; c.Email_CC_Address__c=email[0].CcAddress; c.Email_From_Name__c=email[0].FromName; cases.add(c); } if(cases.size()>0){ update cases; } } }
The trigger is great, but I can't get good code coverage because I need to insert the case with a related emailMessage record and I have no idea how to make that happen. Here's what I have now:
@isTest private class caseTriggerAllTestIsAfter { static testMethod void isAfterCaseTest(){ case newCase = new case (); newCase.Email_CC_Address__c = ''; newCase.Email_From_Name__c = ''; newCase.Email_To_Address__c = ''; newCase.From_Address__c = ''; emailMessage newEmail = new emailMessage(); newEmail.FromAddress = 'Email@No.com'; newEmail.ToAddress = 'Feedback@no.com'; newEmail.CcAddress = 'ccAddress@verify.com'; newEmail.FromName = 'Someones Name'; newEmail.Incoming = True; newEmail.ParentId = newCase.id; insert newCase; insert newEmail; System.assertEquals ('Email@No.com', newCase.From_Address__c); System.assertEquals ('Feedback@no.com', newCase.Email_To_Address__c); System.assertEquals ('ccAddress@verify.com', newCase.Email_CC_Address__c); System.assertEquals ('Someones Name', newCase.Email_From_Name__c); } }
I know that if I create the case first, i can populate the emailMessage.ParentId as the newCase.id, but at that point, the trigger will have already fired because the case has already been inserted. Does anyone know how to create the child emailMessage first, and then associate it to the case at insert? Any help will be greatly appreciated!
Thanks, Mike.
Sure thing Amit. Here you go.
There is come issue in your Trigger it self. I dont think so you trigger is excuting After Insert logic as you are updating same record in After Insert event. If that trigger will execute then you will get Read only error