You need to sign in to do that
Don't have an account?
Krati Kapoor
trigger on Opportunity for updating the checkbox field
Hi All, I need to write a trigger on Opportunity object for checkbox field update by checking the value of a field in Account Object, this trigger will be before insert.
if account.custonersuccess=TRUE then opportunity.customersuccessblock= TRUE
if account.custonersuccess=TRUE then opportunity.customersuccessblock= TRUE
https://trailhead.salesforce.com/en/content/learn/modules/apex_triggers/apex_triggers_intro
Thanks,
Suraj
First of all i would like to point out that this can be done with a simple workflow but if you still want to achieve this via a trigger, then you will need to run a loop on trigger.new context and in that loop you have to populate a set with AccountId.
then you have to query the Account with that Account Id set and create a map of AccountId and account.custonersuccess.
After that you have to run a new loop on trigger.new context and inside that loop you will have to check accountMap.containskey(Accountd) , if yes then get the value of that map accountMap.get(AccountId) , if true then you can update your opportunity
Hope this helps, do let me know if you have any query.
P.S: Mark this as the best answer if it helped.
Your solution is working fine. I checked the same in my Org.
I have tried a below solution, trigger saved succesfully but it didn't work.
Can you please explain why my trigger is not working
Your trigger is not working because in trigger the related object fields are not accessible,
So you can not use below line to access "Customer Success" field, you need to do SOQL on Account object
Thanks,
Suraj
Account and Opportunity has a Master Detail relationship between them then why Account fields are not accessible.
Need to understand the scenario.
Thanks for your help.
Regards,
Akash
Hi Akash Garg 31,
The reason is that for scalability, the Force.com platform doesn't perform an in-memory lookup for each relationship in your object. You need to do that yourself. The good thing is that the solution is relatively painless and is safe for bulk transactions.
Link: https://blog.jeffdouglas.com/2010/02/23/relationship-lookup-objects-in-triggers-are-null/
Thanks,
Suraj
Thanks for your help in understanding this concept.
Regards,
Akash