Resource Hero triggers and related test classes

When setting up Resource Hero on a custom or standard object, you have the option of setting up Resource Hero triggers and related test classes. This trigger will enable the following functionality for your object:

  • Enables the Archive Resource Assignments checkbox.  See details on the Resource Hero Fields page.
  • When a record is deleted all related Resource Hero records (assignments/forecasts) are deleted with it.
  • Ensures that some text fields on Resource Assignment records (Assigned To, for example) are kept in sync with changes to your object.  These fields are typically only used on Group Edition reports and will not impact the display of the matrix.

The creation of Resource Hero triggers and related test classes must be done in a Salesforce sandbox and then moved to production using any of the approved metadata migration tools (Force.com Migration Tool, Workbench, Force.com IDE, Visual Studio Code, etc.).

Setup

Your trigger should be created as follows where [Trigger_Name] is the name you would like to give your trigger and [Object_API_Name] is the API name of the object you are configuring.

trigger [Trigger_Name] on [Object_API_Name](before insert, before update, before delete, after insert, after update, after delete, after undelete)
 {
 ResourceHeroApp.RHA_TriggerDispatcher.entry(new ResourceHeroApp.RHA_TriggerDispatcher.TriggerParameters(Trigger.isBefore, Trigger.isAfter, Trigger.isInsert, Trigger.isUpdate, Trigger.isDelete, Trigger.isUndelete, Trigger.isExecuting, Trigger.old, Trigger.new, Trigger.oldMap, Trigger.newMap));
 }

Before you can deploy this trigger from a sandbox to your production environment, you need to ensure that you meet the minimum code coverage requirements as set forth by Salesforce.

Below is an example of a test class created on a custom object called ProjectObject__c.  You can use this example as a guide to creating your own test class by replacing all instances of ProjectObject__c with your object name and replacing all instances of ProjectField__c with your Resource Assignment lookup field name.

You may need to make additional changes depending on your specific use case, i.e. required fields, validation rules, etc.

@isTest
public class ProjectTrigger_Test {
   public static void Create_RHA_CS() {
      //Ensure that our lookup is configured
      ResourceHeroApp__RHA_Object_Translation__c ot = ResourceHeroApp__RHA_Object_Translation__c.getInstance('Project__c');
      if (ot == null) {
         ResourceHeroApp__RHA_Object_Translation__c opp_ot = new ResourceHeroApp__RHA_Object_Translation__c();
         opp_ot.Name = 'ProjectField__c';
         opp_ot.ResourceHeroApp__Object_Name__c = 'ProjectObject__c';
         opp_ot.ResourceHeroApp__Name_Field__c = 'Name';
         insert opp_ot;
      }
   }

   public static List<ProjectObject__c> CreateObject(String basename, Integer numrecords) {
      List<ProjectObject__c> results = new List<ProjectObject__c>();
      for (Integer i = 0; i < numrecords; i++) {
         ProjectObject__c o = (ProjectObject__c) ProjectObject__c.sObjectType.newSObject(null, true);
         o.Name = basename + String.valueOf(i);
         //o.Whatever_Other_Required_Fields_You_Have__c = 'Whatever default values you want to set';
         results.add(o);
      }
      return results;
   }

   public static List<ResourceHeroApp__Resource__c> CreateResource(String basename, Integer numrecords) {
      List<ResourceHeroApp__Resource__c> results = new List<ResourceHeroApp__Resource__c>();
      for (Integer i = 0; i < numrecords; i++) {
         ResourceHeroApp__Resource__c res = (ResourceHeroApp__Resource__c) ResourceHeroApp__Resource__c.sObjectType.newSObject(null, true);
         res.Name = basename + String.valueOf(i);
         res.ResourceHeroApp__Team__c = 'Development Team';

         res.ResourceHeroApp__Weekly_Target_Min_Hours__c = 35;
         res.ResourceHeroApp__Weekly_Target_Max_Hours__c = 45;

         res.ResourceHeroApp__Minimum_Rate__c = 10.00;
         res.ResourceHeroApp__Target_Rate__c = 15;

         results.add(res);
      }
      return results;
   }

   public static List<ResourceHeroApp__Resource_Assignment__c> CreateResourceAssignment(List<ProjectObject__c> objects, List<ResourceHeroApp__Resource__c> res) {
      List<ResourceHeroApp__Resource_Assignment__c> results = new List<ResourceHeroApp__Resource_Assignment__c>();
      for (ProjectObject__c o : objects) {
         for (ResourceHeroApp__Resource__c r : res) {
            ResourceHeroApp__Resource_Assignment__c ra = (ResourceHeroApp__Resource_Assignment__c) ResourceHeroApp__Resource_Assignment__c.sObjectType.newSObject(null, true);
            ra.ProjectField__c = o.Id;
            ra.ResourceHeroApp__Resource__c = r.Id;
            ra.ResourceHeroApp__Role__c = 'Developer';
            ra.ResourceHeroApp__Rate__c = 10;
            results.add(ra);
         }
      }

      return results;
   }

   static testMethod void ProjectUpdate() {
      Create_RHA_CS();

      //Created test data
      List<ProjectObject__c> objects = CreateObject('Object Update Test', 1);
      insert objects;

      List<ResourceHeroApp__Resource__c> res = CreateResource('Test Resource', 3);
      insert res;

      List<ResourceHeroApp__Resource_Assignment__c> ras = CreateResourceAssignment(objects, res);
      insert ras;

      //Confirm that test data as been created successfully
      List<ProjectObject__c> objectlist = [SELECT id, Name FROM ProjectObject__c];
      system.assertEquals(objectlist.size(), 1);
      List<ResourceHeroApp__Resource_Assignment__c> ralist = [SELECT id, ResourceHeroApp__Assigned_To__c FROM ResourceHeroApp__Resource_Assignment__c WHERE ProjectField__c IN :objectlist];
      system.assertEquals(ralist.size(), 3);

      //Validate that the assigned_to field has been set based on the values of the Object
      system.assertEquals(ralist[0].ResourceHeroApp__Assigned_To__c.left(18), 'Object Update Test');

      //Update the objects
      for (ProjectObject__c o : objectlist) {
         o.Name = 'updated name';
      }
      update objectlist;

      //Confirm that the new values are now reflected on resource assignments
      ralist = [SELECT id, ResourceHeroApp__Assigned_To__c FROM ResourceHeroApp__Resource_Assignment__c WHERE ProjectField__c IN :objectlist];
      system.assertEquals(ralist[0].ResourceHeroApp__Assigned_To__c, 'updated name');
   }
}

Related Support Posts:

Ready to get started?

Schedule a call to see if Resource Hero is right for you

Book now