2

So my batch will collect 843 records, for each batch, which is 200 records, i constructed a json and made the callout, so in total, it is 5 batches and 5 callouts. I have a server hosting my rest service, on the server side, I saw the response looks fine. But on the salesforce side, I only received response from last callout, which is the 43 records one.

Any idea why ?

here is the batch:

global class BatchCheckActiveProducing implements Database.Batchable<sObject>, Schedulable, Database.AllowsCallouts{

    global BatchCheckActiveProducing(){

    }

    global Database.QueryLocator start(Database.BatchableContext bc){
        String queryProjects = 'Select Id, Locus_ComponentId__c, Date_System_Actively_Producing__c from dsProject__c where ' + 
                                'Locus_ComponentId__c != null and Date_System_Actively_Producing__c = null';

        return Database.getQueryLocator(queryProjects);
    }

    global void execute(SchedulableContext sc){
        BatchRefreshLocusComponentId batch = new BatchRefreshLocusComponentId();
        Id batchProcessId = Database.executeBatch(batch);
    }

    global void execute(Database.BatchableContext bc, List<dsProject__c> records){

        try{
            List<Id> projectList = new List<Id>();
            for (dsProject__c p : records){
                projectList.add(p.Id);
            }
            LocusHelper.checkActiveProducing(projectList);
        }
        catch(Exception e){

        }
    }

    global void finish(Database.BatchableContext bc){
        //Datetime sysTime = System.now();
        //sysTime = sysTime.addDays(1);
        //String chronExp = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' + sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();
        //System.schedule('BatchCheckActiveProducing' + sysTime.getTime(), chronExp, new BatchCheckActiveProducing());
    }
}

here is helper method:

global static void checkActiveProducing(List<Id> projectIds){

    System.DEBUG('incoming ids: ' + projectIds);

    List<dsProject__c> projectList = [Select Id, Locus_ComponentId__c from dsProject__c where Id In :projectIds];
    Http h = new Http();
    HttpRequest req = new HttpRequest();

    req.setMethod('POST');
    req.setEndpoint('*************************');
    //req.setHeader('Accept', 'application/json');
    req.setHeader('Content-Type', 'application/json');

    JSONGenerator gen = JSON.createGenerator(true);
    gen.writeStartObject();
    gen.writeFieldName('nodeList');
    gen.writeStartArray();

    // loop through all project list
    // add each into the json
    for (dsProject__c p : projectList){
        gen.writeStartObject();

        gen.writeStringField('componentId', p.Locus_ComponentId__c);
        gen.writeStringField('recordId', p.Id);

        gen.writeEndObject();
    }

    gen.writeEndArray();
    gen.writeEndObject();

    String bodyString = gen.getAsString();
    req.setBody(bodyString);

    System.DEBUG('req: ' + req.getBody());

    HttpResponse res = h.send(req);

    System.DEBUG('res: ' + res);
    System.DEBUG('res body: ' + res.getBody());

    // set of project ids
    Set<Id> projectIdSet = new Set<Id>();
    // map of <projectId, activeDate>
    Map<Id, String> projComponentMap = new Map<Id, String>();

    // loop through returned json list
    // update the active date on project
    if (res.getStatusCode() == 200){
        JSONParser parser = JSON.createParser(res.getBody());

        // advance after start_array
        parser.nextToken();
        while (parser.nextToken() != JSONToken.END_ARRAY){
            System.DEBUG('current token: ' + parser.getCurrentToken() + ' value: ' + parser.getText());
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'recordId'){
                System.DEBUG('in if');
                parser.nextToken();
                String recordId = parser.getText();
                System.DEBUG('recordId: ' + recordId);
                parser.nextToken();
                parser.nextToken();
                String activeDate = parser.getText();
                System.DEBUG('activeDate: ' + activeDate);
                projectIdSet.add(recordId);
                projComponentMap.put(recordId, activeDate);
            }
        }
        System.DEBUG('projectId set: ' + projectIdSet);
        System.DEBUG('project component map: ' + projComponentMap);

        List<dsProject__c> updatingProjList = new List<dsProject__c>();
        for (dsProject__c p : [Select Id, Date_System_Actively_Producing__c from dsProject__c where Id In :projectIdSet]){
            p.Date_System_Actively_Producing__c = Date.valueOf( projComponentMap.get(p.Id) );
            updatingProjList.add(p);
        }


        try{
            update updatingProjList;
        }
        catch(DmlException e){

        }
    }
}

so, the first 4 batches, for this line: System.DEBUG('res body: ' + res.getBody()); it does not have anything, but for last batch, it does have value

2
  • there are all sorts of possibilities here including incorrectly coded batchable. More info would be needed plus results of debug logs for each batch of 200
    – cropredy
    Mar 28, 2016 at 22:53
  • i just attached the code
    – Jie
    Mar 28, 2016 at 22:58

1 Answer 1

1

it turned out it has nothing to do with last batch or not, the 46, this number did inspire me to reduce the batch size and try, so I limited the batch size to 50, now it works fine. It seems some trigger code on dsProject__c by other developer is blocking me doing updates on the records

You must log in to answer this question.

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