You need to sign in to do that
Don't have an account?
Andrew Hoban 6
Test Class to update record that has been approved
Hi,
I have a trigger that automatically puts a recod for approval. Once the recod has been approved, a field called 'Create Case' is checked and a case is then a case is automatically created.
When creating the test class, the trigger.isinsert has been completed. It is the Trigger.isUpdate that is not been checked by the current test class.
Im not sure if its becasue the recod hasnt been approved, and is currently locked? The test class does not seem to throw any errors however.
Trigger:
Test Class:
I have a trigger that automatically puts a recod for approval. Once the recod has been approved, a field called 'Create Case' is checked and a case is then a case is automatically created.
When creating the test class, the trigger.isinsert has been completed. It is the Trigger.isUpdate that is not been checked by the current test class.
Im not sure if its becasue the recod hasnt been approved, and is currently locked? The test class does not seem to throw any errors however.
Trigger:
trigger PassforApproval on Match_Day_Pass__c (after insert, after update) { List <Case> caseToInsert = new List <Case>(); String body = ''; if(ApproveProcessAutomationHandler.isFirstTime) { ApproveProcessAutomationHandler.isFirstTime = false; for (Match_Day_Pass__c MDP : Trigger.New) { if(MDP.Full_Name__c != Null ) { if(Trigger.isInsert){ //call approval method AutomatedApprovalRequest.submitApproval(MDP.id); } if(Trigger.isUpdate){ if (mdp.Create_Case__c == TRUE && mdp.Case_Created__c == FALSE) { Match_Day_Pass__c newMDP = new Match_Day_Pass__c(); newMDP = [SELECT Id FROM Match_Day_Pass__c WHERE Id =: mdp.Id]; newMDP.Case_Created__c = TRUE; Case c = new Case (); Contact[] AccountsMatched = [Select id, Name from Contact where Accountid = '001f000000q4IJC' And Name =:mdp.Full_Name__c LIMIT 1]; if (AccountsMatched.size()>0) { c.contactid = AccountsMatched[0].id; } c.recordtypeid = '012f00000000WiV'; c.Assignee__c = 'Unassigined'; c.Subject = 'Match Day Pass approved for ' + Trigger.new[0].Full_Name__c; c.Description = 'Match Day Pass to be created for: ; c.Category_IT_Help_Desk__c = 'Admin and Services'; c.Category_Detail_IT_Help_Desk__c = 'Matchday Pass'; c.Ownerid = '00Gf0000000qaDp'; insert c; upsert newMDP; } }
Test Class:
@isTest(SeeAllData = True) public class PassforApprovalTest { static testMethod void PassforApproval(){ Account a = New Account(); a.Name = 'Test'; insert a; Match_Day_Pass__c mdp = new Match_Day_Pass__c(); mdp.Full_Name__c = 'Test'; mdp.Create_Case__c = FALSE; mdp.Case_Created__c = FALSE; mdp.Area__c = 'Other'; mdp.Type__c = 'Game Specific'; insert mdp; test.startTest(); mdp.Create_Case__c = TRUE; mdp.Case_Created__c = FALSE; upsert mdp; Match_Day_Pass__c mdpTest = [SELECT Id, Create_Case__c, Case_Created__c FROM Match_Day_Pass__c WHERE Id=:mdp.Id]; system.assertEquals(TRUE, mdpTest.Create_Case__c); system.assertEquals(FALSE, mdpTest.Case_Created__c); system.debug(mdp.Create_case__c); Test.stopTest(); Update mdp; Case c = new Case(); c.recordtypeid = '012f00000000WiV'; c.Assignee__c = 'Unassigined'; c.subject = 'Test subject'; c.description = 'test description'; c.Category_Detail_IT_Help_Desk__c = 'Test'; c.Category_Detail_IT_Help_Desk__c = 'Test'; Insert c; } }
ApproveProcessAutomationHandler.isFirstTime = true;
It's set to false after the first insert, and remains false for the duration of the test method. You need to set it back to true for the update to occur in the trigger.
Let me know if this works.
All Answers
System.DmlException: Update failed. First exception on row 0 with id a08540000002da3AAA; first error: ENTITY_IS_LOCKED, the entity is locked for editing: []
I think i do have permission to edit a recod, but for some reason the record is not being updated.
ApproveProcessAutomationHandler.isFirstTime = true;
It's set to false after the first insert, and remains false for the duration of the test method. You need to set it back to true for the update to occur in the trigger.
Let me know if this works.