top of page
  • Writer's pictureBecka Dente

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());         } }
6 views0 comments

Recent Posts

See All

Comments


bottom of page