Trigger Happy!

I talk a lot about the ‘auto-magic’ in salesforce.com, but we all know it has it’s limitations. One of the things that I always wished could be done with button-clicking was to create a new record when conditions on another record were met (ya know, using some workflow powers). I really just considered myself out of luck.

That is until I started dabbling in the developer world. And while I consider myself knowing just enough to causing catastrophic damage, a business case presented itself in which trying out my new skills was the perfect fit. In short, the team wanted a record auto-created every time a specific type of event was created.

Here it is, my debut into triggers (excluding my copy-edit-paste of one from MichaelForce):

trigger createPDtask on Event (after insert) {     
List<Support_Request__c> sr = new List<Support_Request__c>();
    for (Event newEvent: Trigger.New)
         if (newEvent.Type__c == '1. Meeting - Initial'){
                 sr.add (new Support_Request__c(
                     Name = 'New PD',
                     Task_Type__c = 'PD',
                     SFDC_Record_ID__c = newEvent.Id,
                     Rep__c = newEvent.OwnerId,
                     Event_Date__c = newEvent.ActivityDate));   
         }
   insert sr;
 }

So there it is. Short & fairly simple, but a HUGE productivity win. Plus, now everyone thinks I’m magic! And if you want a deeper dive explanation on the anatomy of this trigger, read my post on the Teach Me Salesforce blog!

UPDATED!! Here is my test class (Huge thanks to Kyle for help with the System.assertEquals bit:

@isTest private class createPDtask_Test { 
   private static testmethod void testTriggerForEvent(){         
 Event e = new Event();   
               
 //Required Fields         
 e.Subject = 'testSubject';         
 e.IM_Type__c = '1. Meeting - Initial';         
 e.Product_Interest__c = 'None';         
 e.StartDateTime = DateTime.NOW();         
 e.EndDateTime = DateTime.NOW()+1;                  


 //Insert the Record         
 insert e;                  


 //Query for Inserted SR         
 List<Support_Request__c> testRequests = [SELECT id FROM Support_Request__c             
 WHERE SFDC_Record_ID__c = :e.Id];                      


 //We expect one record to be created         
 System.assertEquals (1, testRequests.size());        
 } 
}
Advertisements

8 Responses to “Trigger Happy!”

  1. Well done. Looks great. Short and sweet.

  2. Any tips on creating the test cases? That’s where I get challenged. If you have created those, or when you create them, if you could post them it would be most useful.

  3. Rhonda, your test case would be pretty simple.
    Create a test class where you insert an Event object.
    The event insert would fire the trigger.
    After the insert, use SOQL to query Support_Request__c for an object, and compare / assert it with the fields in the event.

  4. on behalf of “community” you are welcome. To explain why the System.assertEquals is needed, we want to verify that the Support Request was actually created. The test method without the assertEquals causes all of the code to run, but adding in the assertEquals makes this a valid test.

    The test now inserts an event and checks for the desired outcome.

Trackbacks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: