0

The case is next. On Custom vf page with a standard controller user might to select Users, He hit the Add users button and opens the second vf page (custom controller) with multiselect field. After selecting values user hits "save" button, and selected values sets in field calles "Assigned To".

Im new in apex and i faced with that at the first time. I don't know how to pass values from multiselect from one page to another. I think that could be done through url, but i don't really know how to do that.

This is the first page:

 <apex:page showHeader="true" tabStyle="Task" StandardController="Task">
    <style type="text/css">
      .activeTab {background-color: #236FBD; color:white; 
         background-image:none}
      .inactiveTab { background-color: lightgrey; color:black; 
         background-image:none}
      .comments {width: 425px; height: 100px;}
      .name {width: 142px;}
    </style>
    <script>
    function OpenVfpageUsers(){
      window.open("/apex/SelectUser","_blank","scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");     
    }

    function OpenVFpageContacts(){
      window.open("/apex/SelectContact","_blank","scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");     
    }

    function ClearValueAssignedTo(){
        document.getElementById("AssTo").value = "My value";
    }
   </script>
 <apex:sectionHeader title="Log a Call" />
  <apex:form >
   <apex:pageBlock title="Task Edit" mode="edit" >
    <apex:pageBlockButtons location="top">
        <apex:commandButton value="Save" action="{!save}"/>
        <apex:commandButton value="Cancel" action="{!cancel}"/>
    </apex:pageBlockButtons>     
    <apex:pageBlockSection title="Additional Information">
        <apex:OutputField value="{!Task.Status}"/>
        <apex:OutputField value="{!Task.Who.Phone}"/>
        <apex:InputField value="{!Task.Communication_Type__c}" required="true"/>
        <apex:outputField value="{!Task.Who.Email}"/>
        <apex:InputField value="{!Task.Location__c}"/>
        <apex:outputLabel ></apex:outputLabel>
        <apex:InputField value="{!Task.Client_Name__c}"/>
    </apex:pageBlockSection>  
    <apex:pageBlockSection title="Task Information">
        <apex:InputField value="{!Task.Assigned_To__c}" required="true" id="AssTo">
            <apex:commandButton value="Add User" Onclick="OpenVfpageUsers();" reRender="none"/>
        </apex:InputField>
        <apex:InputField value="{!Task.WhatID}" />
        <apex:InputField value="{!Task.Subject}" required="true"/>
        <apex:InputField value="{!Task.Name__c}" styleClass="name">
            <apex:commandButton value="Add Contact" Onclick="OpenVFpageContacts();" reRender="none"/>
        </apex:InputField>    
        <apex:InputField value="{!Task.ActivityDate}" />
        <apex:outputLabel ></apex:outputLabel>
        <apex:InputField value="{!Task.Description}" styleClass="comments"/>
        <apex:outputLabel ></apex:outputLabel>
        <apex:InputField value="{!Task.Stocks_of_Interest__c}" />
    </apex:pageBlockSection> 
   </apex:pageBlock>
   <apex:pageBlock title="Schedule follow-up task" mode="edit">
    <apex:pageBlockSection title="Task Information">
    </apex:pageBlockSection>
    <apex:pageblockSection title="Additional Information">
    </apex:pageblockSection>
    <apex:pageBlockSection title="Reminder">
    </apex:pageBlockSection>
    <apex:pageBlockButtons location="bottom">
        <apex:commandButton value="Save" action="{!save}"/>
        <apex:commandButton value="Cancel" action="{!cancel}"/>
    </apex:pageBlockButtons>
   </apex:pageBlock>
  </apex:form>
</apex:page>

This is the second page with multiselect:

    <apex:page controller="MultiselectExampleController" showHeader="false" sidebar="false">
    <apex:form >
        <apex:pageBlock title="Users">
            <c:multiselectpicklist leftLabel="Available Users"
                leftOption="{!allUsers}"
                rightLabel="Selected Users"
                rightOption="{!selectedUsers}"
                size="14"
                width="150px"/>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
    <apex:outputText >{!message}</apex:outputText>
</apex:page>

And Controller for second page:

 public with sharing class MultiselectExampleController {

    public String getTask() {
        return null;
    }

    public SelectOption[] selectedUsers { get; set; }
    public SelectOption[] allUsers { get; set; }

    public String message { get; set; }

    public MultiselectExampleController() {  

        selectedUsers = new List<SelectOption>();

        List<User> users = [SELECT Name, Id FROM User Order BY Name ASC];    
        allUsers = new List<SelectOption>();
        for ( User u : users ) {
            allUsers.add(new SelectOption(u.Id, u.Name));
        }
    }

    public PageReference save() {
        message = 'Selected Users: ';
        Boolean first = true;
        for ( SelectOption so : selectedUsers ) {
            if (!first) {
                message += ', ';
            }
            message += so.getLabel() + ' (' + so.getValue() + ')';
            first = false;
        }

        return null;       
        }
      }
  • I feel merging page together would be better option. Or may be two pages and single controller can be followed. – Mukesh Verma Jun 8 '16 at 12:51
  • Hi Mukesh, I would like to use standard controller for first page. And how can i merge this pages? – Viktor Jun 8 '16 at 13:00
  • Even when you use standard controller you have extensions attribute to specify second controller. – Mukesh Verma Jun 8 '16 at 13:20
3

Follow the below approach

  1. Have single controller class to have logic from both visualforce pages.
  2. If Page has standard controller use that class as extension
  3. Since controller is same you will not need to pass values anywhere. Keep variable as it is.
  4. Use Pagereference to redirect from one page to second. Set setRedirect(false) for Pagereference instance so, state can be maintained.

Alternative

Pass selected values in URL setting them in Pagereference parameter. Take any parameter name like selectedvalue etc. Join selected values by ; and read then on other page controller and split it using String class split() method.

  • Hi Ashwani! I used the second page as popup, and i might set the values from selected Users to the field Assigned to (text field) on save button. How can this be done? I think i don't need to use Page reference. For now i relate one controller to two vf pages. – Viktor Jun 9 '16 at 8:49

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.