Too many callouts: 1 in Salesforce Batch

When i am sending one HTTPRequest it’s giving error : System.LimitException: Too many callouts: 1

Don’t understand why…pls help me out.

Thanks!

Here is my code:

 global class ClassName implements Database.Batchable<sObject>, Schedulable{
     String query;
     ////////////////////////////////////////////
    // Constructor
     ////////////////////////////////////////////
global ClassName(){

    query = 'SELECT fields FROM object WHERE '; 
    query += criteria LIMIT 1';
}

//////////////////////////////////////////////////
// Batchable Methods
//////////////////////////////////////////////////  
global Database.QueryLocator start(Database.BatchableContext BC) {
    return Database.getQueryLocator(query);
}

// EXECUTE - For update google URL
global void execute(Database.BatchableContext BC, List<sObject> scope)
{
    if(scope != null)
    {
        for(SObject scopeRecord : scope)
        {
            ENT_ERC_Resource__c libraryContent = new ENT_ERC_Resource__c();
            libraryContent = (ENT_ERC_Resource__c)scopeRecord;

            Http httpVar = new Http();
            HttpRequest req = new HttpRequest();
            string firstImageURL =  libraryContent.Thumbnail__c;
            req.setEndpoint(firstImageURL);
            req.setMethod('GET');
            req.setHeader('Content-Type', 'image/png');
            req.setCompressed(true);
            req.setTimeout(60000);

            HttpResponse res = new HttpResponse();
            res = httpVar.send(req);

            string responseValue = '';
            responseValue = res.getBody();
            if(responseValue != null && responseValue != '' && !responseValue.contains('Error 404'))
            {
                blob image = res.getBodyAsBlob();

                Attachment resourceAttachment = new Attachment();
                //You will want to tie your attachment to some type of custom or standard object
                resourceAttachment.ParentId = libraryContent.Id;
                resourceAttachment.Name = libraryContent.Name + '_Thumbnail.png';
                resourceAttachment.Body = image;

                resourceAttachment.contentType = 'image/png';
                insert resourceAttachment;
                libraryContent.Thumbnail__c = '/servlet/servlet.FileDownload?file=' +  resourceAttachment.id;
                Update libraryContent;
            }
        }
    }
}

global void finish(Database.BatchableContext BC)
{
}

global void execute(SchedulableContext sc)
{
    Database.executeBatch(new ENT_ERC_BatchResourceUpdate());
}

}

Answer

For the first line of your class, use this:

global class ClassName implements Database.Batchable, Schedulable, Database.AllowsCallouts {

Attribution
Source : Link , Question Author : user7715 , Answer Author : DavidSchach

Leave a Comment