11

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

}

20

For the first line of your class, use this:

global class ClassName implements Database.Batchable, Schedulable, Database.AllowsCallouts {
| improve this answer | |
6

You have to implement

Database.AllowsCallouts
| improve this answer | |
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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.