Find your content:

Search form

You are here

Too many callouts: 1 in Salesforce Batch

 
Share

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());
}

}


Attribution to: user7715

Possible Suggestion/Solution #1

take a look on this, it should solve your issue...

System.LimitException: Too many callouts after set batch scope 1

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_timeouts.htm

Moreover, you can try these options,

  1. reduce your batch size
  2. see if you are calling callout inside a for loop which exceeds the limit

Attribution to: Bforce

Possible Suggestion/Solution #2

You have to implement

Database.AllowsCallouts

Attribution to: Tilak De

Possible Suggestion/Solution #3

For the first line of your class, use this:

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

Attribution to: DavidSchach
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31859

My Block Status

My Block Content