Salesforce coding lessons for the 99%
Finally, Apex tutorials for point-and-click admins! Written by a self-taught Google engineer.
  • Beginner Tutorials
    • Apex
    • Certifications
    • Career Info
    • Technical Architect
    • Visualforce
    • Videos
  • Apex Academy
  • Success Stories
  • About Me
  • Misc
    • Mailbag
    • Challenges
    • Links
    • Login to my Org
Follow @dvdkliuor SUBSCRIBE!

Apex Video Tutorial: Write Your First Trigger!

July 2, 2014

Preface: This video is part 1 of the Official #Apex4Admins series presented by Salesforce and I!

Everything you need to know to get started with Apex from scratch!

  1. When and where to write Apex!
  2. How to write your first trigger and test class!
  3. How to get started in the Salesforce industry!

View slides

Trigger

trigger TShirts on Account (after insert) {
    for (Account acc : Trigger.new) {
Case shirtCase = new Case(); shirtCase.Subject = 'Send them a free T-shirt!'; shirtCase.Priority = 'Ultra High'; shirtCase.AccountId = acc.Id;
insert shirtCase;
} }

Test Class

@isTest
public class TestTShirts {
    static testMethod void accountCreator() {
Account acc = new Account(); acc.Name = 'Tesla Motors';
insert acc;
} }

Continue to Part 2: SOQL & Core Apex!

25 Comments
TRACHUS
September 14, 2015 @ 2:22 am

Super Duper! I’ve written my first test. Thumbs Up David. Just a little question.
When tests run don’t they create records – I don’t seem to have ‘Tesla Motors’ account and related Case after running TestTShirts test class?

thanks

Reply
    TRACHUS
    September 14, 2015 @ 3:24 pm

    I’ve got my answer. Someone replied to the same question under tutorial 2’s forum I think:
    “Record insertion/updation using test class is temporary,they are rollbacked once the test class execution is finished.”

    Reply
    David Liu
    September 15, 2015 @ 9:17 pm

    Everything is reverted back to normal after the test – so any records you create will disappear!

    Reply
Maxie
September 7, 2015 @ 11:48 pm

Hi david! I just successfully executed my first Trigger. Viola! This is so exciting!! Btw, I have question, how do I write in a code if I have a pick list then auto-populate one of the predefined picklists? I got an error when I tried it. It only works for text fields, ayt?

Reply
    David Liu
    September 9, 2015 @ 11:00 pm

    BOOM!!! Treat picklists just as you would strings. You’ll only get errors if you have a validation on them.

    Reply
Deepak Ranjan Mahapatra
January 11, 2015 @ 7:04 am

Hi David,

I copy pasted the above code and I am getting the below error.

Error: Compile Error: Variable does not exist: Id at line 6 column 29

I don’t understand whats wrong with the code. Kindly help.

Regards
Deepak

Reply
    David Liu
    January 12, 2015 @ 6:18 pm

    Double check your copy/paste – it should be good!

    Do you have permissions on these objects?

    Reply
Bharat
December 27, 2014 @ 6:16 am

Man! That was my first trigger. Thanks David for an awesome platform where beginners like me can learn & grow. #FeelingExcited

Reply
    David Liu
    December 27, 2014 @ 12:55 pm

    WOOT! Go Bharat!!

    Reply
Deepthi
December 19, 2014 @ 4:24 am

Ha David,

Today After watching your Apex4Admin webinar, I just tried my first trigger and executed successfully.

Thanks a lot !!!!

Reply
    David Liu
    December 19, 2014 @ 9:12 am

    Great job Deepthi, thanks for sharing!!

    Reply
Smitten_SFDC
October 18, 2014 @ 6:53 pm

Thank you so much for all your motivation and help. HATS OFF!!!

Reply
Shashank Parashar
September 25, 2014 @ 1:44 am

hey dvd so nice of u spreading the knowledge between all of us thank you so much i am a beginner of salesforce apex and feeling confidence after reading all your reply of question such a great teacher as a friend very helpfull thank u so much

