0

I have the following block of code in Execute method of my batch apex class.

    for (String fieldKey : objectSchemaMap.get(scope.get(0).Object_Api_Name__c).getDescribe().fields.getMap().keySet()) {
        SObjectField aField = objectSchemaMap.get(scope.get(0).Object_Api_Name__c).getDescribe().fields.getMap().get(fieldKey);
        String fieldType = aField.getDescribe().getType().name().toLowerCase();
        String fieldName = aField.getDescribe().getName().toLowerCase();
        String isCalculated = aField.getDescribe().isCalculated() ? 'calculated' : 'value';
        csvContent += scope.get(0).Object_Api_Name__c + ',' + fieldName + ',' + fieldType + ',' + isCalculated;
    }

I want to check if 'csvContent' string has changed from yesterday's run versus today's run. How can I do this? Only if 'csvContent' has changed, I want to stamp it in a Custom field and perform an Upsert .

  • 2
    Please respect the community's volunteer engagement by not including things like URGENT. Thank you! – David Reed Sep 24 at 0:05
1

Batch jobs do not share state. A Stateful batch retains its state only through its own conclusion, and has no access to any other batch's state. Your only option to compare results across batch job runs is to persist that state to the database and programmatically perform a comparison.

If you are storing this value in an object, you can query that object (perhaps the most recently created or updated object? The schema you're using is not clear) and compare the value stored to the value you've just generated.

If that model does not apply, you'd have to do something like what Peter already articulated, where you create a new storage location like a Custom Setting or a new sObject, and store the data there.

| improve this answer | |
1

You could consider storing the csvString value and job id to the db during each batch run. Then in each batch run do a lookup by jobid to see if the csvString value is changing.

Be careful to avoid comparing dates because async batches can run at an undetermined time.

An approach might be to compare “whether the csvString value has changed since the last batch run”. For example: You could persist the batch’s job ID into one of two custom setting fields: XyzJobIdToLastSucceed, XyzJobIdToLastFail. and could persist/store the csvString value as well into the same table/object.

If you store job Id and csvString value into the database, (i suggest as a custom setting, etc - then all of your sandboxes will have it, unless thats not a requirment), then you could determine if the batch has run, and whether the value of csvString is changing. That is to say your batch class could then lookup the previous value, assuming you choose to have it persisted.

| improve this answer | |
  • Thanks for responding Peter! Could you please show the sample code for this? I am doing this -- Map<Id,String> storeCSVResult = new Map<Id,String>(); AsyncApexJob a = [Select Id from AsyncApexJob where Id = :bc.getJobId()]; storeCSVResult.put(a.Id,csvContent); – Desat Sep 23 at 21:22
  • This is an urgent request. Could you advise on the code? I do not need to store the values in a custom setting. Is there anyway I can compare the old value vs new value of 'csvContent' within the Batch class? – Desat Sep 23 at 23:23

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.