You need to sign in to do that
Don't have an account?
up_sky
Fields get cleared if any required fields is blank.
Hi All,
I created a VF page with required input fields and a validation rule along with a command buttons ( "Update" buttons). While submitting the information if I does not input on one of the required fields, It displays the validation rule error message and at the same time all fields are get cleared.
But, I like to keep all data on the fields, but display error message.
How to resolve this "Fields get cleared" problem... Please suggest me
Thanks,
<apex:page Controller="MyAccount"> <apex:form > <apex:outputPanel id="AuthorizedContacts"> <apex:message/> <h2>{!primaryContact.Name}</h2><br/> <apex:commandbutton action="{!addAuthContact}" value="Add Contact" rerender="AuthorizedContacts" rendered="{!addAuthContact == false}"/> <apex:repeat value="{!newAuth}" var="a"> <apex:outputpanel rendered="{!addAuthContact}"> {!a.Contact_Type__c}<br/> <h4>Name & Contact</h4><br/> FIRST NAME* <apex:inputField value="{!a.FirstName}" id="contact-first-name" required="true"/> LAST NAME* <apex:inputField value="{!a.LastName}" id="contact-last-name" required="true"/> PHONE* <apex:inputField value="{!a.Phone}" id="contact-phone" required="true"/> EMAIL ADDRESS* <apex:inputField value="{!a.Email}" id="contact-email" required="true"/> <h4>Contact Address</h4><br/> ADDRESS 1 <br/><apex:inputText value="{!a.Contact_Address_Line_1__c}" id="contact-addr-1"/><br/> ADDRESS 2 <br/><apex:inputText value="{!a.Contact_Address_Line_2__c}" id="contact-addr-2"/><br/> CITY <br/><apex:inputText value="{!a.Contact_Town_City__c}" id="contact-city"/><br/> POST CODE <br/><apex:inputText value="{!a.Contact_Postal_Code__c}" id="contact-postal"/><br/> COUNTY <br/><apex:inputText value="{!a.Contact_County__c}" id="County"/><br/> COUNTRY<br/> <apex:SelectList value="{!CountryName}" size="1"> <apex:selectOptions value="{!lstCountry}"></apex:selectOptions> </apex:SelectList><br/> <h4>Is This Authorized Contact Part of Your:</h4><br/> <apex:Inputcheckbox value="{!a.IsHousehold__c}"/> <label>Household</label> <apex:Inputcheckbox value="{!a.IsBusiness__c}"/> <label >Business</label><br/> <apex:messages rendered="{!IsValidate}" styleClass="errorMsg"/><br/> <apex:commandbutton styleClass="cancel" action="{!cancelAuth}" value="Cancel" immediate="true" rerender="AuthorizedContacts"/> <apex:commandButton styleClass="button-medium" action="{!saveAuth}" value="Save Updates" rerender="AuthorizedContacts"/> </apex:outputPanel> </apex:repeat> </apex:outputPanel> </apex:form> </apex:page>
public class MyAccount { private Id uid; public Id aid{get;set;} public Id cid{get;set;} public string CountryName {get;set;} public Boolean IsValidate{get;set;} public Boolean addAuthContact{get;set;} public User objUser{get;set;} public Contact primaryContact; public list<Contact> newAuth; public MyAccount(){ uid = UserInfo.getUserId(); getObjUser(); } public List<SelectOption> getlstCountry() { list<SelectOption> options = new list<SelectOption>(); try{ list<Country__c> ls = [select Name,LZ_Country_Id__c,Id from Country__c]; Country__c uk = [select Name,LZ_Country_Id__c,Id from Country__c Where Name = 'United Kingdom']; options.add(new SelectOption(uk.Id,uk.Name)); for(Country__c o:ls){ if(o.Name != 'United Kingdom') options.add(new SelectOption(o.Id,o.Name)); } } catch(Exception ex) { System.debug('Error From ' + ex.getMessage()); } return options; } public void getObjUser(){ try { uid = Apexpages.currentPage().getParameters().get('id'); if(uid != null){ objUser = [Select User.ContactId, User.AccountId, User.UserType From User Where User.Id = :uid ]; cid = objUser.ContactId; aid = objUser.AccountId; } } catch(Exception ex) { System.debug('Error From ' + ex.getMessage()); } } public Contact getprimaryContact(){ try { if(cid != null){ primaryContact = [Select Contact.Id, Contact.Name, From Contact Where Contact.Id =:cid]; } }catch(Exception ex){ System.debug('Error From ' + ex.getMessage()); } return primaryContact; } public list<Contact> getnewAuth(){ try { newAuth = new list<Contact>(); Contact ac = new Contact(); if(aid != null) { ac.AccountId = aid; ac.Contact_Type__c = 'Authorized Contact'; } newAuth.add(ac); } catch(Exception ex) { System.debug('Error From' + ex.getMessage()); } return newAuth; } public PageReference addAuthContact(){ try{ getnewAuth(); addAuthContact = true; } catch(Exception ex) { System.debug('Error From ' + ex.getMessage()); } return null; } public PageReference cancelAuth(){ try{ newAuth = null; addAuthContact = false; } catch(Exception ex) { System.debug('Error From ' + ex.getMessage()); } return null; } public PageReference saveAuth() { try{ List<Contact> tempCon = new List<Contact>(); if(newAuth != null) { for(Contact t : newAuth) { t.Contact_Country__c = CountryName ; tempCon.add(t); } } insert tempCon; addAuthContact = false; }catch(DmlException ex){ if(newAuth[0].IsBusiness__c == false && newAuth[0].IsHousehold__c == false){ IsValidate = true; } ApexPages.addMessages(ex); } return null; } }
This is really strange, I have the same with an extension. Field Customer_Contact_Measurement__c.Department__c has as default value $User.department. This is displayed correctly on load, but when saving and not filling in the required field the value is cleared. When I fill in required field and hit save, save is performed correcly with values filled in. The action mySave is NOT called when I hit the button save.
And the extension.