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
Gene T.Gene T. 

Help With Test Class

I've written some code that updates a parent record whenever a child record is created. I've tested the functionality, and it works for what I need it to do. Now I am stuck on how to write the test class to get the coverage I need to move it to production. Any help would be much appreciated!

 

trigger UpdateProjLastUpdate on Time_Allocation__c (after insert) {
            
     Map<Id,TTLProject__c> projMap = new Map<Id,TTLProject__c>();
     Set<id> Ids = new Set<id>();
         for (Time_Allocation__c  TA : Trigger.new)
         {
             Ids.add(TA.Project__c);
         }
    
     Map<id,TTLProject__c> projMap2 = new Map<id,TTLProject__c>([Select Id, Last_Update__c from TTLProject__c Where Id in :Ids]); 
    
         for (Time_Allocation__c TA : [SELECT Start_Date__c, Work_Done_By_User__c, Work_Done_By_User__r.name, Description__c, Project__c
                                                      FROM Time_Allocation__c
                                                      WHERE id
                                                      IN :Trigger.newMap.keySet()])
         {
             TTLProject__c proj = projMap2.get(TA.Project__c);
             proj.Last_update__c = TA.Start_Date__c.format() + ' - ' + TA.Work_Done_By_User__r.name + ' - ' + TA.Description__c;
             projMap.put(proj.id,proj);
         }
        
         if(!projMap.isEmpty())
         {
             update projMap.values();
         }
}

 

Vinit_KumarVinit_Kumar

Gene,

 

You need to insert test records for the objects and will have to honour any condition wriiten in your trigger.

 

Pleas ego through the below link which could help you.

 

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

 

http://shivasoft.in/blog/salesforce/step-by-step-salesforce-tutorial-%E2%80%93-creating-trigger-and-test-cases-%E2%80%93-6-of-6/

 

Please create a test class and if you face any issues,I can help you out.

 

Mayank_JoshiMayank_Joshi

Hi Gene,

 

Your Trigger is making Update call, so you need to include 'After Update' Event into your Trigger .

 

Below is your Trigger and Test Class for it . If Test class is not working for you, then you just need to include some more Fields that needs to fullfill your Update( ) call . For that ,we need  to just update field so that your Code logic will cover up in test Method . 

 

Apex Trigger :

trigger UpdateProjLastUpdate on Time_Allocation__c (After Update) {
Map<Id,TTLProject__c> projMap = new Map<Id,TTLProject__c>();
Set<id> Ids = new Set<id>();
for (Time_Allocation__c TA : Trigger.new) {
Ids.add(TA.Project__c);
}
Map<id,TTLProject__c> projMap2 = new Map<id,TTLProject__c>
([Select Id, Last_Update__c from TTLProject__c Where Id in :Ids]);

for (Time_Allocation__c TA : [SELECT Start_Date__c, Work_Done_By_User__c,
Work_Done_By_User__r.name, Description__c, Project__c
FROM Time_Allocation__c WHERE id IN :Trigger.newMap.keySet()])
{
TTLProject__c proj = projMap2.get(TA.Project__c);
proj.Last_update__c =
TA.Start_Date__c.format() + ' - ' + TA.Work_Done_By_User__r.name + ' - ' + TA.Description__c;
projMap.put(proj.id,proj);
}
if(!projMap.isEmpty())

update projMap.values();

}

 

Test Class for this : 

===============

@isTest
private Class Test_UpdateProjLastUpdate
{
public static testmethod void test_to_UpdateProjLastUpdate ()
{
TTLProject__c TTLProject = new TTLProject__c();
test.startTest();

TTLProject.Last_Update__c = 'Test' ;
update TTLProject;

test.stopTest();
}
}

 

Thanks,

Gene T.Gene T.

Hey Mayank,

 

I tried your test class but I get this error:

 

 

I am not a developer and a bit lost on this. Any help would be much appreciated.