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
DerGrobeDerGrobe 

Force custom setter being called for custom object

hi all,

 

i have a problem with a custom setter method not being called for a custom object on a VF page.

 

Controller part:

 

private Partner__c cst; public Partner__c getCustomer() { if (this.cst == null) { this.cst = new Partner__c(); System.debug(Logginglevel.DEBUG, '##### getCustomer(): create new customer'); } System.debug(Logginglevel.DEBUG, '##### getCustomer(): cst.Name = ' + this.cst.Name); return this.cst; } public void setCustomer(Partner__c value) { // Use a customised setter to detect if crucial address details changed // to force the address validation or skip it. System.debug('##### setCustomer(): value -> ' + value + ', customer -> ' + cst); if ( cst <> null && (!value.City__c.equalsIgnoreCase(cst.City__c) || !value.Street__c.equalsIgnoreCase(cst.Street__c) || !value.Country__c.equalsIgnoreCase(cst.Country__c) || !value.PostalCode__c.equalsIgnoreCase(cst.PostalCode__c) ) ) { addressChanged = true; } cst = value; }

 

 VF page:

 

<apex:page title="{!currentStep}" tabStyle="Partner__c" controller="OrderWizardController" action="{!displayCustomerPage}" showHeader="true" > <apex:sectionHeader title="Add a new customer" subtitle="{!currentStep}" /> <apex:form id="theform"> <apex:pageBlock title="Customer Info" mode="edit" id="pageblock"> <apex:pageBlockButtons location="both"> <apex:commandButton action="{!step2}" value="Next"/> <apex:commandButton action="{!cancel}" value="Cancel" immediate="true"/> </apex:pageBlockButtons> <apex:pageMessages /> <apex:pageBlockSection columns="2" title="Customer Address Information" id="cstInfoSection"> <apex:inputField value="{!customer.Address__c}" required="true" id="customerAddress" /> <apex:outputText /> <apex:inputField value="{!customer.Name}" required="true" id="customerName"/> <apex:inputField value="{!customer.Name1__c}" required="true" id="customerName1"/> <apex:inputField value="{!customer.Name2__c}" /> <apex:outputText /> <apex:inputField value="{!customer.Street__c}" required="true" /> <apex:inputField value="{!customer.PostalCode__c}" /> <apex:inputField value="{!customer.City__c}" required="true" /> <apex:inputField value="{!customer.Country__c}" required="true" /> <apex:inputField value="{!customer.County__c}"/> <apex:inputField value="{!customer.State__c}"/> </apex:PageBlockSection> <apex:pageBlockSection columns="2" title="Extended Information" id="cstInfoSection2"> <!-- apex:inputField value="{!customer.Description__c}"/ --> <apex:inputField value="{!customer.EmailHome__c}"/> <apex:inputField value="{!customer.EmailBusiness__c}"/> <apex:inputField value="{!customer.PhoneHome__c}"/> <apex:inputField value="{!customer.PhoneBusiness__c}"/> <apex:inputField value="{!customer.MobileHome__c}"/> <apex:inputField value="{!customer.MobileBusiness__c}"/> <apex:inputField value="{!customer.FaxHome__c}"/> <apex:inputField value="{!customer.FaxBusiness__c}"/> </apex:PageBlockSection> </apex:pageBlock> </apex:form> </apex:page>

 

 

 

 I found in the VF develper guide that the setter is not necessary not therefore not necesarrily called.

 

<qoute>

While a getter method is always required to access values from a controller, it is not always necessary to include a setter method
to pass values into a controller. If a Visualforce component is bound to an sObject that is stored in a controller, the sObject's fields are set automatically if changed by the user, as long as the sObject is saved or updated by a corresponding action method.<quote>

Can I somehow force the setter being called to mark the sObject "as dirty"? The point is that I read the values from the sObject at first and the user is able to change it. In case of changes I need some additional steps on the next pages which aren't necessary otherwise.

 

Any help is highly appreciated.

 

TIA

 

 

bob_buzzardbob_buzzard

What will happen here is that your getCustomer will be called to retrieve the initial customer value (cst), the fields from this are then bound to the input fields, and any changes happen to cst's fields directly.

 

I think you will need to take a copy of cst when you create it, and then check for changes between the objects in your step2 action method. 

DerGrobeDerGrobe

bob_buzzard wrote:

What will happen here is that your getCustomer will be called to retrieve the initial customer value (cst), the fields from this are then bound to the input fields, and any changes happen to cst's fields directly.

 

I think you will need to take a copy of cst when you create it, and then check for changes between the objects in your step2 action method. 


Already thought about this, but hoped to find a "smarter" solution avoiding to copy the object. But it seems to me that this is the only option to sort this out, sometimes the hidden magic that takes place in the framework can be challenging...