0

I need to create box.com folder for 500 Account records. Inside that Account box folder will be 12 other folders. I am using box.com's toolkit for Salesforce.

I am using a batch apex class and invoking it from the dev console. Here is the code.

global class AccountBoxFolderCreationBatch implements Database.Batchable<Sobject>, Database.AllowsCallouts, Database.Stateful {

    global Database.querylocator start(Database.BatchableContext BC) {
        Id householdAccountRTId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Household').getRecordTypeId();
        return Database.getQueryLocator([
            select
                Id, 
                Box_Folder_Id__c, 
                Financial_Accounts_Parent_Box_Folder_Id__c, 
                CLG_Opportunities_Parent_Box_Folder_Id__c, 
                Real_Estate_Assets_Parent_Box_Folder_Id__c, 
                Tax_Documents_Parent_Box_Folder_Id__c, 
                Medical_Records_Parent_Box_Folder_Id__c,
                Funding_Parent_Box_Folder_Id__c,
                Underwriting_Parent_Box_Folder_Id__c,
                Registration_Parent_Box_Folder_Id__c,
                Compliance_Parent_Box_Folder_Id__c,
                Contact_Parent_Box_Folder_Id__c,
                Performance_Reports_Parent_Box_Folder_Id__c,
                Opportunity_Parent_Box_Folder_Id__c
            from Account 
            where RecordTypeId = :householdAccountRTId
            and Box_Folder_Id__c = null
        ]);
    }

    global void execute(Database.BatchableContext BC, List<Account> scope) {
        box.Toolkit boxToolkit = new box.Toolkit();
        for(Account account : scope) {
            account.Box_Folder_Id__c = boxToolkit.createFolderForRecordId(account.Id, null, false);
            account.Financial_Accounts_Parent_Box_Folder_Id__c = boxToolkit.createFolder('Financial Accounts', account.Box_Folder_Id__c, null);
            account.CLG_Opportunities_Parent_Box_Folder_Id__c = boxToolkit.createFolder('CLG Opportunities', account.Box_Folder_Id__c, null);
            account.Real_Estate_Assets_Parent_Box_Folder_Id__c = boxToolkit.createFolder('Real Estate Assets', account.Box_Folder_Id__c, null);
            account.Tax_Documents_Parent_Box_Folder_Id__c = boxToolkit.createFolder('Tax Documents', account.Box_Folder_Id__c, null);
            account.Medical_Records_Parent_Box_Folder_Id__c = boxToolkit.createFolder('Medical Records', account.Box_Folder_Id__c, null);
            account.Funding_Parent_Box_Folder_Id__c = boxToolkit.createFolder('Funding', account.Box_Folder_Id__c, null);
            account.Underwriting_Parent_Box_Folder_Id__c = boxToolkit.createFolder('Underwriting', account.Box_Folder_Id__c, null);
            account.Registration_Parent_Box_Folder_Id__c = boxToolkit.createFolder('Registration', account.Box_Folder_Id__c, null);
            account.Compliance_Parent_Box_Folder_Id__c = boxToolkit.createFolder('Compliance', account.Box_Folder_Id__c, null);
            account.Contact_Parent_Box_Folder_Id__c = boxToolkit.createFolder('Contact', account.Box_Folder_Id__c, null);
            account.Performance_Reports_Parent_Box_Folder_Id__c = boxToolkit.createFolder('Performance Reports', account.Box_Folder_Id__c, null);
            account.Opportunity_Parent_Box_Folder_Id__c = boxToolkit.createFolder('Opportunities', account.Box_Folder_Id__c, null);
        }
        boxToolkit.commitChanges();
        update scope;
    }

    global void finish(Database.BatchableContext BC) {
    }
}

However it keeps giving me the following error.

First error: box:Too many callouts: 101

I even tried reducing the number of batches by doing

AccountBoxFolderCreationBatch objTest = new AccountBoxFolderCreationBatch();
Database.executebatch(objTest, 50);

But it still fails.

Where am I going wrong?

1 Answer 1

2

So you can do at max 100 callouts in a transaction.

As I can see you are Setting batch scope as 50. Database.executebatch(objTest, 50);

As you say, for each Account you will be making 12 Folder ie 12 Callouts.

So in each iteration of execute of Batch, you need 12(Folder) *50 (Account) = 600 callouts.

As we cannot do 600 Callouts in execute, you need to change the scope of your batch to the optimal value.

100(Callout)/12(Folder) = 8.3

that means your scope will be 8.

thus the fix will be:

AccountBoxFolderCreationBatch objTest = new AccountBoxFolderCreationBatch();
Database.executebatch(objTest, 8);

Edit: It looks like more than 1 callouts are used by Box API to create a folder. We have 2 options,do trial and error method or, try to find out how many callouts are taken by 1 account. We would try calling execute method to see a number of callouts made.

List<Account> AccList=[
            select
                Id, 
                Box_Folder_Id__c, 
                Financial_Accounts_Parent_Box_Folder_Id__c, 
                CLG_Opportunities_Parent_Box_Folder_Id__c, 
                Real_Estate_Assets_Parent_Box_Folder_Id__c, 
                Tax_Documents_Parent_Box_Folder_Id__c, 
                Medical_Records_Parent_Box_Folder_Id__c,
                Funding_Parent_Box_Folder_Id__c,
                Underwriting_Parent_Box_Folder_Id__c,
                Registration_Parent_Box_Folder_Id__c,
                Compliance_Parent_Box_Folder_Id__c,
                Contact_Parent_Box_Folder_Id__c,
                Performance_Reports_Parent_Box_Folder_Id__c,
                Opportunity_Parent_Box_Folder_Id__c
            from Account 
            where RecordTypeId = :householdAccountRTId
            and Box_Folder_Id__c = null limit 1
        ];

new AccountBoxFolderCreationBatch ().execute(null,AccList);
System.debug('Callouts Used are:'+LIMITS.getCallouts());

Once we see the debug log, we can find the optimal batch size needed.

Src: Callout limits

7
  • Still got the same error. With 8, the number of batches were 60. Commented May 11, 2018 at 14:07
  • Can Call the execute method directly by passing 1 Account record to see how many callouts are needed for 1 account? Commented May 11, 2018 at 14:13
  • OK, so I did Database.executebatch(objTest, 1); and the total batches it shows is 480 Commented May 11, 2018 at 14:20
  • This is weird because the number of account records I want to update are 480 as well. What is going on?? Commented May 11, 2018 at 14:21
  • So batch is trying to call execute for each account, and execute will be called 480 times. Hence. So keep increasing batch size by 1 till you reach a point that it just start giving you 100 callout exception. Commented May 11, 2018 at 14:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .