function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Krati KapoorKrati 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
Suraj MakandarSuraj Makandar
Hi Krati : You can refer below link to know how to write triggers:

https://trailhead.salesforce.com/en/content/learn/modules/apex_triggers/apex_triggers_intro

Thanks,
Suraj
Shubham_KumarShubham_Kumar
Hi Krati
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.
for(Opportunity opp : trigger.new){
	oppAccountIdSet.add(opp.AccountId)
}

then  you have to query the Account with that Account Id set and create a map of AccountId and account.custonersuccess.
for(Account acc : [Select Id, YourCheckBoxField From Account Where Id IN :oppAccountIdSet ]){
	accountMap.put(Id,YourCheckBoxField);
}

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
for(Opportunity opp : trigger.new){
	if(accountMap.containsKey(opp.AccountId)){
		if(accountMap.get(opp.AccountId)){
			opp.customersuccessblock = true;
		}
	}
}


Hope this helps, do let me know if you have any query.
P.S: Mark this as the best answer if it helped.
AkashGarg555AkashGarg555
Hi Shubam_Kumar,
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
trigger OppoCustomerSuccessCheckboxUpdate on Opportunity (before insert) 
{
for(Opportunity opp : Trigger.New)
{
	if(Opp.AccountID != null)
	{
		if(Opp.Account.CustomerSuccess__c)
		{
			Opp.CustomerSuccess__c = true;
		}
	}
}
}

 
Suraj MakandarSuraj Makandar
Hi Akash Garg 31,

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
 
Opp.Account.CustomerSuccess__c


Thanks,
Suraj
AkashGarg555AkashGarg555
Hi Suraj Makandar,
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
Suraj MakandarSuraj Makandar

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

AkashGarg555AkashGarg555
Hi Suraj Makandar,
Thanks for your help in understanding this concept.

Regards,
Akash