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
CCGNCCGN 

System.LimitException: Too many SOQL queries: 101 help!!

I have this trigger and it is marking the following error, I can not find the fault System.LimitException: Too many SOQL queries: 101

trigger SFDC_Trigger_Edificio on SFDC_Edificio__c (after insert, after update) {
    if(trigger.isAfter) {
        if(trigger.isInsert || trigger.isUpdate) {
            if(trigger.new[0].RecordTypeId == Schema.SObjectType.SFDC_Edificio__c.getRecordTypeInfosByName().get('Edificio Venta').getRecordTypeId()) {
                map<Id, decimal> edificio_DeGracia = new map<Id, decimal>();
                map<Id,map<Id,Date>> idContrato_idEdificio_Fecha = new map<Id,map<Id,Date>>();
                
                for(SFDC_Edificio__c forData : trigger.new) {
                    if(trigger.isUpdate && (forData.Dias_de_gracia__c != trigger.oldMap.get(forData.Id).Dias_de_gracia__c)) {
                        if(!edificio_DeGracia.containsKey(forData.Id)) {
                            edificio_DeGracia.put(forData.Id, forData.Dias_de_gracia__c);
                        }                                                                               
                    }
                }
    
                list<OrderItem> consultaGlobal = [select Id, Fecha_de_entrega_Departamento__c, OrderId, Order.ContractId, Order.Contract.Oportunidad__c, Order.Contract.Oportunidad__r.Edificio__c, Order.Contract.Oportunidad__r.Edificio__r.Dias_de_gracia__c from OrderItem where Order.Contract.Oportunidad__r.Edificio__c IN : edificio_DeGracia.keySet() and Fecha_de_entrega_Departamento__c <> null and Order.Contract.Bloquear_DDGracia__c = false and Order.Contract.Status = 'Borrador'];
                if(!consultaGlobal.isEmpty()) {
                    for(OrderItem forData : consultaGlobal) {
                        if(!edificio_DeGracia.containsKey(forData.Order.Contract.Oportunidad__r.Edificio__c)) { edificio_DeGracia.put(forData.Order.Contract.Oportunidad__r.Edificio__c, forData.Order.Contract.Oportunidad__r.Edificio__r.Dias_de_gracia__c); }
                        if(!idContrato_idEdificio_Fecha.containsKey(forData.Order.ContractId)) {
                            map<Id,Date> tmp = new map<Id,Date>();
                            tmp.put(forData.Order.Contract.Oportunidad__r.Edificio__c,forData.Fecha_de_entrega_Departamento__c);
                            idContrato_idEdificio_Fecha.put(forData.Order.ContractId, new map<Id,Date>());
                            idContrato_idEdificio_Fecha.put(forData.Order.ContractId, tmp);
                        }
                    }
                    list<Contract> contratosInvolucrados = [select Id, Dias_de_gracia__c, Oportunidad__r.Edificio__c from Contract where Id IN : idContrato_idEdificio_Fecha.keySet()];
                    if(!contratosInvolucrados.isEmpty()) {
                        for(Contract forData : contratosInvolucrados) {
                            forData.Dias_de_gracia__c = idContrato_idEdificio_Fecha.get(forData.Id).get(forData.Oportunidad__r.Edificio__c).addDays((Integer)edificio_DeGracia.get(forData.Oportunidad__r.Edificio__c));
                        }
                        update contratosInvolucrados;                   
                    }
                }
            }
        }
    }
}
Best Answer chosen by CCGN
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

This error appears when you exceed the Execution Governors Limit (you can run up to a total 100 SOQL queries in a single call or context): https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm

To fix the issue, change your code so that the number of SOQL fired is less than 100.
If you need to change the context, you can use @future annotation which will run the code asynchronously.
 
Best practices to avoid exceeding the Governors Limit:

Since Apex runs on a multi-tenant platform, the Apex runtime engine strictly enforces limits to ensure code doesn't monopolize shared resources.
 
Reference: https://help.salesforce.com/articleView?id=000181404&type=1

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas