function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andrew Hoban 6Andrew 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:
 
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;
   
    }
}

 
Best Answer chosen by Andrew Hoban 6
UC InnovationUC Innovation
Oh, I think I know what's going on...  In your test case, after the 'insert mdp' statement, reset ApproveProcessAutomationHandler.isFirstTime back to true:

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

UC InnovationUC Innovation
That would be my guess as well.  Did you check the debug log and see if there's any errors?  If you don't have permission to edit a locked record, you should see something like:

System.DmlException: Update failed. First exception on row 0 with id a08540000002da3AAA; first error: ENTITY_IS_LOCKED, the entity is locked for editing: []

 
Andrew Hoban 6Andrew Hoban 6
Thanks for the reply. I checked the logs and i dont seem to have any errors. It does say its only performed one DML operation so its asif the record doesnt get up to the update stage? 

I think i do have permission to edit a recod, but for some reason the record is not being updated.
UC InnovationUC Innovation
Oh, I think I know what's going on...  In your test case, after the 'insert mdp' statement, reset ApproveProcessAutomationHandler.isFirstTime back to true:

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.
 
This was selected as the best answer
Andrew Hoban 6Andrew Hoban 6
Thanks mate, thats worked!