2

I'm inserting and updating a new record before throw an exception like this:

Operation__c o = new Operation__c(ok__c = false,Step__c = 'test1');
insert o;
system.debug(o); //this shows the Id
try{
  //my code of operation
 o.ok__c = true;
 update o;
 //the record is updated ok here and I can query them later
}catch(Exception e)

  o.ok__c = false;
  o.error__c = e.getMessage();
  update o;
 //not error here and the system.debug shows the updated record including the Id

   throw new e;


}

If I query the record later (manually or in other thread), the previous record doesn't exist.

Why I can not query the record when an exception was thrown ?

4

Within your code, database modifications including the allocation of object IDs proceed as you would expect. Those changes are happening within your own transaction and until that transaction is committed to the database other requests will not see the changes. But allowing an exception to escape from your code causes the platform to automatically rollback (not commit) the transaction meaning all changes made to the database are discarded.

There is limited control of transactions in the platform but that does not include the ability to force a commit of a transaction. So you will need to change your design to not re-throw the exception if you want to keep the database changes.

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.