Custom component does not save object changes

I’ve started recently to use Salesforce, so probably it’s a simple question, but I haven’t found a answer yet. I need to customize the edit layout of several objects in a similar way using a visual force page. So, instead of create a page of every object I need with all the fields added manually, I’m trying to develop a custom component that, using SObject metadata and field sets, creates the page dinamically. So, I have a custom page per SObject like this:

<apex:page standardController="MyCustomSObject">
     <c:Translate_SObject object="{!MyCustomSObject}"/>
</apex:page>

And this is my component, first the view:

 <apex:component controller="Edit_Controller" allowDML="true">

<apex:attribute name="object" type="SObject" required="true" description="Id of the object to be translated" assignTo="{!record}"/>

...

<apex:form id="form">

    <apex:pageBlock id="editBlock" rendered="{!showEditBlock}">

        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save"/>
            <apex:commandButton action="{!cancel}" value="Cancel"/>
        </apex:pageBlockButtons>

        <apex:pageBlockSection id="fieldSection">
            <apex:repeat value="{!fieldList}" var="f">
                <apex:inputField value="{!record[f.fieldInfo]}"/>
                ...
                <br/>
            </apex:repeat>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
    </apex:component>

And now, the controller:

    public with sharing class Edit_Controller {

public SObject record{
    get; 
    set{
        System.debug('record: ' + value);
        if(value != null){
            record = value;
        }
    }
}

public Edit_Controller(){
}

public PageReference save(){
    ApexPages.StandardController sc = new ApexPages.StandardController(record);
    return sc.save();
}
...
   }

The page shows the fields of the SObject with their expected values. I can modify these values on the page but when I press the Save button the SObject saved doesn’t have any of the changes. It seems that the SObject reference used by the page it’s different that the SObject reference used by the controller. While checking the logs, I’ve noticed that during the request to get the edit page, record setter is called 4 times. I suppose that this is related with my problem but I don’t undertstand the reason of that.

Could anyone help me? Thanks in advance.

Answer

Agree with @Adrian Larson. You need to fix your getter/setter

public SObject record{
    **set;**  
    **get{**
        System.debug('record: ' + value);
        if(value != null){
            record = value;
        }
    }
}

Attribution
Source : Link , Question Author : Rodrigo Lanza , Answer Author : Chris Duncombe

Leave a Comment