+ Start a Discussion
RossGRossG 

batch job to count related records

I've seen a lot of trigger code online that doesn't really work for this.  What I'm working on is a scheduled batch job that runs once a day, that tallies the number of records in a related list for a record in a given object.  This code needs to be able to handle what all the code I'm seeing online does not:

if the lookup field value changes on the child object, the parent object's tally field must be updated appropriately.

In other words, this is how it should work:

object x
object y
a lookup field on object y to object x called "z"
a number field "a" on object x

batch job runs once a day and populates the field in object x called "a" with the the number of records in object y where object y's field "z" = x

here's an exampel that might be easier to visualize:

youve got account x with a new custom field called "number of contacts"
you've got 3 contacts on account x
run the batch job and it updates account x field "number of contacts" with a value of "3"
great.  that is correct.
Now, update 1 of those 3 contacts such that it NO LONGER has an account = x but rather the contact is moved to another account
run the batch job again
account  x should now show, after the job runs, a value of "2" in the "number of contacts" field

Any ideas on how this batch job class code would work, I'd really appreciate it.  I can't seem to get it to work.

Thanks guys
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Hi Ross,

Is there any specific requirement for batch job.
Because salesforce recommends to use Roll Up summary fields on parent object to display related child object records count.
You may find more about roll up summaries at http://help.salesforce.com/HTViewHelpDoc?id=fields_about_roll_up_summary_fields.htm&language=en_US (http://help.salesforce.com/HTViewHelpDoc?id=fields_about_roll_up_summary_fields.htm&language=en_US)

If this helps you, Mark this as best answer.
Thanks,
N.J
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Just clarify more, Roll Up Summaries can be created on Master object of Master-Detail relationship.

Thanks,
N.J
RossGRossG
Thanks.

Well, I was using account/contact in my example to make things as easy to understand as possible.  In reality I'm not dealing with those 2 objects.  I'm dealing with contacts and contracts, which don't have a master detail relationship.  It's a lookup relationship, and thus, roll up summary fields are inapplicable here.

So, thank you for the response, but roll up summary fields are not applicable to this use case unfortunately.  

This is definitely a use case where I need to use code to populate the field.
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
I would sugesst two approches:
(Assuming contact has count of contracts as a custom field)

1st Approch

1. Query all contacts
2. Query related contracts for thos contacts.
3. Update all contacts

2nd Approch

1. Write trigger on contract (after update) to update related contact record if criteria is meet

Thanks,
N.J