Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Hi and Happy New year,

In accounting (at least double accounting), a financial move is always between 2 accounts, with one account being debited and the other account being credited.

I've wrote a small Apex trigger that is doing that.

trigger AccountingMoveLoading2 on Shipment__c (after update) {

    List<Accounting_Move__c> listaccountingmove = new List<Accounting_Move__c>();

    for (Shipment__c oshipment : trigger.New) {
        if (oshipment.Shipment_Completed__c == True) {
            Accounting_Move__c oAccounting_Move = new Accounting_Move__c();
            oAccounting_Move.Accounting_Move_Date__c = oShipment.ETD__c;
            oAccounting_Move.Accounting_Move_Description__c = oShipment.Invoice_Name__c;
            oAccounting_Move.From_Account__c = oShipment.Shipment_Account__c;
            oAccounting_Move.Transaction_Amount_Cred__c = oShipment.CIF_Value__c;
         listaccountingmove.add(oAccounting_Move);
         }
     }
     insert listaccountingmove;


    List<Accounting_Move__c> listaccountingmove2 = new List<Accounting_Move__c>();

    for (Shipment__c oshipment : trigger.New) {
        if (oshipment.Shipment_Completed__c == True) {
            Accounting_Move__c oAccounting_Move = new Accounting_Move__c();
            oAccounting_Move.Accounting_Move_Date__c = oShipment.ETD__c;
            oAccounting_Move.Accounting_Move_Description__c = oShipment.Invoice_Name__c;
            oAccounting_Move.From_Account__c = oShipment.Exporter_Account__c;
            oAccounting_Move.Transaction_Amount_Deb__c = oShipment.CIF_Value__c;
         listaccountingmove2.add(oAccounting_Move);
         }
     }
     insert listaccountingmove2;
}

It works and does do the job, but is seems a bit cumbersome. Any suggestion on how to make it more elegant? or efficient? Thanks.

share|improve this question
2  
Wouldn't you want your credit value to be -ve? (being pedantic, if they're accounting entries wouldn't you want equal/opposite amounts to net to 0.00?) –  Phil Hawthorn Jan 3 at 15:15
    
You are absolutely right! Actually very important and basic, not pedantic at all. –  Franky Jan 3 at 18:57
add comment

2 Answers 2

up vote 9 down vote accepted

Simplification of the trigger is one possibility:

   List<Accounting_Move__c> listaccountingmove = new List<Accounting_Move__c>();

    for (Shipment__c oshipment : trigger.New) {
        if (oshipment.Shipment_Completed__c) {

            Accounting_Move__c oAccounting_Move = new Accounting_Move__c(
            oAccounting_Move.Accounting_Move_Date__c = oShipment.ETD__c,
            oAccounting_Move.Accounting_Move_Description__c = oShipment.Invoice_Name__c);

            Accounting_Move__c oAccounting_Move2  =  oAccounting_Move.clone();

            oAccounting_Move.From_Account__c = oShipment.Shipment_Account__c;
            oAccounting_Move.Transaction_Amount_Cred__c = oShipment.CIF_Value__c;
            listaccountingmove.add(oAccounting_Move);

            oAccounting_Move2.From_Account__c = oShipment.Exporter_Account__c;
            oAccounting_Move2.Transaction_Amount_Deb__c  = - oShipment.CIF_Value__c; 
            listaccountingmove.add(oAccounting_Move2);
         }
     }

     insert listaccountingmove;

Alternatively, consider a workflow rule for the contra

share|improve this answer
1  
Slight bug in your answer, From_Account__c should be different for dr/cr line :) –  Phil Hawthorn Jan 3 at 15:17
    
Thanks for that (blush) - what would be the point of posting zero to ever account :) –  max Jan 3 at 15:28
    
:D you never know with accountants...they do the strangest of things –  Phil Hawthorn Jan 3 at 15:29
    
What about my edit @max ? –  Franky Jan 3 at 20:07
1  
OK I get it. Actually I've changed it your way on my trigger. It makes things even easier to calculate an account balance with roll up summaries. Thanks again for your advice and good tip. I didn't know the .clone() things. Very helpful! –  Franky Jan 3 at 21:20
show 3 more comments

Uses SObject constructor initialization for optimization, as well as combining both actions into a single loop, removed redundant == true.

trigger AccountingMoveLoading2 on Shipment__c (after update) {
    Accounting_Move__c[] moves = new Accounting_Move__c[0];

    for(Shipment__c record: Trigger.new) {
        if(record.Shipment_Completed__c) {
            Accounting_Move__c f = new Accounting_Move__c(
                    Accounting_Move_Date__c = record.ETD__c,
                    Accounting_Move_Description__c = record.Invoice_Name__c,
                    From_Account__c = record.Shipment_Account__c,
                    Transaction_Amount_Cred__c = record.CIF_Value__c),
                t = new Accounting_Move__c(
                    Accounting_Move_Date__c = record.ETD__c,
                    Accounting_Move_Description__c = record.Invoice_Name__c,
                    From_Account__c = record.Exporter_Account__c,
                    Transaction_Amount_Deb__c = record.CIF_Value__c
                );
            moves.addAll(new Accounting_Move__c[] { f, t });
        }
    }
    insert moves;
}
share|improve this answer
1  
insert moves should be outside the for loop? –  Doug B Jan 3 at 14:51
    
You're right. That was a typo. –  sfdcfox Jan 3 at 15:08
    
Thanks a lot @sfdcfox . This is also a nice way. –  Franky Jan 3 at 20:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.