3

I have an Account(Name='Hello') and the following code

// First get an Account with its Name field and some other fields
Account acc = [SELECT Id, Name, Otherfields FROM Account WHERE Id = ...];
// acc.Name is 'Hello'

// Now some other code that loads the Account, changes the Name field to 'Hello' and updates the Account
someFunctionThatChangesAccountName(acc.Id);
// In DB, the Account will now have the Name 'World'
// The local acc variable will still hold the Name 'Hello'

// Now here I want to change some other fields, then update the Account
acc.OtherField__c = ...;
update acc; // Oh no, acc.Name is still 'Hello' and will overwrite 'World'

But now the Account will now contain 'Hello' again, because the latter update will overwrite the first 'World' Name update from someFunctionThatChangesAccountName.

So how can I update an Account, but not update the Name field?

PS: I know I could just simply pass the acc itself to the function and it would update the Name on the acc record, but in our code that is not possible because someFunctionThatChangesAccountName actually is not a function but a Trigger that runs when I change a Contact record. It fetches the Account, changes its name, then updates the Account.

5
  • Are there any validation rules or trigger on account that is reverting the account name back to original?
    – Mahmood
    Commented Nov 16, 2016 at 11:07
  • Sorry if I was a bit unclear. The name is set back to its original value because the acc variable will still hold the original 'Hello' value, and will overwrite anything that has happened in between. Commented Nov 16, 2016 at 11:15
  • Have you tried the same code with "Name" removed from the SOQL?
    – Mahmood
    Commented Nov 16, 2016 at 11:19
  • Yes, then it works, but in our case we can't do this because we need the Name in our code. So I only want to 'remove' the Name from the update statement, not from the original SOQL query. Commented Nov 16, 2016 at 11:27
  • On second thought, you can place your soql to account after the method call "someFunctionThatChangesAccountName" so that way , account with latest values will be fetched.
    – Mahmood
    Commented Nov 16, 2016 at 11:28

2 Answers 2

4

Okay so if you can't update the function to accept & return the acc variable because it's a trigger or some other secondary process, then it might be easier to insert only the values you want to update:

// First get an Account with its Name field and some other fields
Account acc = [SELECT Id, Name, Otherfields FROM Account WHERE Id = ...];
// acc.Name is 'Hello'

// Now some other code that loads the Account, changes the Name field to 'Hello' and updates the Account
someFunctionThatChangesAccountName(acc.Id);
// In DB, the Account will now have the Name 'World'
// The local acc variable will still hold the Name 'Hello'

// create an empty account and map across only the fields you want to update
Account accToUpdate = new Account();
accToUpdate.Id = acc.Id;
accToUpdate.OtherField__c = ...;

update accToUpdate;
1

Replace your code

// First get an Account with its Name field and some other fields
Account acc = [SELECT Id, Name, Otherfields FROM Account WHERE Id = ...];
// acc.Name is 'Hello'

// Now some other code that loads the Account, changes the Name field to 'Hello' and updates the Account
someFunctionThatChangesAccountName(acc.Id);

with something like

// First get an Account with its Name field and some other fields
Account acc = [SELECT Id, Name, Otherfields FROM Account WHERE Id = ...];
// Now some other code that loads the Account, changes the Name field to 'Hello' and updates the Account
someFunctionThatChangesAccountName(acc.Id);
// second soql to fetch the latest account 
Account LatestAcc = [SELECT Id, Name, Otherfields FROM Account WHERE Id = ...];

Explanation

Querying account after your method call which updates the account name indirectly, will fetch latest account values and account name.

You must log in to answer this question.

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