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
Mak OneMak One 

If we are getting an Object from Select Query and want to update 1 field in that object then do we need to select all fields in that object

Suppose Object Obj__c having 3 fields: Field1__c, Field2__c and Field3__c.
Suppose we do Obj__c obj1=[select Field1__c from Obj1__c where id=:someId]; //Suppose all fields are hainvg value.
obj1.Field2='ABCD';
update obj1;

Now, problem here is Field3__c is nullified.

So, do we have to give all fields in SOQL if we want to update that object (even for 1 field)?
Any solution is there if I don't want to select all fields? Or any other way is there?
Best Answer chosen by Mak One
Jim JamJim Jam
No, you shouldn't have to select all fields just to update a single field. You only need to select the fields you wish to update. If one of the other fields is being nullified, this might be due to a trigger, field update workflow or some other process.

All Answers

Jim JamJim Jam
No, you shouldn't have to select all fields just to update a single field. You only need to select the fields you wish to update. If one of the other fields is being nullified, this might be due to a trigger, field update workflow or some other process.
This was selected as the best answer
vamshi(Nani)vamshi(Nani)

//Sample code

Account a = [SELECT Name FROM Account a where a.id = : id]; //id of that record which is you want update
a.name = 'vamshi';
update a;
 

You need to query for how many fileds you want to update.

Vinit_KumarVinit_Kumar
Agreed with earlier reply,you should be querying only those fields where you need to perform update operation or any operation you want to perform.
Mak OneMak One
I got what I was doing wrong.

I was doing:
toDisplay=obj1;

but I need to query it again if I want all the fields like:

toDisplay = [SELECT id, Field1__c, Field2__c, Field3__c FROM Obj__c where id=:obj1.id];

After that it is showing correct.

Thanks to all for clearing the doubt!