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

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.

75
Like this post
2 Posts
adityamoro

Search Posts

Archives

Categories

Recent posts

10 Amazing Benefits of Salesforce Marketing Cloud

10 Amazing Benefits of Salesforce Marketing Cloud

How to Set SMART Goals for Employee Performance?

How to Set SMART Goals for Employee Performance?

Difference Between ERP and CRM? What Is Their Importance?

Difference Between ERP and CRM? What Is Their Importance?

What You Need To Know About India Salesforce Days 2022 – Platform Accredited Professional

What You Need To Know About India Salesforce Days 2022 – Platform Accredited Professional

<strong>Top digital transformation challenges affecting the businesses in 2022</strong>

Top digital transformation challenges affecting the businesses in 2022

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

Related Posts

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

Cyber-security in an uncertain world
Apex

Cyber-security in an uncertain world

REST API call from Einstein Analytics Dashboard
Apex REST Salesforce Salesforce Einstein Wave Analytics

REST API call from Einstein Analytics Dashboard

2 Comments

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

    Hi..If we need to get new data from external api and store in a new custom object in salesforce and run batch on daily basis how do we pass the list of data to start method from callout and response and insert?

    Reply

Leave a Reply (Cancel reply)

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

*
*

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

ABSYZ Software Consulting Pvt. Ltd.
USA: 49197 Wixom Tech Dr, Wixom, MI 48393, USA
M: +1.415.364.8055

HYD – India: 6th Floor, SS Techpark, PSR Prime, DLF Cyber City, Gachibowli, Hyderabad, Telangana – 500032
M: +91 79979 66174

2nd Floor, 91Springboard, Padmavathi Complex, 80 Feet Rd, Koramangala, 8th Block, Bengaluru, Karnataka-560095.
+91 8886332345

Copyright ©2020 Absyz Inc. All Rights Reserved.

youngsoft
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