[SalesForce] way to remove a field from a SObject variable for update

Suppose I have a SObject A__c, it has two fields b__c and c__c.

Now I have:

A__c a = [Select Id, b__c, c__c From A__c limit 1];
//Suppose a.b__c = 'a', and a.c__c = 'a' right now. 
a.b__c = 'b';
a.c__c = 'c';
if(/*...*/) {
    //here I want to remove c__c field value so I can only update b__c.
    //But a.c__c apparently won't work here.
    //What I want to do is only update a.b__c to 'b' but keep the value of a.c__c here.  
update a;

Is it possible to simply get rid of a.c__c in the variable value? Something like delete a.c__c?

Best Answer

The direct answer is "no." There's only one way to unload a field from an sObject, and that's to call clear, which, as the name implies, remove all fields completely. You need to construct a new version of A__c somehow, either by way of new A__c(...) or re-querying the database, or storing a temporary copy via clone, or through a JSON round-trip, etc. The preferred method would be to not change the field if it doesn't need to be to begin with. It's almost always possible to shuffle around the code such that you don't get any unwanted updates.