+ Start a Discussion

batch apex to syncronize two objects

hi i am trying to write an batch apex class which conducts a check between two objects. Basically i have two objects


object1 and object2. object2 is a replica of object1.  So what are all the records exists in object1 should also exist in object2.


So i want to write a batch apex class and schedule it to run everynight to check the the both objects and make sure both are in sync.


If a record exists in object1 and not in object2 it should generate an error . All these errors should be displayed in a form of error log and displayed at the final result. So the output should be an error report.



Did anybody faced same scenario and worked on it,can u share the code or give an suggestion how to go ahead with this.





You would want a custom field on object2 that is a lookup to object1, and have your trigger(s) include object1.Id within object2. Next, you'd write your batch Apex to run on all object1 records, performing a query against object2, and checking the results. You would want an object3 that would store the results of the entire batch operation (in the event that there are errors in each batch). Your "finalize" routine for the batch apex would query object3, compile the information into a viewable format, and then use the Email apex objects to deliver this report to whomever you specify. I do not have time at the exact moment to write up an entire example at the moment, but I think it would be fairly straightforward to implement. The only other consideration is that your email can not exceed 100,000 characters, so if that were a problem, you would want to break it up into separate emails.


thanks man good approach. But the thing is objectA has no llokup on objectB. So there is no direct relationship between them. there two unique fields for both objectA and objectB which helps us to check for existence. So we endedup writing a batch class which runs the check on the objectA and objectB and get the results and send them thru email to admin.


Hi Naga,

In the execute method you can populate a list with the object content(object1 or object2) in a batch process and compare it with the other based on the identifier field. Just populate a set with the non-matching records(Pass the set to a list and then update the object). You can populate another object with the records or have an rollup summary field on either of the objects. In the finish method you can send a notification mail with the error log genrated in the object. As far as scheduling is considered, have a cron trigger to evoke it every night.


Hope this helps. Please let me know the workaround you found out. Thanks