+ Start a Discussion
max.alexander1.3945796734965325E12max.alexander1.3945796734965325E12 

System.LimitException: Too many SOQL queries: 101

HI have follwing code, it has a queerry inside a for loop, however  if i take it out  i cannot refference the date fields orrectlyher eis the code any help greatly appreciated.



trigger UpdatePeriod on Revenue__c (before insert, before update) {
    set<Date> Ids = new set<Date>();
    map<date, Id> myMap = new map<date, Id>();
    for(revenue__c r : trigger.new) {
       
  for(Period__c p: [SELECT Id FROM Period__c WHERE (Start_Date__c <= :r.Date__c and End_Date__c >= :r.Date__c) ]) {
        myMap.put(r.Date__c,p.id);
    }
    }
   
   
    for(Revenue__c r : Trigger.new) {
       
           
            r.Period__c = myMap.get(r.Date__c);
       
    }
}
Best Answer chosen by max.alexander1.3945796734965325E12
max.alexander1.3945796734965325E12max.alexander1.3945796734965325E12
I fixed it i think
trigger UpdatePeriod on Revenue__c (before insert, before update) {
  
    map<date, Id> myMap = new map<date, Id>();
    list<Period__c> Period = [SELECT Id,Start_Date__c,End_Date__c  FROM Period__c ];

    for(revenue__c r : trigger.new) {
       
  for(Period__c p: Period) {
  if(p.Start_Date__c <= r.Date__c && p.End_Date__c >= r.Date__c){
        myMap.put(r.Date__c,p.id);
    }
    }
   
  } 
    for(Revenue__c r : Trigger.new) {
       
           
            r.Period__c = myMap.get(r.Date__c);
       
    }
}