How to create the Resource Hero trigger and related test class

As part of the steps to Setup Resource Hero on a custom or standard object, you may be required to create a new trigger on one or more objects.  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));
}
Copy to Clipboard

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 create your own test class by replacing all instances of ProjectObject__c with your object name and replace all instances of ProjectField__c for 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.

As always, contact us with any questions.

 

@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');
    }
}
Copy to Clipboard

Have more questions?

Contact Us