0

I am new to this and I hope someone can help me with this.

What my aim is to count the number of AVTRRT__Job_Applicant__c records with a date value on Last_Activity__c field on AVTRRT__Job_Applicants__c object that is associated to AVTRRT__Job__c and place the total count on the field called Number_of_Applicants_with_Activities__c on AVTRRT__Job__c object.

I think Agrgregate SOQL is the only option I have since using the code below conflicts with another trigger as it causes continues loop.

trigger DerekRollUp on AVTRRT__Job_Applicant__c (after insert, after update, after delete, after undelete) {
  Map<Id,AVTRRT__Job__c> updateJob = new Map<Id,AVTRRT__Job__c>();
  Set<Id> updateJobIds = new Set<Id>();
  // If we are inserting, updating, or undeleting, use the new ID values
  if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete)
    for(AVTRRT__Job_Applicant__c applicant:Trigger.new)
      updateJobIds.add(applicant.AVTRRT__Job__c);
  // If we are updating, some Jobs might change, so include that as well as deletes
  if(Trigger.isUpdate || Trigger.isDelete)
    for(AVTRRT__Job_Applicant__c applicant:Trigger.old)
      updateJobIds.add(applicant.AVTRRT__Job__c);
  // Do not create a record for null field
  updateJobIds.remove(null);
  // Create in-memory copies for all Jobs that will be affected
  for(Id JobId:updateJobIds)
    updateJob.put(JobId,new AVTRRT__Job__c(id=JobId,Number_of_Applicants_with_Activities__c=0));
  // Run an optimized query that looks for all Jobs that meet the if/then criteria
  for(AVTRRT__Job_Applicant__c applicant:[select id,AVTRRT__Job__c from AVTRRT__Job_Applicant__c where AVTRRT__Job__r.Id in :updateJobIds and Last_Activity_Date__c !=null])
    updateJob.get(applicant.AVTRRT__Job__c).Number_of_Applicants_with_Activities__c++;
  // Update all the Jobs with new values.
  Database.update(updateJob.values());
}
  • what is the relationship between AVTRRT__Job_Applicants__c and AVTRRT__Job__c? Who is parent and child? – Mahmood Dec 9 '14 at 6:20
  • Hope this link will help you - blog.jeffdouglas.com/2010/04/12/… – Prajith Mar 7 '16 at 10:35
  • Is the relationship between Job and Job applicants master detail? If yes, then you could proceed with rollup summary fields with condition. – Prajith Mar 7 '16 at 10:41
1

I am assuming the relationship between Job and Job Applicant is that of a Master Detail. i.e. 1 Job will have multiple Job Applicants and applicants cannot exist without a Job. Assuming this is true you could do the following using roll up summary fields

  1. Create a new Rollup Summary field called Applicant Count on the AVTRRT__Job__c object.
  2. Use COUNT() as the aggregate option
  3. In the FIlters section you could set the criteria as Last_Activity__c "not equal to" BLANK.

Add the Applicant Count field to page layout and you should be good. If the relationship between Job Applicant and Job is a Lookup then the trigger would be the way to go. However you could check this free appexchange app which allows rolling up field values with Lookup relationships. Rollup Helper almost eradicates the need to write triggers for the similar redundant requirement of summarizing fields between objects related using a Lookup relationship.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.