[SalesForce] Help with calling future method from trigger

I installed Payment360 and I am trying to capture a transaction from a trigger. Payment360 offers the following documentation: payment360 documentation

Through trial and error I see that i need my trigger to call a future method which then calls the capture() method from the installed payment360 package. I had to do it this way because callouts from triggers are not allowed.

I have the below code for testing using ID=a0I46000000jg6c, but ultimately i want to use the ID from the record that initiates the trigger.

  1. How would I do this?
  2. I don't think my current code is working, but I get no errors.

Apex Class:

global class FutureClass
{
    @future
    public static void captureFuture()
    {  

         try {
    bt_stripe.P360_API_v1.Tra t = bt_stripe.P360_API_v1.transactionFactory('a0I46000000jg6c');
    t.capture();

    //bt_stripe.P360_API_v1.P360_Exception if something goes wrong with committing records in database
    bt_stripe.P360_API_v1.commitWork();
} catch (Exception ex) {
    //do error handling here
}

    }
}

Trigger:

trigger CaptureTrans on bt_stripe__Transaction__c (after insert, after update) {

    for (bt_stripe__Transaction__c pt : Trigger.new) {

    if (pt.GameSet__c) {

        FutureClass.captureFuture();

    }
}

}

Best Answer

The first thing to do is remove the current try/catch block you have in captureFuture if it is just a comment of //do error handling here as you show in the question. It will be catching and then ignoring any potentially useful details as it is shown. That is of course assuming the method actually appears as it does in the question. If you've got meaningful exception handling then feel free to disregard this step.

Once you know that any exceptions will actually be recorded use the Developer Console to capture the debug logs. You will see two log entries when inserting a bt_stripe__Transaction__c record. One for the trigger transactions, and one a short time later for the future method transaction.

Open the one for the future method and observe what is actually occurring.


Within your trigger, you can pass a list of bt_stripe__Transaction__c Id's to the future method. Ideally the transactionFactory method will also be able to work with a collection of Id's rather than one at a time. As a guess you might only need a single call to commitWork() for all the IDs.

Related Topic