You need to sign in to do that
Don't have an account?
Andrew Aldis
Visual Force picklist with other records
I have 3 objects Scoping which is a parent object for both Packages and Sign Instances, I created a junction object so users could relate sign isntances to the appropriate packages. Both Sign Instances and packages can be related to multiple records in both objects. I then created a visualforce page which is inserted into the Packages object that will automatically create and display the junction records. I want to crate a picklist to pull in all sign instances related to the scoping object so users do not have to use a look up field, but I cannot find an example of this scenario.
Apex Class
public class PackageSignInstanceJunctionController {
// Constructor
public PackageSignInstanceJunctionController(ApexPages.StandardController controller)
//master
{
this.proj = (Package__c)controller.getRecord();
// child
this.junction = [ SELECT
t.Package__c, t.Sign_Instance__c, t.Name, t.Id
FROM Package_Sign_Instance_Junction__c t
WHERE
t.Package__c = :proj.id
ORDER BY t.CreatedDate Desc];
}
//Picklist Options
// Insert new Staff junction
public pagereference insertmethod()
{
Package_Sign_Instance_Junction__c cc= new Package_Sign_Instance_Junction__c();
cc.Package__c = proj.id;
insert cc;
return null;
}
public Package_Sign_Instance_Junction__c[] getjunction() {
return this.junction;
}
// Action Method called from page button
public pagereference saveChanges() {
upsert this.junction;
pageRef.setRedirect(true);
return pageRef;
}
// Action Method called from page link
public pagereference newjunction() {
Package_Sign_Instance_Junction__c d = new Package_Sign_Instance_Junction__c();
d.Package__c = this.proj.id;
junction.add( d );
getjunction();
return null;
}
public pagereference DeleteAccount()
{
// if for any reason we are missing the reference
if (SelectedAccountId == null)
{
return null;
}
// find the account record within the collection
Package_Sign_Instance_Junction__c tobeDeleted = null;
for(Package_Sign_Instance_Junction__c a : this.junction)
if (a.Id == SelectedAccountId)
{
tobeDeleted = a;
break;
}
//if account record found delete it
if (tobeDeleted != null)
{
Delete tobeDeleted;
}
pageRef.setRedirect(true);
return pageRef;
}
// class variables
PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl());
Package__c proj;
Package_Sign_Instance_Junction__c[] junction;
Sign_Instance__c[] sign;
public string SelectedAccountId { get; set; }
}
Visual Force Page
<apex:page standardController="Package__c" extensions="PackageSignInstanceJunctionController">
<apex:messages />
<apex:form >
<apex:PageBlock id="happy" >
<apex:pageBlockButtons >
<apex:commandLink value="Save" action="{!savechanges}" rerender="main" styleClass="btn" style="text-decoration:none;padding:4px;"/>
<apex:commandButton action="{!newjunction}" value="New" />
</apex:pageBlockButtons>
<apex:actionStatus id="ajaxStatus" startText="Updating schedules...">
<apex:facet name="stop">
<apex:outputPanel id="main" >
<table>
<tr>
<td style="width:50px; margin-right:50px"><b>Sign</b></td>
</tr>
<apex:repeat value="{!junction}" var="a">
<tr>
<td><apex:inputField value="{!a.Sign_Instance__c}" style="width:200px" rendered="{!IF(a.Sign_Instance__c =null,true,false)}"/>
<apex:OutputField value="{!a.Sign_Instance__c}" style="width:200px" rendered="{!IF(a.Sign_Instance__c !=null,true,false)}"/></td>
<td><a href="javascript:if (window.confirm('Are you sure?')) DeleteAccount('{!a.Id}');" style="font-weight:bold">Del</a></td>
</tr>
</apex:repeat>
</table>
</apex:outputPanel>
</apex:facet>
</apex:actionStatus>
<apex:actionFunction action="{!DeleteAccount}" name="DeleteAccount" reRender="main" >
<apex:param name="accountid" value="" assignTo="{!SelectedAccountId}"/>
</apex:actionFunction>
</apex:pageblock>
</apex:form>
</apex:page>
Apex Class
public class PackageSignInstanceJunctionController {
// Constructor
public PackageSignInstanceJunctionController(ApexPages.StandardController controller)
//master
{
this.proj = (Package__c)controller.getRecord();
// child
this.junction = [ SELECT
t.Package__c, t.Sign_Instance__c, t.Name, t.Id
FROM Package_Sign_Instance_Junction__c t
WHERE
t.Package__c = :proj.id
ORDER BY t.CreatedDate Desc];
}
//Picklist Options
// Insert new Staff junction
public pagereference insertmethod()
{
Package_Sign_Instance_Junction__c cc= new Package_Sign_Instance_Junction__c();
cc.Package__c = proj.id;
insert cc;
return null;
}
public Package_Sign_Instance_Junction__c[] getjunction() {
return this.junction;
}
// Action Method called from page button
public pagereference saveChanges() {
upsert this.junction;
pageRef.setRedirect(true);
return pageRef;
}
// Action Method called from page link
public pagereference newjunction() {
Package_Sign_Instance_Junction__c d = new Package_Sign_Instance_Junction__c();
d.Package__c = this.proj.id;
junction.add( d );
getjunction();
return null;
}
public pagereference DeleteAccount()
{
// if for any reason we are missing the reference
if (SelectedAccountId == null)
{
return null;
}
// find the account record within the collection
Package_Sign_Instance_Junction__c tobeDeleted = null;
for(Package_Sign_Instance_Junction__c a : this.junction)
if (a.Id == SelectedAccountId)
{
tobeDeleted = a;
break;
}
//if account record found delete it
if (tobeDeleted != null)
{
Delete tobeDeleted;
}
pageRef.setRedirect(true);
return pageRef;
}
// class variables
PageReference pageRef = new PageReference(ApexPages.currentPage().getUrl());
Package__c proj;
Package_Sign_Instance_Junction__c[] junction;
Sign_Instance__c[] sign;
public string SelectedAccountId { get; set; }
}
Visual Force Page
<apex:page standardController="Package__c" extensions="PackageSignInstanceJunctionController">
<apex:messages />
<apex:form >
<apex:PageBlock id="happy" >
<apex:pageBlockButtons >
<apex:commandLink value="Save" action="{!savechanges}" rerender="main" styleClass="btn" style="text-decoration:none;padding:4px;"/>
<apex:commandButton action="{!newjunction}" value="New" />
</apex:pageBlockButtons>
<apex:actionStatus id="ajaxStatus" startText="Updating schedules...">
<apex:facet name="stop">
<apex:outputPanel id="main" >
<table>
<tr>
<td style="width:50px; margin-right:50px"><b>Sign</b></td>
</tr>
<apex:repeat value="{!junction}" var="a">
<tr>
<td><apex:inputField value="{!a.Sign_Instance__c}" style="width:200px" rendered="{!IF(a.Sign_Instance__c =null,true,false)}"/>
<apex:OutputField value="{!a.Sign_Instance__c}" style="width:200px" rendered="{!IF(a.Sign_Instance__c !=null,true,false)}"/></td>
<td><a href="javascript:if (window.confirm('Are you sure?')) DeleteAccount('{!a.Id}');" style="font-weight:bold">Del</a></td>
</tr>
</apex:repeat>
</table>
</apex:outputPanel>
</apex:facet>
</apex:actionStatus>
<apex:actionFunction action="{!DeleteAccount}" name="DeleteAccount" reRender="main" >
<apex:param name="accountid" value="" assignTo="{!SelectedAccountId}"/>
</apex:actionFunction>
</apex:pageblock>
</apex:form>
</apex:page>