0

I have a weird situation here where I've got a list of Contacts in a wrapper which has checkboxes, allowing users to process these records. These Contacts are obtained by a getting a query from the URL.

E.g. salesforce.com/apex/MyApp?Id=003D123,003D456,003789

I've got a function called removeSelected which modifys the URL and removes Ids, works great, a function called prescreenSelected which sends the Ids to another VisualForce page, works great, and a function called createApplication which creates an application, works great.

The problem comes from having these all in an Apex:Form and the fact the LookUp is a required field. Although the only function that uses the LookUp is createApplication, it's being processed and throwing a validation error when another function is used.

My VisualForce page looks like this below:

<apex:form>
  <div class="row">
    <div class="col-md-4">
      <apex:commandButton styleClass="btn btn-default form-control" value="Remove Selected" action="{!removeSelected}" />
    </div>
    <div class="col-md-4">
      <apex:commandButton styleClass="btn btn-default form-control" value="Prescreen Selected" action="{!prescreenSelected}" reRender="redirectpanel" oncomplete="redirect()" /          
    </div>
    <div class="col-md-4">
      <apex:inputField styleClass="form-control" id="candidate" value="{!applicationLookup.ts2__Job__c}" />
      <apex:commandButton styleClass="btn btn-default form-control" value="Create Application(s)" action="{!createApplications}" />
    </div>  
    <apex:pageBlock>
      <apex:pageBlockTable value="{!contacts}" var="c">
        <apex:column>
          <apex:inputCheckbox value="{!c.selected}" />
        </apex:column>
        <apex:column value="{!c.con.FirstName} {!c.con.LastName}" />
        <apex:column value="{!c.con.Phone}" />
        <apex:column value="{!c.con.MobilePhone}" />
        <apex:column value="{!c.con.EMail}" />
      </apex:pageBlockTable>
    </apex:pageBlock>
  </div>
</apex:form>

I'm guessing the VisualForce page is still posting the LookUp even though it's not used in two of the three functions and thus is still being processed for validation.

I've tried:

  • Using two Apex Forms to seperate the LookUp and createApplication button from the other two functions, but this renders them useless as they can't then get what's been selected in the list
  • Wrapping the buttons within an Apex:ActionRegion, but this seems to have no effect
  • Other horrible hacky things

Genuinely stuck on how to handle this one. Any help would be massively appreciated.

4
  • Action region should work as long as you have a rerender on the button.
    – Eric
    Commented Aug 5, 2015 at 17:16
  • So, really your question is how to make a field - in your case, the lookup - conditionally required and only enforced when the user clicks the Create Application button?
    – Mark Pond
    Commented Aug 5, 2015 at 17:20
  • @Eric I'll try to do this. Wasn't aware about using rerender on the button. Would this rerender target the form itself?
    – Dan Jones
    Commented Aug 6, 2015 at 7:41
  • @MarkPond In a sense. It's not so much that I don't want the validation to occur, rather I don't want the form to post that field when the method called doesn't use it. The edit to the title in this post doesn't really reflect what I asked in the question, but would achieve the same thing I suppose. Perhaps I wasn't clear enough.
    – Dan Jones
    Commented Aug 6, 2015 at 7:45

2 Answers 2

0

Moving comment to answer:

Action region should work as long as you have a rerender on the button

<div class="col-md-4">
      <apex:inputField styleClass="form-control" id="candidate" value="{!applicationLookup.ts2__Job__c}" />
      <apex:commandButton styleClass="btn btn-default form-control" value="Create Application(s)" action="{!createApplications}" />
    </div>  
    <apex:actionRegion id="ignoreApply">
      <div class="col-md-4">
        <apex:commandButton styleClass="btn btn-default form-control" value="Remove Selected" action="{!removeSelected}" reRender="ignoreApply" />
      </div>
      <div class="col-md-4">
        <apex:commandButton styleClass="btn btn-default form-control" value="Prescreen Selected" action="{!prescreenSelected}" reRender="redirectpanel, ignoreApply" oncomplete="redirect()" />
      </div>
0

Thanks to Eric who commented a solution that worked! As it happened the rerender function targeting the actionRegion worked! I'm assuming that because the createApplications is outside of this region, but still in the form that it can iterate through the list of selected Contacts and because the removeSelected and prescreenSelected are in an actionRegion wrapping in the form, this seperates them. The final thing looks like this:

<apex:form>
  <div class="row">
    <div class="col-md-4">
      <apex:inputField styleClass="form-control" id="candidate" value="{!applicationLookup.ts2__Job__c}" />
      <apex:commandButton styleClass="btn btn-default form-control" value="Create Application(s)" action="{!createApplications}" />
    </div>  
    <apex:actionRegion id="ignoreApply">
      <div class="col-md-4">
        <apex:commandButton styleClass="btn btn-default form-control" value="Remove Selected" action="{!removeSelected}" reRender="ignoreApply" />
      </div>
      <div class="col-md-4">
        <apex:commandButton styleClass="btn btn-default form-control" value="Prescreen Selected" action="{!prescreenSelected}" reRender="redirectpanel, ignoreApply" oncomplete="redirect()" />
      </div>
      <apex:pageBlock>
        <apex:pageBlockTable value="{!contacts}" var="c">
          <apex:column>
            <apex:inputCheckbox value="{!c.selected}" />
          </apex:column>
          <apex:column value="{!c.con.FirstName} {!c.con.LastName}" />
          <apex:column value="{!c.con.Phone}" />
          <apex:column value="{!c.con.MobilePhone}" />
          <apex:column value="{!c.con.EMail}" />
        </apex:pageBlockTable>
      </apex:pageBlock>
    </apex:actionRegion>
  </div>
</apex:form>

Notice the reRender and the actionRegion.

Credit where it's due, this was thanks to Eric's response. I'd happily remove this answer should he reply with an answer and mark it as correct for reputations sake.

1
  • Answer added. Please feel free to add context from this post (to my answer or your questions) if you would like
    – Eric
    Commented Aug 6, 2015 at 13:42

You must log in to answer this question.

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