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
FinneyFinney 

HELP PLEASE - System.LimitException: Too many SOQL queries: 101

Hi There,

 

I am getting an error when trying to save an Inventory record. I have a trigger on the Inventory Object which is causing the error.

 

Been strruggling with the trigger for days but couldn't find a solution.

 

I'm posting the trigger here

 

Please help me.

 

Thanks and Kind Regards

 

Finney

 

trigger CreateBankingExsLease on pb__InventoryItem__c(before update){
  List<pb__InventoryItem__c> invs = new List<pb__InventoryItem__c>();
  List<ID> invIds = New List<ID>();

  for( pb__InventoryItem__c inv: Trigger.new){
  invs.add(inv);
  invIds.add(inv.Id);
  pb__InventoryItem__c beforeUpdate = System.Trigger.oldMap.get(inv.Id);
     
  if (beforeUpdate.Property_Managed__c != inv.Property_Managed__c){ 
    
      
    }
  }

  List <pb__Agreement__c> Ag = [ SELECT Id, pb__InventoryId__c, pb__AgreementType__c, Stage__c  from
pb__Agreement__c where pb__InventoryId__c = :invIds and pb__AgreementType__c = 'Lease' and Stage__c = 'Transaction Closed and Commission Received'LIMIT 5];
 for(pb__Agreement__c agg : Ag)
    {
        for(pb__InventoryItem__c i: invs) 
  if(i.Property_Managed__c == true)
  Agg.Banking_Lease__c =  'Yes';
 
  update Ag;   
  
  }

  
}
jiah.choudharyjiah.choudhary

@Finney,

There might be 3 reasons
1. 'update Ag;' in a for loop is causing this error. (Add the items to be updated in a list and update it outside the for loop.)
2. May be the trigger is going in recursion.
3. See if you can avoid the for loop inside a for loop.

FinneyFinney

Hi Jiah,

 

Can you suggest the changes in the code please and post the revised code.

 

Thanks

 

Finney

jiah.choudharyjiah.choudhary

@Finney,

Find the changes below (Adding the items to be updated in a list and update it outside the for loop):


If this still doesnt work, I would further make changes for avoiding for loop inside a for loop.

List<pb__Agreement__c> lstPbAgreement = new List<pb__Agreement__c>();
for(pb__Agreement__c agg : Ag)
{
for(pb__InventoryItem__c i: invs)
{
if(i.Property_Managed__c == true)
Agg.Banking_Lease__c = 'Yes';
}

lstPbAgreement.add(Agg);

}

If(!lstPbAgreement.isEmpty())
update lstPbAgreement;

 

FinneyFinney

Hi Jiah,

 

Still getting the same error after changing the code.

 

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger CreateBankingExsLease caused an unexpected exception, contact your administrator: CreateBankingExsLease: System.LimitException: Too many SOQL queries: 101".

 

Changed Code

 

trigger CreateBankingExsLease on pb__InventoryItem__c(before update){
  List<pb__InventoryItem__c> invs = new List<pb__InventoryItem__c>();
  List<ID> invIds = New List<ID>();

  for( pb__InventoryItem__c inv: Trigger.new){
  invs.add(inv);
  invIds.add(inv.Id);
  pb__InventoryItem__c beforeUpdate = System.Trigger.oldMap.get(inv.Id);
     
  if (beforeUpdate.Property_Managed__c != inv.Property_Managed__c){ 
    
      
    }
  }

  List <pb__Agreement__c> Ag = [ SELECT Id, pb__InventoryId__c, pb__AgreementType__c, Stage__c  from
pb__Agreement__c where pb__InventoryId__c = :invIds and pb__AgreementType__c = 'Lease' and Stage__c = 'Transaction Closed and Commission Received'LIMIT 5];
 List<pb__Agreement__c> lstPbAgreement = new List<pb__Agreement__c>();
for(pb__Agreement__c agg : Ag)
{
for(pb__InventoryItem__c i: invs)
{
if(i.Property_Managed__c == true)
Agg.Banking_Lease__c = 'Yes';
}

lstPbAgreement.add(Agg);

}

If(!lstPbAgreement.isEmpty())
update lstPbAgreement;
}

 

jiah.choudharyjiah.choudhary
@Finney

can you send me the entire requirement?
FinneyFinney

Hi Jiah,

 

We have 3 objects here

 

1) Inventory

2) Agreement

3)Banking

 

Inventory and Agreement are connected using a lookup. Inventory is the Parent and Agreement the Child.

 

The same way agreement and Banking are connected using a lookup where Agreement is the parent and Banking the child.

 

We have 2 agreement types 1) Lease 2) Property Management

 

What we are trying to do is when the Property Management agreement is closed, a banking record should be created under the Lease Agreement which is also closed.

 

Here the Lease Agreement will be created and closed first and when the Property Management Agreement is closed next, the banking record should be created on the Lease agreement.

 

So what I have done is I have created 2 fields. One field called Property Managed(Check Box) on the Inventory object and the other Banking Lease (Picklist) on the Agreement.

 

When the Property Management Agreement is closed, the PROPERTY MANAGED field on the Inventory will be ticked.

When the PROPERTY MANAGED field on the Inventory gets ticked, the BANKING LEASE field on the Lease agreement will be YES which in turn creates the Banking record on the Lease Agreement.

 

I have got this trigger to update the Banking Lease field on the Lease Agreement when the Property Managed field on the Inventory gets ticked.

 

Thanks

 

Finney