5

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.

6
  • 1
    I'd try to initialise the standard controller with a new record in your controller constructor, use that record reference and in your save, simply invoke sc. save() Jun 5, 2013 at 10:18
  • Do you have allowDML="true" on your component? Jun 5, 2013 at 11:01
  • @Sergey Yes, I've missed the first line of the file. It has the allowDML property. As I said, the controller is saving actually a record (I can see the time updated being modified). But, if I print record before doing save, I see the fields' old values, not the new ones. Jun 5, 2013 at 11:15
  • @techtrekker I don't know how to pass the record to the component constructor to try this. Could you give me an example? Jun 5, 2013 at 11:15
  • You've got your getter/setter reversed on record. Lazy load in the getter.
    – Adrian Larson
    Jul 24, 2013 at 19:24

2 Answers 2

1

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;
        }
    }
}
1

The problem is that when you click your save button, the component has it's attributes set again by the containing page before executing the action. If you try to load the object in the setter with soql, you get the old values instead of the ones modified by the user, thus creating the illusion that the changes are not being sent to the controller.

What you'll have to do is add some check to the setter so that it doesn't overwrites the data you want to save.

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.