You need to sign in to do that
Don't have an account?
John Athitakis
ystem.LimitException: Too many SOQL queries: 101
Have some very simple code. Basically when a custom object (sales area) is updated, I want all the related opportunities the not won/lost opps that are related to it to be updated as well (which for most sales areas, is just 5-6 opps atm since this is a new process). I've tried a few dozen ways to get around the error in the title, have moved my update out of a for look as well as the select statement, but cannot for the life of me understand what I'm doing wrong?
trigger SalesAreaRelatedOpportunityUpdate on Sales_Area__c (after update) { if(checkRecursive.runOnce()) { List<ID> salesAreaId = new List<ID>(); for (Sales_Area__c sa : Trigger.new){ salesAreaId.add(sa.id); } List <Opportunity> updateOps=[SELECT Id FROM Opportunity WHERE IsClosed=False AND (Sales_Area__c in :salesAreaId OR Secondary_Sales_Area__c in :salesAreaId)]; update updateOps; } }
Take backup of your existing code and replace it with below code:
Reduced one for loop, which will improve the performance.
In the similar way if we can re-factor the other code, for sure you will not get CPU error.
If you still gets into then we will think of @future / batch apex.
Please do let me know if it helps.
Regards,
Mahesh
All Answers
There may be an issue with the other triggers on the Opportunity.
Just to test it, disable all the Triggers on Opportunity and try testing it.
Regards,
Mahesh
Here is my other code. I do not believe this can be the source of the problem? I will try to disable it later tonight--are you seeing anything wrong here? (I had a itch that my first trigger had no problems...)
I modified the above trigger as below:
First try to de-activate it and perform the test.
Next, activate the trigger, take a backup of your code and replace it above code and perform the test.
Regards,
Mahesh
1) http://help.salesforce.com/apex/HTViewSolution?id=000181404&language=en_US
2) https://help.salesforce.com/apex/HTViewSolution?id=000213152&language=en_US
Here are some best practices that will stop the error messages and/or help you avoid hitting the Governors Limit:
1. Since Apex runs on a Multitenant structure, Apex runtime engine strictly enforces limits to ensure code doesn't monopolize shared resources. Learn about the Governors Limit.
2. Avoid SOQL queries that are inside FOR loop.
3. Check out the Salesforce Developer Blog where you can find Best Practices for Triggers.
4. Review best practices for Trigger and Bulk requests on our Force.com Apex Code Developer's Guide.
5. Be sure you're following the key coding principals for Apex Code in our Developer's Guide.
Let us know if this will help you
Thanks
Amit Chaudhary
So, this seems to eliminate the error (Thank you Mahesh) but the original code does not seem to be updating the Opportunities.
The ideal is that editing the sales area would cause the opportunities to 'update', in turn firing off the apex trigger that you helped fix for me.
Basically there are 2 lookups that relate an Opp to a Sales area (Sales_Area__c and Secondary_Sales_Area__c).
I feel something is wrong here and that maybe my list or update is wrong?
Notably, doing the following reintroduces the 101 error even with the Opportunity Trigger disabled
1. Changing my Sales Area Trigger to a before
2. Removing the recursive check (to be expected I imagine)
3. I updated the code to the below with the opp trigger disabled and I'm seeing unexpected behavior. I would expect that of the 30-40 related opps in my sandbox, that all of them would get a Best Case (Currency field) of 1000, but none of them are being updated.
Em I attempting to update all of these records incorrectly in my original code? I'm tempted to say my List is empty (Although I'm certain the logic is sound).
Regards,
Mahesh
I simplified down my select statement, trying to take out any noise. Currently -this- is doing nothing it seems (none of the fields im forcing in the for loop to update are actually updating. (Note: PO_Number__c is a text string).
Please enable the debug log and do test.
Paste the debug log once you are done with test.
Regards,
Mahesh
It might be related to this (unsure). There are two other triggers that exists at Sales Area, which updates the users per below. It could be the reason for it? Both are however before triggers, while this one is after.
Might be one of these is being greedy and causing the issues with my much simpler 'update child opportunities' code..?
Please make sure that you are not having any SOQL queries inside for loop.
Regards,
Mahesh
Hello Mahesh.
I totally agree. However, I've disabled all other code and getting the CPU APEX limit error, so I'm not entirely sure what it is going on and believe it might be an installed package. I will research until then. I believe the issue originally (the Trigger not updating the Opportunity) is likely still an issue. I will update ticket soon as I clear up the apex cpu issue.
Regards,
Mahesh
Here is the log.
Please use the below code so that we can get some debug messages.
Regards,
Mahesh
Hello Mahesh,
Here was the code I was running. I'm pulling the log off the dev console.
Here is the log I generated with this.Is there a reason I should make the List into a Set? I changed it to a set (per what you have above). Removing the recursive check results in an APEX Cpu limit.
Regards,
Mahesh
Mahesh---Let me thank you. You are a super star!
Removing the checkRecursive with a salesarea with only 13 opportunities was successful.
Moving to one with over 100 opportunities resulted in an APEX CPU error however.
The log is quite large (to large to post) and I can't seem to figure out what code could be causing the issue. Since it works with Sales Areas with very few opps but not larger ones, I'm inclined to think its the following apex code.
I've read on @future. Would converting this code to @future help? It's not time sensitive--but I've never used the @future function before.
Thank you :-)
We are done with one big stopper issue :-)
Now, talking about your next issue, I have to go through the whole trigger and streamline it. Give me some time I will modify and post it here.
Also we can look into @future options, will give you openion on it as well in my next post.
Regards,
Mahesh
You are amazing ^_^ Thank you so much.
Take backup of your existing code and replace it with below code:
Reduced one for loop, which will improve the performance.
In the similar way if we can re-factor the other code, for sure you will not get CPU error.
If you still gets into then we will think of @future / batch apex.
Please do let me know if it helps.
Regards,
Mahesh
Mahesh! I don't know if you have the @future code handy? I attempted to do it myself but it seems it is doing nothing! Here is what I did.
Trigger Code
However, it does nothing. I even hard coded a change early on (sa.Inside_Rep_Lookup__c='005G0000006uYgi';) and it is not doing it. I see from my apex jobs that the @future is firing and completing without any errors.@Future Code
Here you asked one custom object and another one is Opportunity(5), it means it is parent child relationship here. parent is custom object and child is Opportunity object. whenever update parent field (phone num or any field we have to update in child(opportunity) which have stage name not eqaul to(closed won and closed lost)
Here The Trigger code is
public class customobjupdTriggerHelper {
public void updateopportunityphn(set<id> salessids, Map<id,Sales_area__c> NewsalesMAp)
{
list <Opportunity> lstopp = [select id,name,phone__c,Sales_area_name__c,StageName from opportunity where Sales_area_name__c in:salessids];
for(Opportunity opp : lstopp)
{
//system.debug('----------------'+ opp.StageName);
if( NewsalesMAp.containskey(opp.Sales_area_name__c) && opp.StageName != 'Closed Won' && opp.StageName != 'Closed Lost' )
{
//system.debug('=========='+NewsalesMAp.get(opp.Sales_area_name__c).phone__c);
opp.phone__c = NewsalesMAp.get(opp.Sales_area_name__c).phone__c;
opp.StageName = NewsalesMAp.get(opp.Sales_area_name__c).StageName__c;
}
}
update lstopp;
}