function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KruviKruvi 

System.LimitException: Too many callouts: 1

Hi


I'm piloting an app I wrote.

I've written a Batch Apex that processes by records and for each performs some logic including sending one HTTP request and one database.insert() operation.

 

I set my batch size to 1 since the limit on callouts within a batch operation is 1.


Problem is that I'm still getting an exception: System.LimitException: testtt:Too many callouts: 1

 

This is my code:

 

	
...	

if(doBatch){
	MicroCampaignBatchPublish batch = new MicroCampaignBatchPublish(GW.Name, template.From_Number__c, 
									campaign.Greeting__c, campaign.SMS_Text__c, 
									campaign.Id, campaign.Campaign_num__c, 
									template.Id);		

	String queryAll = 'SELECT ID, Name, Member_Num__c, Contact__c, Contact__r.FirstName, Contact__r.MobilePhone FROM Micro_Campaign_Member__c WHERE   Contact__r.DoNotCall = false AND Active__c = true AND Micro_Campaign__c = \'' + campaignId + '\'';

	batch.query = queryAll;      		

	ID batchId = Database.executeBatch(batch, 1);
}

 

and then:

 

global void execute(Database.BatchableContext BC, List<Micro_Campaign_Member__c> scope){
					   				
			
for(Micro_Campaign_Member__c member :scope){
			
		...

		SMSBody = myGreeting + ' ' + member.Contact__r.FirstName +', ' + SMSBody;

		try{				
			//This is where the callout happens
                        sms = sender.sendASingleSMS(member.Contact__r.MobilePhone, member.Contact__r.FirstName, SMSBody);
		}
		catch(Exception e){
		   	system.debug(Logginglevel.DEBUG, 'problem sending SMS: ' + e.getTypeName());			
		   	system.debug(Logginglevel.DEBUG, 'problem sending SMS: ' + e.getMessage());			
		   	system.debug(Logginglevel.DEBUG, 'problem sending SMS: ' + e.getCause());			
		}			
				
		//sentSMSs.add(sms); 
		MicroCampaignAPI.insertSMSs(new List<SMS__c> {sms});				
	...
		   										
	}			
    }

 


Does anyone knows what is the problem?

I need to release this app ASAP - Please help.


Many Thanks

Best Answer chosen by Admin (Salesforce Developers) 
KruviKruvi

The problem was I didn't include the Database.AllowsCallouts on the class definision.

 

This should have been it:

 

global public with sharing class MicroCampaignBatchPublish    implements Database.Batchable<sObject>,    Database.AllowsCallouts, Database.Stateful{

 

...

 

}

All Answers

myforcedotcommyforcedotcom

The scope variable is the List<Micro_Campaign_Member__c> which is filled by your batch.query.

Since your query does not have a "limit 1" you could be getting multiple records.

 

When the batch executes, it iterates thru the List of Micro_Campaign_Member__c, which could be 1 record or 2000 records.

Your failing when it is iterating thru the batch and making the callout per record in the batch.

 

 

 

 

KruviKruvi

The problem was I didn't include the Database.AllowsCallouts on the class definision.

 

This should have been it:

 

global public with sharing class MicroCampaignBatchPublish    implements Database.Batchable<sObject>,    Database.AllowsCallouts, Database.Stateful{

 

...

 

}

This was selected as the best answer