0

I think my code is wrong.

Last version of my HttpGet/Callout code (it works): How to use HttpGet service to get a list of objects and create them? Now I try to make it batch. My Execute Anonymous Window part: Id batchJobId = Database.executeBatch(new Callout(), 300);

In debug window I get just 'Start!'. Why I can't get another debug's string? Now my code doesn't work (I also can't get recorts from HttpGet).

Code:

global class Callout implements Database.Batchable<sObject>, Database.AllowsCallouts {    
public String query = 'SELECT First_Name__c, Last_Name__c FROM AnotherItem__c';
global Database.QueryLocator start(Database.BatchableContext BC) {
    System.debug('Start!');
    return Database.getQueryLocator(query);
}

public class Callout {    
public static String webTokenBody(){
    String requestBody = 'my_web_token_string';
    return request;
}

public class webTok{
    public String webToken{get;set;}    
}

public static HttpRequest createRequestForToken(String service){
    String requestBody = webTokenBody();
    HttpRequest ourRequest = new HttpRequest();
    ourRequest.setBody(requestBody);
    ourRequest.setMethod(service);
    ourRequest.setEndpoint('https://my.salesforce.com/services/oauth2/token');
    return ourRequest;
}

public static HttpRequest createRequestForService(String token, String method){
    HttpRequest finalRequest = new HttpRequest();
    finalRequest.setHeader('Authorization','Bearer ' + token);
    finalRequest.setHeader('Content-Type','application/json');
    finalRequest.setHeader('accept','application/json');
    finalRequest.setMethod(method);
    finalRequest.setEndpoint('https://my.salesforce.com/services/apexrest/endpoint/');
    return finalRequest;
}

global void execute(Database.BatchableContext BC, List<AnotherItem__c> records) {
    System.debug('Process...');
    for(integer i=0; i<records.size(); i++){
        Http ourHttp = new Http();
        HttpRequest requestForToken = createRequestForToken('GET');
        HttpResponse responseToken = ourHttp.send(requestForToken);
        webTok objAuthenticationInfo = (webTok)JSON.deserialize(responseToken.getbody(), webTok.class);

        if(objAuthenticationInfo.webToken != null){
            HttpRequest requestForService = createRequestForService(objAuthenticationInfo.webToken, 'GET');
            HttpResponse responseService = ourHttp.send(requestForService);
            System.debug('ALL RECORDS:  ' + responseService.getBody());
        }
    }
}

global void finish(Database.BatchableContext BC){
    System.debug('Finish!');
}
}
  • Does the query SELECT First_Name__c, Last_Name__c FROM AnotherItem__c return any result? Have you tried checking this? – Hemant Jain Nov 22 '18 at 12:01
  • It can't get to the execute() method. – MichaelLev19 Nov 22 '18 at 12:04
  • Are you getting proper access token – Pranay Jaiswal Nov 22 '18 at 12:08
  • Yes. it worked without batch: salesforce.stackexchange.com/questions/239876/… – MichaelLev19 Nov 22 '18 at 12:15
  • 1
    Okay can you change the batch size as 50? Database.executeBatch(new Callout(), 300); You can do only 100 callouts in 1 transaction or 100callouts in excute – Pranay Jaiswal Nov 22 '18 at 12:17
1

Check if the AnotherItem__c object has any records or not. Also since you are writing a batch class, it would call the execute method n/scope times, where 'n' is the number of records fetched from the query in the start() method. In your case, it might not return any records so the execute() method won't be called at all.

Also the debugs for all the methods in a batch class are generated differently. You'll find the debug for execute method in a different log. It should have a specific name like SerialBatchApexRangeChunkHandler in the debug logs.

  • It has records, of course. I get my records for upsert from HttpGet service: salesforce.stackexchange.com/questions/239876/… So I don't understand why I still haven't that. – MichaelLev19 Nov 22 '18 at 12:11
  • 1
    A batch class would work only when it gets a result from the query that you make in the start method. In your case, the query is SELECT First_Name__c, Last_Name__c FROM AnotherItem__c. If this does not return any results, it will never call the execute method. The answer that you linked fetches the records from a different org and not the one in which your batch is running. – Saket Joshi Nov 22 '18 at 12:14
  • Thank you. I fixed a number of records. Now it calls execute method. But I can't get changed records from HttpGet anyway :( – MichaelLev19 Nov 22 '18 at 12:22
1

A transaction can do at max 100 callouts, so the Batch size should be less than or equal to 100

Now, I can also see you are doing the callout to get accesToken in loop, you can move it away from loop

global void execute(Database.BatchableContext BC, List<AnotherItem__c> records) {
    System.debug('Process...');
    HttpRequest requestForToken = createRequestForToken('GET');
    HttpResponse responseToken = ourHttp.send(requestForToken);
    webTok objAuthenticationInfo = (webTok)JSON.deserialize(responseToken.getbody(), webTok.class);
    for(integer i=0; i<records.size(); i++){
        Http ourHttp = new Http();


        if(objAuthenticationInfo.webToken != null){
            HttpRequest requestForService = createRequestForService(objAuthenticationInfo.webToken, 'GET');
            HttpResponse responseService = ourHttp.send(requestForService);
            System.debug('ALL RECORDS:  ' + responseService.getBody());
        }
    }
}

Database.executeBatch(new Callout(), 99);

Would be the optimal value for batch execution.

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.