2

I'm using custom_object on the Visualforce Page and showing the fields based on the user selection on the picklist, even though I hide the fields the value is still saving to the object, how do I not save the fields in the object which is not rendered/hide?

To give you an example here is how I'm not rendering the field based on the condition:

<apex:inputField value="{!Employee__c.WeekDay__c}" rendered="{!isTransSelected==true}"  />  

I'm using the following Save action:

public PageReference Save()
{
    try
    {
        system.debug('updating: ' + employeeObj);
        update employeeObj;
    }
    catch (exception e){           
        ApexPages.addMessages(e);
    }
    return null;           
}

Or should I explicitly defined what fields needs to be updated?

1
  • 1
    A nitpick: you dont need to compare Boolean values to true or false. You can directly write rendered="{!isTransSelected}". – battery.cord Jul 27 '16 at 15:35
1

If you want to update specific fields, one option is to create a new copy of the SObject that only contains that data.

Employee__c record = new Employee__c();
for (SObjectField field : /*yourCollection*/)
{
    if (/*shouldSaveField*/)
    record.put(field, controllerRecord.get(field));
}
// try/catch
2
  • one question, I dozens of fields and it will very painful to go each, how about that, if I remove the one I do not want will that work? – Nick Kahn Jul 27 '16 at 18:17
  • You can't really "unset" the value afaik. You might be able to make the structure that stores which fields to update more scalable so you can loop through fields. @Nick – Adrian Larson Jul 27 '16 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.