You need to sign in to do that
Don't have an account?

Help with writing a Test Class for basic Trigger
Hi All,
I have written a basic trigger and currently have code coverage of 66%.
Not sure how to cover lines 8 and 9. Could anyone help me please??
trigger CaseAssignmentOutofOffice on Case (before update) {
For (Case c : Trigger.new) {
Case NewCase = Trigger.newMap.get(c.id);
Case OldCase = Trigger.oldMap.get(c.id);
If (OldCase.OwnerId != NewCase.OwnerId && c.Status != 'Closed' && NewCase.Back_Up__c != Null){
c.OwnerId = NewCase.Back_Up__c;
c.IsBackUp__c = True;
}
}
I have written a basic trigger and currently have code coverage of 66%.
Not sure how to cover lines 8 and 9. Could anyone help me please??
trigger CaseAssignmentOutofOffice on Case (before update) {
For (Case c : Trigger.new) {
Case NewCase = Trigger.newMap.get(c.id);
Case OldCase = Trigger.oldMap.get(c.id);
If (OldCase.OwnerId != NewCase.OwnerId && c.Status != 'Closed' && NewCase.Back_Up__c != Null){
c.OwnerId = NewCase.Back_Up__c;
c.IsBackUp__c = True;
}
}
Try following test method.
Thanks,
Himanshu
All Answers
Try following test method.
Thanks,
Himanshu
Find the below code,
Logic:
1. Insert a new user, then insert case by using the system.runAs(). So the owner will be a new inserted owner.
2. Update a case in the outer of system.runAs(), so the owner will become the current user.
So your trigger logic will satisfied. Please mark this as a solution, if you are satisfy this solution.
Greatly appreciated.