Teach Me Salesforce

A community approach to learning salesforce.com

Trigger to create a new record

with 13 comments

Ask me a year ago, and I never thought I’d be writing triggers, but yet, here I am. I gained a bit of confidence after taking the Salesforce DEV 531 class, and that confidence then flourished with the support of this community. This blog just goes to show how many people out there are willing to help, and I definitely want to return the favor whenever possible. So here goes – the anatomy of my first trigger.

What I was looking to do was to create a new support request record (a custom object) when a new Event record of a specific type was created. Pretty straight-forward, but definitely not doable without a trigger. The final result was this:

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;
 }

Now for the breakdown of what I was trying to do with each step. First, I define what I want the trigger to do & when to do it.

trigger createPDtask on Event (after insert) {

The second line compiles all new records I am going to insert into a list (& therefore takes care of bulkifying my trigger).

List<Support_Request__c> sr = new List<Support_Request__c>();

Then I need to define the criteria around the event record that would cause this trigger to create a new support request.

for (Event newEvent: Trigger.New)
         if (newEvent.Type__c == '1. Meeting - Initial'){

Next, I set the details that get inserted into the new Support Request record. You can see that some are static, while a few are dynamically pulled from the event record.

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));

Finally, the list gets inserted.

 }
   insert sr;
 }

As far as triggers goes, it’s pretty simple, but it was a great learning process for me to build on my own. From here, the next step is to write some test coverage, and while I thought I had it in the bag, I’m going to let Kyle share the lesson he gave me in a subsequent post.

Written by Becka

May 16, 2011 at 10:10 am

Posted in Apex, Beginner, Intermediate, Trigger

Tagged with

13 Responses

Subscribe to comments with RSS.

  1. […] 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 […]

  2. Great article! I like the element-by-element anatomy. I was just training my Adv. Admin to start writing Apex and I’m going to forward this to him as a great breakdown.

    Luke Cushanick

    May 16, 2011 at 1:16 pm

    • Luke, I have a companion piece to this for writing test methods that will be posted tomorrow morning. Keep an eye out!

      knthornt

      May 16, 2011 at 3:03 pm

    • Thanks for the feedback! Just like learning a new language, it’s sometimes easier to learn when broken down to bits. Granted, I’m still learning, but it’s definitely how I tackle reading Apex.

      Becka

      May 16, 2011 at 10:34 pm

      • ya thats truth..

        Sagar

        December 22, 2011 at 2:23 am

  3. […] Rebecca (@sfdc_nerd) posted about her first trigger.   She did a great job and even got 100% coverage from her test class. Rebecca had actually posted […]

  4. Thanks for posting this!!! Just curious, what did you think of the dev531 class? I am trying to take it but it has been cancelled due to lack of interest the last 2 months.. thanks again!!

    Fred

    May 17, 2011 at 8:38 pm

    • Take this class by whatever means necessary!!! I spent a week in NYC 20 days before my wedding to take this class & it was worth it a million times over.

      Is that a strong enough endorsement? Not yet? OK, I’ll add this – knowing what I know now about the class, I would pay for it out of my own pocket.

      Becka

      May 20, 2011 at 3:34 pm

  5. Hi Becka,
    This is great article but its very basic one.If I need to insert around 10000 events it will show the Exception of Governor limits.So U need to optimize the same trigger.Otherwise Its looks great.

    krishna

    May 24, 2011 at 12:18 pm

    • Krishna, Where do you see the need to optimize? Becka processes everything in a list and then does a single DML call to insert the records. There is no optimization needed here.

      knthornt

      May 24, 2011 at 10:42 pm

  6. great stuff. Thanks for breaking it down

    mitzchauhan

    May 25, 2011 at 12:23 pm

  7. This is good one, trigger power is necessary in salesforce.
    I hava also written some blog posts over triggers.

    http://forceschool.blogspot.com/search/label/Apex%20Triggers

    Shashikant Sharma

    July 1, 2011 at 3:30 pm

  8. […] sfdc_nerd Becka D {Blog} Just published my first article on Teach Me Salesforce – http://tinyurl.com/3snrmjc #salesforce #apex […]


Leave a reply to knthornt Cancel reply