You need to sign in to do that
Don't have an account?
sudhirn@merunetworks.com
Trigger is not firing in opportuntiy
Hi,
I wrote below trigger on opportuntiy to update account field for some reasons it is not workng please suggest me what is the issue in below trigger
Thanks
Sudhir
I wrote below trigger on opportuntiy to update account field for some reasons it is not workng please suggest me what is the issue in below trigger
trigger update_account_billing_email on Opportunity (before insert, before update) { for(Opportunity opp:System.Trigger.new) { Opportunity op = [SELECT Billing_ContactEmail__c,AccountId from Opportunity where id in :Trigger.newMap.keySet()]; Account ac = [SELECT Billing_Email__c from Account where id = :op.AccountId]; If ( ac.Billing_Email__c == '') { ac.Billing_Email__c = op.Billing_ContactEmail__c; update ac; } } }
Thanks
Sudhir
Further more, your trigger is performing an update or DML inside of a for loop, which will throw an exception in cases where you load several Opportunity records.
In other words, your trigger needs a bit of refactoring. I've taken a stab at it for you below:
All Answers
Further more, your trigger is performing an update or DML inside of a for loop, which will throw an exception in cases where you load several Opportunity records.
In other words, your trigger needs a bit of refactoring. I've taken a stab at it for you below:
Thanks
Amit Chaudhary
There are few mistakes in the code, such as
1. A SOQL query will return a List of Opportunity. But you are trying to collect as a single Opportunity.
2. Trigger.newMap.Keyset() will work only for "Before Update" and does not work for "Before Insert" Trigger. For "Before Insert" the value of Trigger.NewMap will be NULL
3. While querying the Account, do not use op.AccountID, which is single opportunity. Always use SET of all opportunity IDs
4. The most important error is, you have placed Two SOQL queries inside the FOR Loops. Remove that and keep the SOQL queries outside the FOR loop as shown in "Amit Chaudhary"'s example
Thanks
Sudhir