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!
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!
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
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.”
Everything is reverted back to normal after the test – so any records you create will disappear!
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?
BOOM!!! Treat picklists just as you would strings. You’ll only get errors if you have a validation on them.
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
Double check your copy/paste – it should be good!
Do you have permissions on these objects?
Man! That was my first trigger. Thanks David for an awesome platform where beginners like me can learn & grow. #FeelingExcited
WOOT! Go Bharat!!
Ha David,
Today After watching your Apex4Admin webinar, I just tried my first trigger and executed successfully.
Thanks a lot !!!!
Great job Deepthi, thanks for sharing!!
Thank you so much for all your motivation and help. HATS OFF!!!
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
woot woot!
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
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!
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!
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
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.
Hi david,
Can you tell me what is trigger.new,trigger.old,trigger.newmap,trigger.oldMap and keySet
Thanks in advance
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!
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) ?
This official Salesforce page will have your answers =)
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm
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.”
Can you give me more detail what you’re trying to do? Sorry having trouble following!