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
up_skyup_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 &amp; 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;
    }
}

 

 

 

 

 

 

JeeedeeeJeeedeee

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.

 

 

<apex:page standardController="Customer_Contact_Measurement__c" extensions="CCMController2">
	<apex:form >
		<apex:sectionHeader title="{!$Label.Customer_Contact_Measurement_Edit}" subtitle="{!Customer_Contact_Measurement__c.Name}"/>
		<apex:pageBlock title="{!$Label.Customer_Contact_Measurement_Edit}" mode="edit">
	        <apex:pageBlockButtons >
            	<apex:commandButton action="{!mySave}" value="{!$Label.Save}"/>
            	<apex:commandButton action="{!cancel}" value="{!$Label.Cancel}"/>
        	</apex:pageBlockButtons> 
			<apex:pageMessages id="pm"/>

   	    	<apex:pageBlockSection title="Basisgegevens" columns="2">
				<apex:outputField value="{!Customer_Contact_Measurement__c.Customer_Name__c}"/>
				<apex:inputField value="{!Customer_Contact_Measurement__c.Contact_Gender__c}" required="true"/>
			</apex:pageBlockSection>
 
        	<apex:pageBlockSection title="Medewerker- en afdelinggegevens" columns="2" id="details2">
        	<!--  below field contains the department, Default Value	$User.Department, gets cleared on save when gender is not filled -->
				<apex:outputField value="{!Customer_Contact_Measurement__c.Department__c}"/> 
			</apex:pageBlockSection>
		</apex:pageBlock>
	</apex:form>
</apex:page>

 And the extension.

public with sharing class CCMController2 {
	public Customer_Contact_Measurement__c custumerContactMeasurementObj {get; private set;}
	public static final String REL_ACCOUNT_PARAM_NAME = 'accId';

	private Apexpages.Standardcontroller stdController;
	
	public CCMController2(Apexpages.Standardcontroller stdController) {
		this.stdController = stdController;
		custumerContactMeasurementObj = (Customer_Contact_Measurement__c)stdController.getRecord();
	}

	public Pagereference mySave() {
		throw new CRMWPException('Never gets here');
		Pagereference retPage = null;
		try {
			retPage = stdController.save();
		} catch(Exception exc) {
			System.debug(Logginglevel.ERROR, 'Catching exception in customer contact measurement controller save operation:' +exc);
			ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, exc.getMessage()));
		}
				
		return retPage;
	}
	
	private Account account {
		get {
			if (account == null) {
				Id accountId = Apexpages.currentPage().getParameters().get(REL_ACCOUNT_PARAM_NAME);
				custumerContactMeasurementObj.Customer_Name__c = accountId;
				List<Account> accounts = AccountDAO.getInstance().getAccountsDetails(new Set<Id>{accountId});
				if(!accounts.isEmpty()) {
					account = accounts.get(0);
				}
			}
			return account;
		}
		private set;
	}
}