SFDO-Community / declarative-lookup-rollup-summaries

Declarative Lookup Rollup Summaries (DLRS) is a community built and maintained Salesforce application that allows you to create cross object roll-ups declaratively - no code! For install instructions and documentation, visit our website https://sfdo-community-sprints.github.io/DLRS-Documentation/
https://sfdo-community-sprints.github.io/DLRS-Documentation/
BSD 3-Clause "New" or "Revised" License
684 stars 235 forks source link

Enable partial update of Parent records in batch #259

Open AlexIsMaking opened 8 years ago

AlexIsMaking commented 8 years ago

I run batch updates of Opportunities overnight, triggering code which updates child records, which then triggers a DLRS update of the parent Opportunities. If the resulting update to the parent Opportunity hits a validation rule or error, every Opportunity in the batch is not updated.

Could a checkbox be added to the Lookup Rollup Summary records to enable users to allow partial success of the bulk DML operation on the Parent records? I'm thinking of the use of a Database Class Method, rather than DML Statement.

afawcett commented 8 years ago

Sounds like a valid use case, i've marked this as an enhancement. My question in the meantime would be what behaviour would you want in terms of how errors are expressed to the user? Would the DLRS trigger just set the addError on the child records that corrsespond to the failed parents? Then in your own code you can use more granular error handling via Database methods to allow partial updates on the child records? What do you think, other thoughts?

AlexIsMaking commented 8 years ago

Apologies for the slow reply.

I could be misinterpreting your question but I'm not proposing any changes to the way that errors on child records are handled. It's only the resulting update to the parent record that I need to manage with a Database Class Method.

Am I correct in thinking that currently any errors on the child records will prevent the DLRS update on the parent from being triggered anyway? In which case, I wouldn't need errors on the child records to be managed differently.

The issue occurs when the DLRS tries to update a parent record once the child record's have been changed, that update causes an error on the parent and then the rest of the parent records in the batch aren't updated as a result.

afawcett commented 8 years ago

Ok i understand. I'm just wondering to make it a complete solution what you would want DLRS to do with the errors, i'm not sure just ignoring them makes for a good implementation, even though you may not be interested in them. If you don't have any preference thats cool, i'll come up with some approach that works for you and delivers the applicable error handling if some of the parent updates fail.

AlexIsMaking commented 8 years ago

Ok that sounds good.

I would have liked to have saved you having to work out an approach for handling the child's errors. But my assumption is that errors on the child records will prevent the DLRS from being triggered at all (which would be fine for me), it sounds like that's incorrect. But I'm not sure why you would still want the DLRS to execute after the error, since I would have thought any resulting rollups to the parent would be missing values from the child record that hit the error.

afawcett commented 8 years ago

I think there is some confusion here between us. I've been re-reading your original post. So you have a rollup defined that has Opportunity as the parent and child, yes? And you have some Apex code that is updating some Opportunity records. Say for example 10 of them, 5 of them belong to a parent Opp that is ok to update, 5 of them don't. Yet when the DLRS trigger processes its fails the whole DML operation, is this correct? This is regardless if you use the Database methods to allow partial updates, yes?

AlexIsMaking commented 8 years ago

Yes it sounds like we're getting more confused with every comment but we'll get there!

The child records that are related to the Opportunities are another object's. Here's the Lookup Rollup Summary record that's causing me the issue, as an example -

net fsr in year rollup

My process is reasonably complicated - the scheduled update changes Opportunity fields, an Opportunity trigger then fires which updates the related Monthly_Revenue_Forecast__c child records, those updates trigger the DLRS and then if the resulting update to the Aggregate Result Field (AFR) on the parent triggers the parent's validation rule or some other error, it's that error that I need to handle with the database class method.

So to answer the question in your first response, you would need to addError on the child records, to prevent them from being updated at any time throughout the course of the process because obviously if the child records are updated but the parent's AFR isn't then the AFR will probably be displaying an incorrect value. It sounds like you understood the implications for the end to end process before me there. But I am assuming that the error's on the child records won't block the updates on the remaining parent records in the scheduled update's batch, if they would, that would be an issue..

wes1278 commented 8 years ago

Are you trying this in a bulk context? If you are using the Salesforce ui, if the dlrs update due to a child update breaks a validation rule, it will throw an error and not allow the child update. I would think, in bulk, you just want to prevent the children updates for the children that would break a parent validation rule(or log them out) but you want the rest to succeed. Right?

AlexIsMaking commented 8 years ago

Thanks, yes this is in bulk and yes that's the behaviour I'm looking for. On Fri, 25 Dec 2015 at 15:57, Wes Weingartner notifications@github.com wrote:

Are you trying this in a bulk context? If you are using the Salesforce ui, if the dlrs update due to a child update breaks a validation rule, it will throw an error and not allow the child update. I would think, in bulk, you just want to prevent the children updates for the children that would break a parent validation rule(or log them out) but you want the rest to succeed. Right?

— Reply to this email directly or view it on GitHub https://github.com/afawcett/declarative-lookup-rollup-summaries/issues/259#issuecomment-167246743 .

afawcett commented 8 years ago

Ok...i think... this is what i ment by "Would the DLRS trigger just set the addError on the child records that correspond to the failed parents?" earlier. Its probably best to repo this in my test org and perhaps if your game invite you into the org take a look and confirm.

AlexIsMaking commented 8 years ago

That sounds good and yes I can certainly take a look at the org once you're set up.