Skip to main content
added 152 characters in body; edited tags
Source Link
Adrian Larson
  • 151.3k
  • 38
  • 247
  • 431
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());
    }
}

}

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

}

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());
    }
}
added 155 characters in body
Source Link
Jie
  • 449
  • 1
  • 5
  • 13

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

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

added 4177 characters in body
Source Link
Jie
  • 449
  • 1
  • 5
  • 13

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){

        }
    }
}

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){

        }
    }
}
Source Link
Jie
  • 449
  • 1
  • 5
  • 13
Loading