+ Start a Discussion
john8407john8407 

Autosave in background

In a scenario where I have a field that is required if another is a certain value, is there any way to have the record save in the background without reloading and the user noticing anything? I'm asking cause I want to include the required with an if statement on the other field.

Starz26Starz26

Not quite sure how to code it, but what if

 

 

1. set the required value of the dynamically required field to an {!IF(variable=xxx,true,false)}

 

2. you put the field that is dynamically required in a seperate component wrapper

 

3. and then when the field that detemins the required fields status is changed call

 

4. a javascript function that rerenders the component with the dynamically required field

 

 

Starz26Starz26

**UPDATED WORKING**

 

Ok, I have a solution but it needs one thing: properly setting the variable to the value entered in the field

 

Controlling field = FieldA

Dependent Field = FieldB

Controller Variable = VarA

Entry to make required = 'Mine'

 

1. Add around FieldB

 

                               <apex:outputPanel id="test">
                                <apex:InputField id="FieldB" value="{!object.Field}"  required="{!IF(VarA=='**Whatever you want it to be to make other required**',True,False)}" />
                                </apex:outputPanel>

 

2. Make this where the controlling FieldA is

 

                               <apex:inputField id="FieldA" value="{!object.field}" required="true" onchange="getInputValue()">
                               

                              <script>
                                   function getInputValue(){
                                  var ex = document.getElementById('{!$Component.FieldA}').value;
                                   passValue(ex)
                                  }
                               </script>

 


                                 </apex:inputField>
                               

3. In the form add somewhere

 

<apex:actionFunction name="passValue" action="{!gValue}" immediate="True" rerender="test">
<apex:param name="myParam" value=" "/>
</apex:actionFunction>

4. In the controller

 

Public String VarA{get;set;}

 

Public void gValue(){

 

VarA = ApexPages.CurrentPage().getParameters().get('myParam'');

}

 

 

Starz26Starz26

Here is a working example:

 

Controller:

 

public class testDynamicRequiredFields {

    Public String VarA{get;set;}
    Public Contact conn{get;set;}
    
    public testDynamicRequiredFields (){    
        init();
            
    }

        
    private void init(){

      VarA = ' ';
      conn = New Contact();
}    
        
       
      
      Public void gValue() {
        VarA = ApexPages.CurrentPage().getParameters().get('myParam');
        }
}

 

Visualforce Page:

 

<apex:page controller="testDynamicRequiredFields" standardStylesheets="true">
  
  
  <apex:form id="mForm">
  
   <apex:actionFunction name="passValue"  action="{!gValue}" immediate="True" rerender="test">

     <apex:param name="myParam" value=" "/>

  </apex:actionFunction>
 
 
<apex:pageblock id="pBlock"> 

Controlling Field: <apex:inputField id="inputA" value="{!conn.FirstName}" required="true" onchange="getInputValue()">
  
<script>

    function getInputValue(){
        var ex = document.getElementById('{!$Component.InputA}').value;
        passValue(ex);
    }
</script>



</apex:inputField>

<apex:outputPanel id="test">
      Dependant Field: <apex:InputField id="FieldB" value="{!conn.LastName}" required="{!IF(VarA=='This',True,False)}" />
</apex:outputPanel>

</apex:pageblock>
  </apex:form>
  
  </apex:page>

 

johnc82johnc82

Thank you Starz26! One question.  I can't get it to work in a pageBlockSection and pageBlockSectionItem.  Do you know how?

Starz26Starz26

Sure, remove the outputpanel and replace the code section with this:

 

It will require the field based on the value you enter in the controlling field as well as make the section not collapsible based on the controlling field.

 

<apex:pageblockSection Title="Additional Information" id="test" collapsible="{!IF(VarA=='This',False,True)}" >

<apex:pageBlockSectionItem >
      Dependant Field: <apex:InputField id="FieldB" value="{!conn.LastName}" required="{!IF(VarA=='This',True,False)}" />
</apex:pageBlockSectionItem>

</apex:pageblockSection>