0

Here is my scenario.

Transaction 1: DML to update the Status field from Open to Closed on ABC record.

Transaction 2: SOQL is performed to retrieve the ABC record before the above DML and updates the price value. Will the Status be Closed or Open after the second DML?

If the status is Open, how can I ensure that Transaction 1 updates are not overwritten in Transaction 2 DML?

3
  • Are these really separate transactions you're working with, and you're asking about concurrent operations and/or race conditions?
    – Derek F
    Commented Jun 22, 2022 at 16:48
  • I read it as two concurrent transactions.
    – Phil W
    Commented Jun 22, 2022 at 18:42
  • @DerekF Yes, they are two concurrent transactions.
    – Ram
    Commented Jun 22, 2022 at 20:56

1 Answer 1

1

Only include the fields you really want to update in the record passed to the DML operation. A perfect way to do that is to not use the record you queried, but construct a new one. For example:

List<ABC__c> abcs = [SELECT Id, A__c, B__c, C__c FROM ABC__c WHERE ...];
List<ABC__c> updatedAbcs = new List<ABC__c>();

for (ABC__c abc : abcs) {
    updatedAbcs.add(new ABC__c(abc.Id, B__c = 'new value'));
}

update updateAbcs;

This way only the B__c field will be updated, leaving all other fields unaffected, even if you queried them in the original SOQL used here.

You must log in to answer this question.

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