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
Derek Patrick DayaDerek Patrick Daya 

Need help to bulkify the code I created.

Hi All,

I have the following codes and for some reason it's hitting SOQL Limit when another Trigger fires. I think I need to bulkify this code in order for the error not be received. Can anyone help me bulkify the codes below? Please.

    trigger DerekCountApplicantsWithActivity on AVTRRT__Job__c (before update) {
     for (AggregateResult ar : [
      Select Count(Id) numRecs, AVTRRT__Job__c jobId
      From AVTRRT__Job_Applicant__c
      Where Last_Activity_Date__c != null and AVTRRT__Job__c In :Trigger.New
      Group By AVTRRT__Job__c]) {
    
       Id jobId = (Id) ar.get('jobId');
       AVTRRT__Job__c job = Trigger.newMap.get(jobId);
       job.Number_of_Applicants_with_Activities__c = (Decimal) ar.get('numRecs');
     }
    }



The error receive when we run a debug log is:

"Trigger.DerekCountApplicantsWithActivity: line 2, column 1
18:59:34.291 (21291921315)|FATAL_ERROR|System.LimitException: Too many SOQL queries: 101

Trigger.DerekCountApplicantsWithActivity: line 2, column 1
18:59:34.292 (21292028756)|CODE_UNIT_FINISHED|DerekCountApplicantsWithActivity on Job trigger event BeforeUpdate for [a0FF000000I9dIx]
18:59:34.294 (21294614785)|DML_END|[21]
18:59:34.298 (21298582601)|FATAL_ERROR|System.LimitException: Too many SOQL queries: 101

Trigger.DerekCountApplicantsWithActivity: line 2, column 1
18:59:34.298 (21298597822)|FATAL_ERROR|System.LimitException: Too many SOQL queries: 101"


Please help!
YuchenYuchen
Maybe you can try the following:

AggregateResult[] theResult = [Select Count(Id) numRecs, AVTRRT__Job__c jobId
      From AVTRRT__Job_Applicant__c
      Where Last_Activity_Date__c != null and AVTRRT__Job__c In :Trigger.New
      Group By AVTRRT__Job__c];
for(AggregateResult ar: theResult){
       Id jobId = (Id) ar.get('jobId');
       AVTRRT__Job__c job = Trigger.newMap.get(jobId);
       job.Number_of_Applicants_with_Activities__c = (Decimal) ar.get('numRecs');
}
 
Derek Patrick DayaDerek Patrick Daya

Hi Yuchen,

Thank you for your response. Upon validating the trigger, I receive quite a few errors regarding Apex heap size too large. How can we limit the result or better yet how can we apply login when heap size is too large to minimize the result?

 

This is the code:

Trigger

AggregateResult[] theResult = [Select Count(Id) numRecs, AVTRRT__Job__c jobId
      From AVTRRT__Job_Applicant__c
      Where Last_Activity_Date__c != null and AVTRRT__Job__c In :Trigger.New
      Group By AVTRRT__Job__c];
for(AggregateResult ar: theResult){
       Id jobId = (Id) ar.get('jobId');
       AVTRRT__Job__c job = Trigger.newMap.get(jobId);
       job.Number_of_Applicants_with_Activities__c = (Decimal) ar.get('numRecs');
}
}

Test Class 

@isTest(seeAllData=true)
private class DerekApplicantsWithActivityTestClass{
    static testMethod void DerekCountApplicantsWithActivity() {
      
      Date CurrentDate = Date.Newinstance(2014,11,12);
      
      AVTRRT__Job__c a = new AVTRRT__Job__c();
      a.AVTRRT__Job_Title__c = 'Sys Ad';
      
      insert a;

      Contact c = new Contact();
      c.FirstName = 'Derek';
      c.LastName = 'Daya';
      c.Email = 'derekpatrickdaya@yahoo.com';
      
      insert c;
      
      AVTRRT__Job_Applicant__c b = new AVTRRT__Job_Applicant__c();
      b.AVTRRT__Job__c = a.id;
      b.AVTRRT__Contact_Candidate__c = c.id;
      b.Last_Activity_Date__c = CurrentDate;
        insert b;
     }
}