function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
loneboatloneboat 

When updating via DML statement, need all fields?

Suppose I have an object type A with fields A.Id, A.a, A.b, A.c.

 

If I want to update ONLY field A.a, do I have to select ALL the fields if I want to retain their present values?

 

E.g.:

 

A x = [SELECT Id, a FROM A WHERE A.Id=:someIdValue];
x.a='asdf';
update x;

 

Would this wipe out x.b and x.c, since they were not selected and were thus empty when I called "update"?

 

Also, can I change values which were NOT selected:

 

A x = [SELECT Id, a FROM A WHERE A.Id=:someIdValue];
x.a='asdf';
x.b='qwer';
x.c='zxcv';
update x;

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

No, you only need the fields you are going to update.  The other fields won't be blanked as they don't really exist on that instance.

All Answers

bob_buzzardbob_buzzard

No, you only need the fields you are going to update.  The other fields won't be blanked as they don't really exist on that instance.

This was selected as the best answer
loneboatloneboat

Excellent, thanks for the reply.

 

How about the second question - can I update fields which were not selected?

bob_buzzardbob_buzzard

Sorry, missed that one.  Yes you can.

 

E.g.  

 

account acc=[select id,Name from Account where name='Spim2'];
acc.Phone='01234 1213';
update acc;
acc=[select id,Name, Phone from Account where name='Spim2'];

When I run this in the debug log, I'm able to update the phone number.