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
DoubleADoubleA 

Help to deploy trigger to production account (need to create test methods)

Hi I recently created my first trigger with the great help from one of hte users on the group, however I realized I cannot deploy this trigger to a production account without a Test method testing over 75% of the code (below).  Unfortunately I have never test code and would like some help in doing this since I cannot deploy to production without doing so.

 

trigger UpdateContactsAccount on Task (after insert, after update) { SET<ID> setContactID = new SET<ID>(); string contactID = ''; for(Task t : Trigger.New){ if(t.whoid!=null && t.whatid ==null){ contactID = t.Whoid; if(contactID.startsWith('003')){ setContactID.add(contactID); } } } LIST<Contact> recordsToUpdate = [Select Id, AccountID, Account.Description, Account.Last_Call_Date__c, Account.Last_Visit_Date__c From Contact Where Id IN : setContactID]; List<Account> accountsToUpdate = new List <Account>(); for(Task t : Trigger.New){ for(Contact c : recordsToUpdate){ if(t.subject.startswith('Call')|| t.subject.startswith('call')){ c.Account.Last_Call_Date__c = t.activitydate; Account a = new Account(Id=c.Account.Id, Last_Call_Date__c=c.Account.Last_Call_Date__c); accountsToUpdate.add(a) ; } if(t.subject.startswith('Visit') || t.subject.startswith('visit')){ c.Account.Last_Visit_Date__c = t.activitydate; Account a = new Account(Id=c.Account.Id, Last_Visit_Date__c=c.Account.Last_Visit_Date__c); accountsToUpdate.add(a) ; } } } if(accountsToUpdate.size() > 0){ database.update(accountsToUpdate, false); } }

 

aalbertaalbert

I recommend this article to help get started with Test Methods.

In short, you have to write apex code that will initiate your trigger and execute at least 75% of the trigger's code base (obviously strive for 100% code coverage). 

DoubleADoubleA

Thanks Albert once again!


I actually just got a hold of a similar documents so i'm reading that now as well.

 

Thanks again!

ShikibuShikibu

A tip, DoubleA: I am also pretty new to apex. One thing I've learned is that it's easier and more maintainable to write a trigger less than a dozen lines, move all the logic to a helper class, and put all the test code in a testclass.

 

You can then test little bits of your logic, make sure each bit works, and finally test the trigger with 21 test records, which will ensure that you catch "too many SOQL queries" exception on the trigger.

 

Once you get comfortable with this pattern, you'll probably find that it's easier to sketch your logic from the top down, but implement in detail from the bottom up, using a testmethod for each bit you implement to prove to yourself that it is correct before you move on to methods or classes that depend on it.

 

If you adopt this pattern, it's also helpful to make it very easy to disable testing the trigger (static final Boolean testBulkTrigger = true) . I ran into trouble when I wanted to disable a trigger in production, and there was a circular dependency between trigger, helper class, and test class.

Message Edited by Shikibu on 05-11-2009 04:31 PM