0

I am trying to add a feature by which I can update all records from a JSON payload, but the insert should only occur for those which have "archived" value = False.

below is a sample JSON. My batch job currently adds all records ,both for archived = true and false. How ever , I would like false records to be added and existing records to get updated and even change from false to true. But for any insert with archived = true , should not get added to SF.

I tried the trigger (after insert) approach to check for archived = true , but that bombed because as soon as SF found one record which matches the criteria (record 1 .. payload below), it prevented that insert , but even stopped update of sister roots (record 2) from the same payload.

Checking the archived condition in the controller class also worked for insert, but it won't update existing records from the same payload.

{  
   "content":[  
      {  
         "record":1,
         "number":12345,
         "archived":"True"
      },
      {  
         "record":2,
         "number":54321,
         "archived":"False"
      }
   ]
}
  • 1
    Can you post code for your batch job as well? – Pranay Jaiswal Jul 9 '18 at 18:47
  • Would it be acceptable to remove all of the archived records from the content array and leave the rest to be upserted? – Mark Pond Jul 9 '18 at 18:55
  • That is too big a code block to post and explain here Pranay as its just the tip of iceberg. The entire functionality is very convoluted , with Batch , Util , Controller & Trigger .. And posting only excerpts of related code won't serve any purpose. :) – login2ak Jul 9 '18 at 18:55
  • @MarkPond I am not sure I understood. Are you suggesting cleaning out the source so that when SF calls it only gets archived=false or are you suggesting SF should make 2 callouts .. one to create a list of all false records and then call for them one by one?? – login2ak Jul 9 '18 at 18:59
  • 4
    well, you could use Database.update(recs,false) // allowpartialsuccess and have a validation rule that rejects an update when archived is true (you'll need a custom field for that state) – cropredy Jul 9 '18 at 19:04
0

Okay team. I finally found a way. I did add try the scenario with a validation rule, but it did not work, as all other changes were rolled back, when one entry had archived =true.

So instead I queried all the existing records using SOQL.

Map<Decimal, List<sObject>> existingMap = GroupBy.decimals('primary_key', [SELECT Id, primary_key,Order_Id__c FROM sObject WHERE primary_key IN :parent_object_list]);

And used an if-condition to check all incoming records for boolean condition (archived == True) and use primary key to check for existing records. If primary_key exists, I am updating the record. If not and boolean flag is true, I am rejecting insertion.

if(list.Archived__c == True && existingMap.containsKey(list.primary_key) == False)
  {
    errors.add('Archived record insertion is rejected: ' + list.primary_key);
    recordsToIgnore.add(counter_value);  
  }

In this way, all updates are successful, even when an entry is archived , while all archived insertions are rejected.

Thank you all for your input.

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.