+ Start a Discussion
Somya TiwariSomya Tiwari 

Missing transactions from a trigger

I have a Webservice which sends me transactions. We want to track the changes record by record just as a Bank Statement. We are using webservice to store Credit and Debit amount.

In the trigger we are quering for the last record [I know this is not what best practice says, but was not able think of a better way] and then calculating the balance for this record.

trigger:

trigger ASM_Balance on Assembly_Transactions__c (before insert) {
    
    List<Assembly_Transactions__c> trans = [SELECT ID, balance__c FROM Assembly_Transactions__c WHERE Wallet_Owner__c =: Trigger.new[0].Wallet_Owner__c ORDER BY CreatedDate DESC LIMIT 1];
    
    Double balance = 0;
    if(trans.size()>0)
    {
        if(trans[0].balance__c != null)
            balance = trans[0].balance__c;
    }
    Assembly_Transactions__c tr = Trigger.new[0];
    Double cr = tr.credit__c;
    Double dt = tr.debit__c;
    Double amount = balance+cr-dt;
    tr.balance__c = amount;
}

The error occours when we have two transactions at the same time, and the webservice is hit back to back. 

I think this is due to SOQL being run just before we have the last transaction inserted successfully. 

Please help me out with suggestions.

Prady01Prady01
Hi, I am little confised from your requerment, But have made an effort to understand and pupose and solution. 

1) You dont need the query line number 03 in your code. Below is my code sample.
 
trigger ASM_Balance on Assembly_Transactions__c (before insert, before update) {

     for(ASM_Balance asm: trigger.new){
          // You will get all the newly updated the records and well as newly inserted records.
     }

}
Hope this helps!
​​​​​​​Prady01
Somya TiwariSomya Tiwari

Hi,

thanks for your proposed solution, but it will not work for my use case. I need to get the previous transaction which will not get updated or inserted. but i don't want to miss any single transaction.

Please find here with the report i need to build.

Balance Report ScreenShot

Dushyant srivastava 8Dushyant srivastava 8
Hi Somya,

Just like Prady I am also confused with the requirement but as you said that you need to do the balance chec just like Banks do an dI am familiar with Financial domain, I would like to share my thoughts. 

How Baking Database is Handeled:

The most important think what customer have in Bank's is there Account Number and Account Type. In terms of salesforce that could be Opportunity with Autonumber or the unique field.
Secondly, Customer Details need to be store in the DB(In SF thats Contact).
And Last is the Transaction(There should be a related clid object of Opportunity store all the info of it).

Solution:

What you can do is that you can store all the transactions happed for an account in a Custom Object(I am calling that Transactions__c) which will have master detailed relationship with opportunity and all the credit and debit will be store in two fields in Opportunity(Roll up Summary). The Difference of both will be the Balance.

Best Reagrds,
Dushyant Srivastava
Somya TiwariSomya Tiwari

Hi Dushyant,

Thanks for your time!
Actually we have successfully implemented that part. The issue with our system is when there are more than one transaction at our endpoint at the same time, the balance is missing certain transactions. The Transaction gets recorded, but the balance misses that one.

Dushyant srivastava 8Dushyant srivastava 8
@Somya - As you have not share the actual cause of Issue "Webserivce class" Its hard to comment about that part. Still as per the Coding point I will not suggest you to write a trigger and handle the things in webservice.

Reason for this is a siple one. You are make DMLS on the same record from webservice then in trigger. Please avoid that and do the necessary things in the webservice.

Please share a bit about webservice to get things much more clear.

Best Regards,
Dushyant Srivastava
Somya TiwariSomya Tiwari

Hi Dushyant !

The Webservice class is kind of simple. We simply take the values and insert them, there is nothing so complicated that will cause an issue. Even if i add the things in trigger onto the webserivce, i will still be left with an issue. 

What i have thought is to use the transaction time i have got, and sort all the transactions by that time. once i have the time with me, i will simply write a batch class to solve the wrong balance issue.