0

I have lookup field that has reference to Contact object and in my custom object I have reference to the Contact object as a lookup reference

What I'm looking is that if the user select a VALID contact from the lookup field then only render <apex:outputPanel otherwise not.

I have tried something like this but after selecting the valid Contact from the lookup field its not reRendering.

<apex:pageBlock title="Find/Select a customer!" mode="edit">
  <apex:pageBlockButtons location="Top"> 
           <apex:inputField value="{!cust_obj__c.contact_ren__c}"  />
  </apex:pageBlockButtons>

 <apex:outputPanel rendered="{!(!ISBLANK(cust_obj__c.contact_ren__c))}" id="op1" >

  <!--more input fields here....-->
 </apex:outputPanel>

</apex:pageBlock>   

1 Answer 1

0

Below is an example of using an action function tied with an event to validate the code and update the page, ajax style. If you're familiar with Javascript, you can create a remote action to validate the contact ID. It will run faster and make users happier. If you want the JS example, let me know.

<apex:pageBlock title="Find/Select a customer!" mode="edit">

  <apex:actionFunction name="afValidateContactID" 
                       action="validateContactID"
                       rerender="theOutputPanel" /> 

  <apex:inputField value="{!cust_obj__c.contact_ren__c}" 
                   onChange="afValidateContactID(); return false;" />

  <apex:outputPanel id="theOutputPanel" 
                    rendered="{!NOT(ISBLANK(cust_obj__c.contact_ren__c))}">

    <!--more input fields here....-->

  </apex:outputPanel>

</apex:pageBlock>

Here's a quick and dirty JS solution

 <apex:pageBlock title="Find/Select a customer!" mode="edit">

  <apex:actionFunction name="afValidateContactID" 
                       action="validateContactID"
                       rerender="theOutputPanel" /> 

  <apex:inputField value="{!cust_obj__c.contact_ren__c}" 
                   onChange="afValidateContactID(); return false;" />

  <div id="theOutputPanel" 
       style="display:{!IF(NOT(ISBLANK(cust_obj__c.contact_ren__c)),'block','none')}">

    <!-- more input fields -->

  </div>

  <script>
    function jsValidateContactID(domRef) {
      if(domRef && domRef.value !== null) {
        yourController.validateContactId(
          domRef.value,
          callbackContactIdValidation(result, event)
        );
      }
    }

    function callbackContactIdValidation(results, event) { 
      if(event.type === 'exception'){ 
        console.log("exception"); 
        console.log(event); 
        editTheForm(false);
      } else if(event.status) { 
        .each(results, function(i, result) { 
          editTheForm(true);
        }); 
      } else { 
        console.log(event.message); 
      } 
    }

    function editTheForm(good) {
      var outputPanelDiv = document.getElementById("theOutputPanel");
      outputPanelDiv.style.display = (good ? 'block' : 'none');
    }
  </script>
</apex:pageBlock>
2
  • Please post the JS version as well it will help me understand both.
    – Nick
    Nov 21, 2016 at 20:20
  • I will not use JS but you have action method(validateContactID) where is that action method? and i tried your code but did not work
    – Nick
    Nov 21, 2016 at 23:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .