Skip to main content
edited body
Source Link
Raul
  • 18.9k
  • 5
  • 42
  • 70

I am trying to write unit test for a a trigger which calls batch apex which performs callout andperforming callouts first then updates the response to certainthose record.

Trigger's psuedo code looks like:

trigger AccountTrigger on Account (after update) {
    if(myCondition) {
        Database.executeBatch(new MyBatchClass(), 1);
    }
}

And batch is:

public class MyBatchClass implements Database.Batchable<SObject>, 
    Database.AllowsCallouts {
    
    // only relevant execute method
    public void execute(Database.BatchableContext batchableContext, 
    List<Account> accounts) {

        for(Account account: accounts) {

            // make http request callout

            // based on response update account
        }
    }
}

When I run my test test, it gives me error as:

System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

From the related question, I understand that Database.executeBatch is treated as a DML. When implementing this solution, I get:

System.CalloutException: Callout from triggers are currently not supported.

Please suggest any workaround or point towards any gap in the design. Any insight is highly appreciated.

I am trying to write unit test for a a trigger which calls batch apex which performs callout and updates the response to certain record.

Trigger's psuedo code looks like:

trigger AccountTrigger on Account (after update) {
    if(myCondition) {
        Database.executeBatch(new MyBatchClass(), 1);
    }
}

And batch is:

public class MyBatchClass implements Database.Batchable<SObject>, 
    Database.AllowsCallouts {
    
    // only relevant execute method
    public void execute(Database.BatchableContext batchableContext, 
    List<Account> accounts) {

        for(Account account: accounts) {

            // make http request callout

            // based on response update account
        }
    }
}

When I run my test test, it gives me error as:

System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

From the related question, I understand that Database.executeBatch is treated as a DML. When implementing this solution, I get:

System.CalloutException: Callout from triggers are currently not supported.

Please suggest any workaround or point towards any gap in the design. Any insight is highly appreciated.

I am trying to write unit test for a trigger which calls batch apex performing callouts first then updates the response to those record.

Trigger's psuedo code looks like:

trigger AccountTrigger on Account (after update) {
    if(myCondition) {
        Database.executeBatch(new MyBatchClass(), 1);
    }
}

And batch is:

public class MyBatchClass implements Database.Batchable<SObject>, 
    Database.AllowsCallouts {
    
    // only relevant execute method
    public void execute(Database.BatchableContext batchableContext, 
    List<Account> accounts) {

        for(Account account: accounts) {

            // make http request callout

            // based on response update account
        }
    }
}

When I run my test test, it gives me error as:

System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

From the related question, I understand that Database.executeBatch is treated as a DML. When implementing this solution, I get:

System.CalloutException: Callout from triggers are currently not supported.

Please suggest any workaround or point towards any gap in the design. Any insight is highly appreciated.

Source Link
Raul
  • 18.9k
  • 5
  • 42
  • 70

Testing a trigger calling batch class for making a callout gives System.CalloutException

I am trying to write unit test for a a trigger which calls batch apex which performs callout and updates the response to certain record.

Trigger's psuedo code looks like:

trigger AccountTrigger on Account (after update) {
    if(myCondition) {
        Database.executeBatch(new MyBatchClass(), 1);
    }
}

And batch is:

public class MyBatchClass implements Database.Batchable<SObject>, 
    Database.AllowsCallouts {
    
    // only relevant execute method
    public void execute(Database.BatchableContext batchableContext, 
    List<Account> accounts) {

        for(Account account: accounts) {

            // make http request callout

            // based on response update account
        }
    }
}

When I run my test test, it gives me error as:

System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

From the related question, I understand that Database.executeBatch is treated as a DML. When implementing this solution, I get:

System.CalloutException: Callout from triggers are currently not supported.

Please suggest any workaround or point towards any gap in the design. Any insight is highly appreciated.