2

I currently have a VF page and custom controller that collects name and email and searches for contacts that match the email, if it finds 1 contact, it 'registers' that contact for our training course.

I'm looking to update this form to allow for multiple people to be entered at one time.

The relevant part of the current VF page looks like this:

<apex:form >
   <apex:inputText value="{!firstName}" id="FirstName"/>
   <apex:inputText value="{!lastName}" id="LastName"/>
   <apex:inputText value="{!emailAddress}" id="Email"/>
   <button type="button" data-dismiss="modal">No, thank you</button>
   <div type="button" value="Confirm"> Register
      <apex:actionFunction action="{!registerForCourse}" rerender=""></apex:actionFunction>
   </div>
</apex:form>

The relevant part of the current controller is:

public PageReference registerForCourse(){
   ContactList = [SELECT Id, FirstName, LastName, Email FROM Contact WHERE Email = :emailAddress];

   if(!ContactList.isEmpty() && ContactList.size() == 1){ //if a contact was found, and only 1 contact was found

      //code to 'register' the contact

   }
}

I could just add a static amount of name and email fields (maybe 5)to the VF page and then independently check each of these in the controller, but I'd like to put the entries into a list, and then cycle through them.

Ideally I would like a way for the user to indicate how many names and email addresses they want to enter, and then that amount of name and email fields would be generated for them to fill in, but I would satisfied with the 5 static fields for now.

How can I capture these fields in a list, and cycle through the entries to check if they match? Also is there a way to do only one SOQL query using this method?

Any help would be appreciated, thanks!

  • Can you write your complete code and put the issue which you are facing. Current code is far behind from your expected usecase – Santanu Boral Sep 19 '17 at 23:38
  • 1
    you can create a visualforce component considering name, email and try to add those components dynamically in VFP – Santanu Boral Sep 19 '17 at 23:40
2

One suggestion would be to allow them to add another entry and store them here and when they click submit - you can then take in the values that they enter and then query for a list of contacts this way. I haven't run the below code - but it could serve for a good example of how to achieve this.

Form/Section Example:

<apex:actionFunction name="addRow" action="{!addRow}" rerender="formId" />
<apex:form id="formId" >

    <apex:repeat value="{!registryList}" var="reg" >
        <apex:inputText value="{!reg.firstName}" id="FirstName"/>
        <apex:inputText value="{!reg.lastName}" id="LastName"/>
        <apex:inputText value="{!reg.emailAddress}" id="Email"/>      
    </apex:repeat>
    <button class="btn" id="addRowButton" onClick="addRow()">Add Row</button>

    <button type="button" data-dismiss="modal">No, thank you</button>
    <div type="button" value="Confirm"> Register
        <apex:actionFunction action="{!registerForCourse}" rerender=""></apex:actionFunction>
    </div>
</apex:form>

Controller:

public class RegistrationSearchController{
    public List<Registration> registryList  { get; set; }
    public List<Contact> contactList    { get; set; }

    public RegistrationSearchController(){
        registryList = new List<Registration>();
        addRow();
    }

    public void addRow(){
        registryList.add( new Registration() );
    }

    public PageReference registerForCourse(){
        Set<String> emailAddresses = new Set<String>();

        for( Registration reg : registryList ){
            if( !emailAddresses.contains( reg.emailAddress ) && !String.isBlank( reg.emailAddress )){
                emailAddresses.add( reg.emailAddress );
            }
        }
        contactList = [SELECT Id, FirstName, LastName, Email FROM Contact WHERE Email IN :emailAddresses];

       if(!ContactList.isEmpty() ){ //if a contact was found, and only 1 contact was found

          //code to 'register' the contact

       }
    }
    public class Registration{
        public String firstName     { get; set; }
        public String lastName      { get; set; }
        public String emailAddress  { get; set; }
    }

}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.