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
Jacob Elliott 8Jacob Elliott 8 

Create Test Class For Approval Process Trigger

Hello, I am trying to unlock a "Matter" record when the record goes into an approval process. A trigger will see that it is locked and then unlock the record. This is working, but my test class is only covering 66%. How can I get it to 100%? Thanks!

Trigger
trigger UnlockMatter on litify_pm__Matter__c (after insert, after update) {
    for(litify_pm__Matter__c matter : trigger.New) {
        If(Approval.isLocked(matter.Id)) {
            Approval.unlock(matter.Id);
        }
    }
}

Test Class
@isTest
public class TestUnlockMatter {
     @isTest static void TestEditMatterInApproval() {
          // Test data setup
          
          // Create A Party to assign to the matter
          Account party = new Account(litify_pm__First_Name__c='Jim', litify_pm__Last_Name__c='Halpert');
          insert party;
          
          // Create A Matter that is locked and unlock and edit it
          litify_pm__Matter__c matter = new litify_pm__Matter__c(litify_pm__Display_Name__c='Jim Halpert vs. Dwight Schrute', litify_pm__Client__c= party.Id);
          insert matter;
          Approval.lock(matter.Id);
         
         //Perform Test
         Test.startTest();
         Approval.unlock(matter.Id);
         matter.litify_pm__Display_Name__c ='Jim Halpert vs. Micheal Scott';
         Test.stopTest();
     }
}

 
Best Answer chosen by Jacob Elliott 8
Anthony McDougaldAnthony McDougald
Good Afternoon Jacob,
Hope that your day is off to an amazing start. Can we request for you to try the new updated test class below and report how it works for you? Thank you, and hope this helps. May God bless you abundantly.
@isTest
public class TestUnlockMatter {
     @isTest static void TestEditMatterInApproval() {
          // Test data setup
          
          // Create A Party to assign to the matter
          Account party = new Account(litify_pm__First_Name__c='Jim', litify_pm__Last_Name__c='Halpert');
          insert party;
          
          // Create A Matter that is locked and unlock and edit it
          litify_pm__Matter__c matter = new litify_pm__Matter__c(litify_pm__Display_Name__c='Jim Halpert vs. Dwight Schrute', litify_pm__Client__c= party.Id);
          insert matter;
         
         //Perform Test
         Test.startTest();
         //Lock record and update to activate trigger
         Approval.lock(matter.Id);
         matter.litify_pm__Display_Name__c ='Jim Halpert vs. Micheal Scott';
         update matter;
         Test.stopTest();
     }
}


Best Regards,
Anthony McDougald

All Answers

Anthony McDougaldAnthony McDougald
Good Afternoon Jacob,
Hope that your day is off to an amazing start. Can we request for you to try the new updated test class below and report how it works for you? Thank you, and hope this helps. May God bless you abundantly.
@isTest
public class TestUnlockMatter {
     @isTest static void TestEditMatterInApproval() {
          // Test data setup
          
          // Create A Party to assign to the matter
          Account party = new Account(litify_pm__First_Name__c='Jim', litify_pm__Last_Name__c='Halpert');
          insert party;
          
          // Create A Matter that is locked and unlock and edit it
          litify_pm__Matter__c matter = new litify_pm__Matter__c(litify_pm__Display_Name__c='Jim Halpert vs. Dwight Schrute', litify_pm__Client__c= party.Id);
          insert matter;
         
         //Perform Test
         Test.startTest();
         //Lock record and update to activate trigger
         Approval.lock(matter.Id);
         matter.litify_pm__Display_Name__c ='Jim Halpert vs. Micheal Scott';
         update matter;
         Test.stopTest();
     }
}


Best Regards,
Anthony McDougald
This was selected as the best answer
Jacob Elliott 8Jacob Elliott 8
Thanks, Anthony, That worked perfectly! God Bless!