Reply
    David Liu
    September 25, 2014 @ 7:15 pm

    woot woot!

    Reply
DS
September 15, 2014 @ 2:51 am

Hi David, 1st of all I must appreciate and thanks you for creating a platform for amateur and prof users of SFDC, hats of to you and thank you so much .Taking Inspiration from you , this website and your tutorials I have started learning APEX and TRIGGERS , I am still on my first step and I promise to bother you too much with my doubts.

So I created a Trigger on account just like your 1st example on Triggers on this page but I am getting an error ” Error Error: Compile Error: Invalid field AccountId for SObject Lead at line 6 column 1″

I have also pasted the Trigger I have written
Trigger NewLead on Account (after Insert) {
for ( Account acc : Trigger.new) {
Lead myLead = new Lead();
myLead.Description = ‘Finally Lead converted into an opportunity’;
myLead.Status = ‘Hot’;
myLead.AccountId = acc.Id;
insert MyLead;
}
}
Please advice ?

DS

Reply
Brandon
August 15, 2014 @ 7:51 am

Hey David,
I found out approvals don’t work on the Idea sObject. So, I’m trying to write a trigger to sync a new entry from the Idea sObject to an Idea Lobby cObject so it can be reviewed before posting. So, the process would be:

Idea entered through Idea sObject->Trigger syncs it to Idea Lobby cObject and then deletes it from Idea sObject-> Admin approves by changing picklist to “Accepted”-> Idea is moved back to Idea sObject for commenting and voting

Is this possible? I would really appreciate any tips on how to achieve this flow.

Thanks!

Reply
    David Liu
    August 15, 2014 @ 3:22 pm

    Interesting!

    Do you guys really need thee child object? Maybe you can get it done with one object instead – don’t forget to turn on field history tracking!

    Otherwise it’s definitely possible with Apex – try doing a few chapters on this site and you’ll know enough to get started. Once you have a baseline I can help!

    Reply
Nitin
August 11, 2014 @ 9:01 am

Hey David,
I was wondering, is Trigger.new a buffer for account objects being updated, or is it a List? if it is, when does the SFDC platform decide to execute the code in the loop and commit the data?
Thanks in Advance!!!
Nitin

Reply
    David Liu
    August 11, 2014 @ 8:30 pm

    It’s a list of Accounts being updated. The code will execute immediately after any updates are made on any records! Most of the time it’s only running on a single record, since users mostly updated one record at a time in the Salesforce UI.

    Reply
Karthick
July 23, 2014 @ 11:14 pm

Hi david,
Can you tell me what is trigger.new,trigger.old,trigger.newmap,trigger.oldMap and keySet

Thanks in advance

Reply
    David Liu
    July 27, 2014 @ 9:47 pm

    Trigger.new – list of the updated versions of records in your trigger
    Trigger.old – list of the older versions of records in your trigger
    Example: If a checkbox was checked, Trigger.new would have the record after it was checked and Trigger.old would have the record before it was checked

    newMap / oldMap are the above but in Map format. Keyset is the keys of the Map!

    Reply
      utkarsh srivastava
      August 12, 2014 @ 10:59 am

      David,

      Just a quick clarification in this regard, U said that trigger.new is the list of updated verions of the record but the trigger which we are writting is after INSERT so will it not contradict 3 scenarios (as on which record this trigger will perform – -the one which is getting created or the one which is getting updated or both) ?

      Reply
        David Liu
        August 12, 2014 @ 12:34 pm

        This official Salesforce page will have your answers =)
        https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

        Reply
sanjay kumar
July 17, 2014 @ 4:29 am

Hi David,
using this piece of code to display the RL of articles attached but not able to detach the articles.

When trying to do that, it gives me error: “The link you followed wasn’t valid for your session. Return to the previous page, refresh it, and try again.”

Reply
    David Liu
    July 17, 2014 @ 10:04 pm

    Can you give me more detail what you’re trying to do? Sorry having trouble following!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


*

*

Theme: Simple Style by Fimply