You need to sign in to do that
Don't have an account?
Anthony Zirilli 10
Help Bulkifying Code. Problem with query inside loop.
Hi there,
I desperatly need help tweaking this code. It works to pull in the active opportunity on the account level, but does not work when doing mass updates/uploads. I have the understading that the querry needs to be moved outside the loop but don't know how to do that exactly.
Any help would be greatly appreciated. Thank you!
I desperatly need help tweaking this code. It works to pull in the active opportunity on the account level, but does not work when doing mass updates/uploads. I have the understading that the querry needs to be moved outside the loop but don't know how to do that exactly.
trigger act_opp on Account (before insert, before update) { map<Id, Account> acctmap = new map<Id, Account>(); for(Account a : trigger.new){ acctmap.putAll([SELECT Id, (SELECT Id, Name FROM Opportunities WHERE (RecordType.Name = 'Membership Renewal' AND Active_Membership__c = 'Active') OR (RecordType.Name = 'Membership Prospect' AND Web_Expire_Date__c != null) LIMIT 1) FROM Account WHERE Id =: a.Id]);} for(Account a : trigger.new){ if(acctmap.get(a.Id).Opportunities.size() != 0){ Opportunity opp = acctmap.get(a.Id).Opportunities[0]; a.Active_Membership_Opportunity__c = opp.Id;} else {a.Active_Membership_Opportunity__c = null;} } }
Any help would be greatly appreciated. Thank you!
There was a field missing in query.
I have updated the code. Please replace your code from below code :
Let me know if there is any issue.
Regards,
Abhishek
All Answers
As per your requirement i have modified your trigger code.
Please update your trigger code from below code :
Please let me know if you need more help on this.
Thanks,
Abhishek
Thank you for your help.
Please change line no. 2 from below line :
Let me know if there is any issue.
Thanks,
Abhishek.
Opportunity spelled wrong, AND is missing proper parentheses, AccountId is referenced with opp.
I believe this fixed them all.
thx
As a common practice, if your question is answered, please choose 1 best answer.
But you can give every answer a thumb up if that answer is helpful to you.
Thanks
I put the code in and went to update the account record and recieved this error: Apex trigger act_opp caused an unexpected exception, contact your administrator: act_opp: execution of BeforeUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Opportunity.AccountId: Trigger.act_opp: line 7, column 1
Any thoughts?
There was a field missing in query.
I have updated the code. Please replace your code from below code :
Let me know if there is any issue.
Regards,
Abhishek