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
AkashGarg555AkashGarg555 

before insert trigger is not working

I have a requirement where CustomerSuccess__c field on opportunity to be updated  'true' if CustomerSuccess__c field on Account is 'true' in before Insert event.

I have tried below solution, trigger saved succesfully but it didn't work.
Can anyone 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;
		}
	}
}
}
Regards,
Akash
 
Best Answer chosen by AkashGarg555
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi akash,
Try this code.kindly modify according to your requirement
trigger OppoCustomerSuccessCheckboxUpdate on Opportunity (before insert) 
{
    list<Id> AccountIds = new list<Id>();
    
    for (Opportunity o : Trigger.New){
        AccountIds.add(o.AccountId);
    }
    
    map<Id,Account> oppAccounts = new map<Id,Account>([SELECT Id, Name,customersuccess__C FROM Account WHERE Id IN :AccountIds]);
    
    for (Opportunity opp : Trigger.New){
        Account acc = oppAccounts.get(opp.AccountId);
        if (acc != NULL && acc.customersuccess__c ){  
            
            opp.customersuccess__c = true;
        }
    }
    
}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
 

All Answers

abhishek singh 497abhishek singh 497
Hello Akash,
Can you just debug below line of your code:-
if(Opp.Account.CustomerSuccess__c) and let me know what value it is returning.

Regards,
Abhishek Singh.
AkashGarg555AkashGarg555
Hi abhishek singh 497,
It is returning false in debugging but i have checked  CustomerSuccess__c field on Account is checked(true).
Why it is returning FALSE, can you help me on understanding this.

Regards,
Akash
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi akash,
Try this code.kindly modify according to your requirement
trigger OppoCustomerSuccessCheckboxUpdate on Opportunity (before insert) 
{
    list<Id> AccountIds = new list<Id>();
    
    for (Opportunity o : Trigger.New){
        AccountIds.add(o.AccountId);
    }
    
    map<Id,Account> oppAccounts = new map<Id,Account>([SELECT Id, Name,customersuccess__C FROM Account WHERE Id IN :AccountIds]);
    
    for (Opportunity opp : Trigger.New){
        Account acc = oppAccounts.get(opp.AccountId);
        if (acc != NULL && acc.customersuccess__c ){  
            
            opp.customersuccess__c = true;
        }
    }
    
}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
 
This was selected as the best answer
abhishek singh 497abhishek singh 497
Hello Akash,
There is a concept that trigger will not execute based on the condition of lookup object field like in your case.
if(Opp.Account.CustomerSuccess__c) ===> this will not work.
if(opp.Accountid!=null) ====> this will work.

You can take help of Devi's trigger,it is correct.

Thanks.
Abhishek Singh.
Mark Moser 10Mark Moser 10
Related object data is not populated when evaluating in a trigger.

You cannot access opp.account.anything

You need to query the data, put in a map and then look up the info in the loop
AkashGarg555AkashGarg555
Thanks abhishek singh 497 and Mark Moser 10 for clearing my doubt.
@Devi Chandrika  - Your code works perfectly.
Closing this ticket.