0

I am having an issue with a trigger which i have put together using a couple of similar posts.

It fires on an Order, to update a field called Outstanding_Balance__c on the Order's Account, which should collect all outstanding balances from all associated Orders.

The field on the order is a Roll Up Summary, called Outstanding_Balance__c.

There is no validation, and both the Order and the Account can be updated manually (any mandatory fields are fulfilled.

The trigger successfully updates the Account field if I simply replace the line -

acc.Outstanding_Balance__c = 0.00;

with

acc.Outstanding_Balance__c = 2.00;

So it is firing correctly, otherwise, the Outstanding_Balance__c field on Account isn't being calculated. It seems the following line is not working as intended.

acc.Outstanding_Balance__c += ord.Outstanding_Balance__c;

Full code:

trigger OrderTrigger on Order (After insert, After update, After delete, After undelete){

    Set<id> AccountIds = new Set<id>();

    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUndelete){
        for (Order ord : Trigger.new){
            AccountIds.add(ord.AccountId);
        }
    }
    If(Trigger.IsDelete){
        For(Order ord: Trigger.Old){
            AccountIds.add(ord.AccountId);
        }
    }

    List<Account> accsToUpdate = New List<Account>();
    For(Account acc : [SELECT Id,Name,Outstanding_Balance__c,Sales_Order_Count__c,(
        SELECT id,name,Outstanding_Balance__c FROM Orders__r) FROM Account WHERE Id IN :AccountIds]) {
        {
            acc.Outstanding_Balance__c = 0.00;

            for(Order ord: acc.Orders__r) {
                acc.Outstanding_Balance__c += ord.Outstanding_Balance__c;
                acc.Sales_Order_Count__c = acc.Orders__r.size();

            }
            accsToUpdate.add(acc);                 
        }
        try{
            update accsToUpdate;
        }
        catch(Exception E){
            system.debug('Error thrown is: ' + E.getMessage());
        }
    }
}
Adrian Larson
  • 149,971
  • 38
  • 239
  • 420
TheMikeyBoosh
  • 517
  • 12
  • 30
  • 1
    Could you please let us know what is it that is not working? Any errors or exceptions? – Mohit Arora May 11 '18 at 06:46
  • @sfdcfox no, this doesn't help as it is a different question. He is having issues on delete, I have some kind of syntax issue – TheMikeyBoosh May 11 '18 at 07:01
  • @sfdcfox whilst you make a fair point, you are missing the bigger picture. I have looked at other posts to come to a conclusion to ask for help. I am not a developer, so i have posted my issue here. Effectively saying "Just learn how to do it" is not particularly helpful on your part. Please re-open the question, so someone can help me understand how to better write this scenario. – TheMikeyBoosh May 11 '18 at 07:10
  • 2
    @TheMikeyBoosh I've reopened it at your request. However, you may still suffer another closure if you don't include any additional information. At minimum, you should [edit] in pertinent information, such as the fact that you've looked at other posts, any errors you see, any relevant entries in your debug logs. Your code looks like it should at least tally correctly (except for some edge cases), but we don't know what's wrong, and so will have to play a guessing game. At least review this question for hints on debugging. – sfdcfox May 11 '18 at 07:19
  • @sfdcfox I will update post with your suggestions and debug as best i can. Thanks. – TheMikeyBoosh May 11 '18 at 07:38
  • @TheMikeyBoosh That's all that we ask. I'm going to bed, but I'll take a look again in the morning to make sure you're taken care of. – sfdcfox May 11 '18 at 07:39

1 Answers1

2

If you're trying to roll up data from child to parent, I really highly recommend you use clicks not code and install Declarative Lookup Rollup Summaries. Even if you can regularly rip off dozens of these triggers a week with no errors, a tool like this one is a vast improvement as it is generic and configurable by any admin with access to the objects. Another advantage is that as soon as you are done configuring them, you can run a batch to backfill the values on all existing records.

If you install this tool, you would then just create two Lookup Rollup Summary records, one which counts Order records under an Account, and another which sums Outstanding_Balance__c.

I can post more specific steps if you get stuck, but the tool itself is very intuitive and I think you'll find you can have these triggers up and running in an hour or so.

Adrian Larson
  • 149,971
  • 38
  • 239
  • 420
  • 1
    Thanks for your input Adrian, but this does not answer the original post's issue. I have used the tool before, but would prefer to nail this trigger instead. The tool is clunky, and adds many components which will confuse the low level admin using the system going forward. – TheMikeyBoosh May 11 '18 at 21:37