2

I have a batch class that is used to update a Field1 of Obj1.

Obj1 is child of Obj11. Inorder to update Obj1, I need to unlock its parent Obj11 first.

[by updating lock__c=false].

Then update Obj1.

Then again lock back the Parent Obj11

[by updating lock__c=true].

Is this a correct way to do so, by using successive update statement inside

void execute(Database.BatchableContext bc, list<sObject> sObj){} method, like

Update List<Obj11>;
Update List<Obj1>;
Update List<Obj11>;

or I need to place some of these Update statement under

void finish(Database.BatchableContext bc){} method.

Please advise.

10
  • 2
    I guess since you build the logic it should work. What is the problem you facing to implement this logic. I suggest wrote all code inside your execute method it self.
    – Ratan Paul
    Jun 13 '16 at 6:08
  • thanks, @Ratan, I'm giving it a try and let u know the outcome
    – user59759
    Jun 13 '16 at 6:10
  • 2
    try inner query in this way you will not update same parent twice. Jun 13 '16 at 7:11
  • 1
    What problem is the "lock" field trying to solve?
    – sfdcfox
    Jun 13 '16 at 16:35
  • 1
    @user59759 Then, yes, your approach is feasible. If you were doing the lock validation in a trigger, though, you could avoid both updates entirely by simply setting a static variable somewhere.
    – sfdcfox
    Jun 13 '16 at 17:16

Your Answer

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

Browse other questions tagged or ask your own question.