14

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

   }

3 Answers 3

29

For the first line of your class, use this:

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

You have to implement

Database.AllowsCallouts
0
0

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

You must log in to answer this question.

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