ABSYZ ABSYZ

  • Home

    Home

  • About us

    Who We Are

  • Our Expertise

    What we Do

  • Our Approach

    How We Do It

  • Products

    What We Made

  • Industries

    Who We Do It For

  • Clients

    Whom We Did It For.

  • Article & Blogs

    What Experts Think

  • Careers

    Join The Team

  • Get In Touch

    Let’s Get Started

ABSYZ

Making Callouts with Batch Apex for Data of over 12 MB

Home / Article & Blogs / Apex / Making Callouts with Batch Apex for Data of over 12 MB
By adityamoro inApex, Integration

Those who often work on integrations of Salesforce with other platforms must be familiar with the restrictions imposed on callout requests or responses. For those who are not so familiar, the maximum heap size of callouts (either HTTP or WebService calls) is 6 MB for synchronous apex and 12 MB for asynchronous apex. Not so often, this comes across as a serious handicap to developers.

This hack involves a batch class to making a callout to another class that makes the actual HTTP callouts. Let us consider a scenario. Suppose you have to import a large number of attachments into Salesforce on a daily basis. These attachments are to go under Accounts created on a daily basis. In order to get the attachments, we have to make the callouts from the finish() method. So, first we write the batch class. Assuming it looks something like below:

[sourcecode language=”java”]
global class mybatchclass Implements Database.Batchable <sObject>,database.stateful,database.allowcallouts {

//variable to be used in SOQL
public Date today = system.today();
public set<ID> finalaccounts = new set<Id>();
public String SOQL = ‘SELECT Id, Name, Type from Account where CreatedDate =: today’ ;

// Start Method of Batch class
global Database.queryLocator start(Database.BatchableContext bc){
// Query the records
return Database.getQueryLocator(SOQL);

}

// Execute Method of Batch class
global void execute(Database.BatchableContext bc, List<Account> accountlist) {

// Iterate over the list of contacts and get their Account ids
for(Account cc:accountlist){
finalaccounts.add(cc.Id);
}

}

// Finish Method of Batch class. This is where you make the callout
global void finish(Database.BatchableContext bc) {

attachfiles.attachfilestoaccount(finalaccounts);
//make the callout here for getting the attachments data of size greater than 12 MB

}

}
[/sourcecode]

Now, lets clear a few things off here before we proceed any further. We are going for Database.Stateful as we need to maintain the state of the Accounts set variable over multiple batches. This is a particular advantage of using batch classes even though the class will become serialized and result in longer execution time. More importantly, we are implementing the Database.AllowsCallouts interface. This is more important in the current scenario where, once we get the list of Accounts, we are making the callout from the finish () method of the batch class.

The batch class can be called either from another class or from the anonymous debug window. To keep things simple, lets initiate the batch class from the anonymous debug window. The syntax will look like the code below:

[sourcecode language=”java”]
// You can invoke the batch class from the anonymous debug window with the below syntax
mybatchclass mb = new mybatchclass();
Database.executebatch(mb);
[/sourcecode]

Now, lets start the callout class. You will be passing the set of AccountIDs to the method in this class.

[sourcecode language=”java”]
public class attachfiles(){

public static void attachfilestoaccount(set<id> listaccount)
{
// First set the callout parameters such as the authToken, Endpoint and the accessToken
String authToken = ‘test authorization token’;
String authEndpoint = ‘test authEndpoint’;
String calloutEndpoint = ‘test calloutEndpoint’;
String accessToken = ‘test_act’;

// Now make the HTTP callout
Http h1 = new Http();
HttpRequest hreq2 = new HttpRequest();
String dealId = ”;
hreq2.setEndPoint(calloutEndpoint);
hreq2.setMethod(‘POST’);
hreq2.setHeader(‘Content-type’,’application/xml’);
hreq2.setHeader(‘Authorization’,’Bearer’+’ ‘+accessToken);
hreq2.setTimeout(30000);

hreq2.setBodyAsBlob(Blob.valueOf(‘testClass’));

// Get the response which contains the attachment
HttpResponse res = h1.send(hreq2);

List<Attachment> atLst = new List<Attachment>();

for(Account acc:listaccount){
Attachment att1 = new Attachment();
att1.Body = Blob.valueOf(res.getbody());
att1.Name = String.valueOf(‘Response.txt’);
att1.ParentId = acc.Id;
atLst.add(att1);
}

// Finally, insert the list
if(atLst.size()> 0)
insert atLst;

}
}
[/sourcecode]

The attachfilestoaccount() method provide simple steps to integrate with another platform. Note that we are getting the Attachments in the form of HTTP responses. Finally, we are iterating over the Account set and inserting the attachments for them.

110
Like this post
2 Posts
adityamoro

Search Posts

Archives

Categories

Recent posts

How To Upload A File To Google Drive Using LWC

How To Upload A File To Google Drive Using LWC

How To  Generate A PDF Using JSPDF In LWC

How To  Generate A PDF Using JSPDF In LWC

Create a No-Code Facebook Messenger Channel in the Service Cloud

Create a No-Code Facebook Messenger Channel in the Service Cloud

Salesforce Sales Cloud Features & Pricing

Salesforce Sales Cloud Features & Pricing

Salesforce Service Cloud: What Is It? And Its Features

Salesforce Service Cloud: What Is It? And Its Features

  • Previous PostWinter '18: Uploading Files in lightning Component
  • Next PostApex Documentation Using ApexDoc

Related Posts

How To Upload A File To Google Drive Using LWC
Integration Salesforce

How To Upload A File To Google Drive Using LWC

Image Generation Using ChatGPT Inside Salesforce
Integration Salesforce

Image Generation Using ChatGPT Inside Salesforce

PLATFORM EVENTS IN SALESFORCE
Integration Salesforce

PLATFORM EVENTS IN SALESFORCE

Integrate SharePoint with Salesforce using Microsoft Graph API
Apex Integration Salesforce

Integrate SharePoint with Salesforce using Microsoft Graph API

1 Comment

  1. Making Callouts with Batch Apex for Data of over 12 MB — ForceOlympus – SutoCom Solutions
    Reply
    11 December 2017
    Reply

Leave a Reply (Cancel reply)

Your email address will not be published. Required fields are marked *

*
*

ABSYZ Logo

INDIA | USA | UAE | CANADA

  • About us
  • Article & Blogs
  • Careers
  • Get In Touch
  • Our Expertise
  • Our Approach
  • Products
  • Industries
  • Clients
  • White Papers

Copyright ©2022 Absyz Inc. All Rights Reserved.

Copy
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “ACCEPT ALL”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent. Privacy Policy
Cookie SettingsREJECT ALLACCEPT ALL
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.

CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.

Functional

Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.

Performance

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Analytics

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.

Advertisement

Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.

Others

Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.

SAVE & ACCEPT