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;
}
}
Best Answer
Follow the below approach
Pagereference
to redirect from one page to second. SetsetRedirect(false)
forPagereference
instance so, state can be maintained.Alternative
Pass selected values in URL setting them in
Pagereference
parameter. Take any parameter name likeselectedvalue
etc. Join selected values by;
and read then on other page controller and split it usingString
classsplit()
method.