Preface: this post is part of the Write Your First Trigger From Start to Finish series.
Chapter 1 Questions:
1. If you can solve a business need using either a workflow or a trigger, which should you use?
2. Is it possible to write a trigger directly in a production Salesforce org?
3. What is Trigger.new and why is it so important?
4. Which version of the field name do you reference in triggers? Field Label or Field/API Name?
-Field Name: Favorite Color
-Field / API Name: Favorite_Color__c
5. Is it possible to deploy code to production without writing a test class?
6. How does your test class know which trigger to test?
7. True or false. Developers should avoid using comments as much as possible in code since comments are ignored by Salesforce.
8. At minimum, how many components must be added to a Change Set to deploy an Apex trigger?
Chapter 1 Practice Trigger:
Write a trigger that transfers every new contact to an Account named “Sfdc99”!
Don’t forget the test class!
Wee! After three years as a Salesforce Admin, I have written my first lines of code! And they even worked as expected!! lol
Great job Sami. =)
Hi David,
You have hard coded an Id over here, so if deploy this code to other org it won’t work because of hard coded Id of Account. So I developed a generalized code for this, if you want I can share for all.
Yes please share!
Hi David,
Question: While deploying a trigger to production, it is compulsory to add the test class too in the change set. From what i have faced as an admin, if a field is used in a test class, it gives me issues if i ever try to change the field type of that field!!
Hence the question. Can i deploy a trigger to production without the test class??
Technically, there are hacky ways to get a trigger into prod without a test class.
But I can honestly think of no good reason why you would want to do so!
Build good habits early and make a good test class =)
David
Hi David,
Thanks for this website…!
Basically I am from mechanical background. At the start of learning SFDC, I was very curious of coding. But after exploring to coding part, really I was wondering how I’ll learn. After getting in to your website, I got confidence that I’ll learn.
Once again Thank you…!
Hello David,
I was able to successfully write the trigger and test class to transfer contact. However when I run the test class, it tells me the method has passed but I don’t see the contact in contact list. Does the trigger actually save the test record I tried to create through the test method? When I create a contact manually it does transfer the contact as expected so I know the trigger works when I test manually. But when I ran the test class I expected to see that contact in contacts list but I don’t see it.
Thanks,
Sree
Thank you!! I just wrote my first trigger and test code and it worked!! You explained it so well – it just makes sense.
Good job Claire!!
I don’t know how to thank you enough … when I will receive my developer certification [thanks to your precious help] … It would be an honor to take you out for dinner.
My wish for you is always do what you enjoy most and get paid for that!!!!
Thank you =)
Hi David,
The site is very much useful for me.I could learn a lot of new things and practice them.Feel happy that I could answer all the questions correctly.Only because of the way you explain things.Thank you so much.Waiting for the new topics :)
Great job Neenu!!!! Proud of you!
Hi David,
Good quiz.I just started learning SF so able solved few of them.
I tried to create new opportunity through just like u did with Case Object.Then i saw ur trigger,U have used List to insert value in this trigger can i know why??
I don’t see any lists O_o
I can’t answer this question: How does your test class know which trigger to test?
Check out the answer sheet here =)
https://www.sfdc99.com/2014/01/27/quiz-answers-chapter-1/
I know this could be done using a custom button with a mashed up URL. In those situations is it better to go with a custom button or still use a trigger?
Great question!
Custom button URL hacking is a great way to quickly get things done in an org! The downsides are that you only control the logic when someone actually uses the button – creating records any other way would bypass the logic! For example creating records through the quick create, data loader! etched etc.
In an actual org you’d weigh these disadvantages based on their importance with your org – but if both were equally useful, go with the custom button route cuz it’s easier! We’re just practicing so assume we must make this trigger, hahaha
Thanks for the nice Quiz.
I was able to answer all the Questions correctly.
******************************************************
I have a question n sorry if you find it a silly question …
When we are writing any trigger under any object, directly in the Trial edition (Developer edition) of Salesforce, it works without any test class.
Is it a freedom in this or how it is??
Well done Anurag!!
This is a freedom only in developer edition =) Salesforce lets you do this so you can quickly run/test your code!