Trying to Avoid Governor Constraints in SOQL Queries

I have 2 user objects: destination_c and TimeDrive_c.

Destination has fields

  • startdate__c
  • contact__c

TimeDrive__c has

  • Target_date__c
  • contact__c
  • Createddate

Here is what i need to do

I need to get all records of all records in a specific date range

select id, Target_date__c, Contact__c,  Name, CreatedDate
                    from TimeDrive__c  where Target_date__c >=:startdate  and 
                    Target_date__c <=:enddate

I need to scroll through each entry in this list and check if there are appointments for this contact whose start date has fallen between targetdate and createddate

Here is the bit that I have done so far

        timeDriverLst = [select id, Target_date__c, Contact__c,  Name, CreatedDate
                    from TimeDrive__c  where Target_date__c >=:startdate  and 
                    Target_date__c <=:enddate ];

    if(timeDriverLst.size() >0){
    for(integer i =0; i < timeDriverLst.size(); i++)
    {
        mapTime.put(timeDriverLst[i].id, timeDriverLst[i]);

       /* appLst = [Select Name, Contact__c from Appointment__c where (StartDate__c > = :timeDriverLst[i].CreatedDate 
                 and StartDateTime__c <=:timeDriverLst[i].Target_date__c) and Contact__c = :timeDriverLst[i].Contact__c ];   
           */      

    }

I know that I should not have a SOQL query in a for loop. How can I avoid this and achieve this requirement.

+3
source share
1 answer

, : , . , . , . ( ).

+4

All Articles