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
TilluTillu 

Unable to resolve error? please correct this code?

I am getting  MarketMetricUpdate: System.LimitException: Too many SOQL  101 ....how to resolve this...can anyone correct below code.


Trigger MarketMetricUpdate on Market_Metrics__c(after insert,before update,after update)
{
  Map<string,Market_Metrics__c> BusinessMap = new Map<string,Market_Metrics__c>();
  set<id> Accountlist = new set<id>();
  set<string> ZipList = new set<string>();

  for(Market_Metrics__c b: Trigger.new)
  {
    if(String.IsNotBlank(b.Zip_Code__c) && String.IsNotBlank(b.SFDC_Account_Name__c))
    {
      BusinessMap.put(b.Zip_Code__c+b.SFDC_Account_Name__c,b);
      Accountlist.add(b.SFDC_Account_Name__c);
      zipList.add(b.Zip_Code__c);
    }
  }
Map<id,contact> ContactList = new Map<id,contact>([select id,Accountid,MailingPostalCode from Contact where Accountid IN : AccountList and MailingPostalCode IN:ZipList]);
  List<contact> cUpdate = New List<Contact>();
  for(Contact c : ContactList.values())
  {
    if(BusinessMap.get(c.MailingPostalCode+c.Accountid)!=null)
    {
      c.Market_Metrics__c = BusinessMap.get(c.MailingPostalCode+c.Accountid).id;
      cUpdate.add(c);
    }
  }

  if(cUpdate.size()>0)
   update cUpdate;
 

}
Best Answer chosen by Tillu
Peter_sfdcPeter_sfdc
Harsha__c, I'm not seeing the recursion in this instance. 

The code I see shows a trigger on one object causing an update on another. This code is not by its nature recursive.

I agree that recursion is one of the possibilities to explore, but if you're seeing something I'm not, I'd love it if you would share. 

Or do you just mean for Tillu to explore this as another possbility? For instance to look for code in the Contact trigger that is taking action upon Market_Metrics__c. 


Tillu, one additional thing. Your trigger is firing on both before and after update. But you don't have anything in your code to control when this code executes. One quick fix that you might attempt is to remove "before update" from the events of this trigger to see if you get better results. 

All Answers

Peter_sfdcPeter_sfdc
I don't think that the actual error is being thrown from this code directly. I don't see anywhere that you are invoking a query from within a loop (which is the classic root cause of this symptom). 

Can you attempt this again trying to catch the debug log for this transaction? I would be more than willing to bet that this line: 

update cUpdate;

is responsible for invoking another trigger down stream, which has the code that is the root cause. 
harsha__charsha__c
The error seems to occur due to recursion. For all the trigger events, you are just performing DML on the same object again, which in turn causes recursion. 

Try to use recursion helper to get rid of this issue.

Regards,
- Harsha
Peter_sfdcPeter_sfdc
Harsha__c, I'm not seeing the recursion in this instance. 

The code I see shows a trigger on one object causing an update on another. This code is not by its nature recursive.

I agree that recursion is one of the possibilities to explore, but if you're seeing something I'm not, I'd love it if you would share. 

Or do you just mean for Tillu to explore this as another possbility? For instance to look for code in the Contact trigger that is taking action upon Market_Metrics__c. 


Tillu, one additional thing. Your trigger is firing on both before and after update. But you don't have anything in your code to control when this code executes. One quick fix that you might attempt is to remove "before update" from the events of this trigger to see if you get better results. 
This was selected as the best answer
harsha__charsha__c
Crap....! how did I miss that :\

Cool.... you are right Peter..!

Regards,
- Harsha