You need to sign in to do that
Don't have an account?
Reference/Insert Custom Junction Object in Visualforce.com page
I am trying to create a visualforce page (rendered as PDF) that would open with a custom button on the opportunity page. So the standardController I am using is Opportunity. But i cannot figure out how to reference or insert the value from a custom junction object I've created (called Designated Contact Roles - which replaces the native contact roles related lists).
Can anyone help me understand how to call values from the custom fields within the custom junction object in my visualfoce page?
Any help would be great greatly appreciated. I am very new to visualforce, I am jsut learning all of its capabitlities, but I am very intrigued and excited by all the things I'm learning it can do.
You need to query the records from the junction object related to the current opportunity in the controller, then you can display these in the vfpage.
Ex.
private List<Designated_Contact_Roles__c> myDesignatedContas = [SELECT Id, Contact__c, Role__c, Opportunity__c FROM Designated_Contact_Roles__c WHERE OpportunityId = opp.Id];//opp.Id is the id of the current opportunity.
public List<Designated_Contact_Roles__c> getMyDesignatedContacts(){
return myDesignatedContacts;
}
This will return a list of all Designated_Contact_Roles__c related to the current opp. Simply add an apex:pageBlockTable to your vfPage.
Ex.
<apex:PageBlock>
<apex:pageBlockSection title="Designated Contact Roles">
<apex:pageBlockTable value="{!myDesignatedContacts}" var="dc">
<apex:column value="dc.Contact__c"/>
<apex:column value="dc.Role__c"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:PageBlock>
Hope this helps!
I am a Group Edition user, so I don't think i am able to create extensions or Apex classes. I am assuming the query your suggesting I run has to be done in an Apex class? Can I run a query directly from my Visualforce page?
Do you want to display the junction object as a related list? if so you can use the <apex:relatedList> tag.
Yes, you can query from vfpage using javascript.
Ex.
<apex:page standardController="Opportunity">
<script type="text/javascript" src="/soap/ajax/22.0/connection.js">
<script type="text/javascript">
//Query to get records
var myQuery = "SELECT Id, Contact__r.FirstName, Contact__r.LastName, Role__c, Opportunity__c FROM Designated_Contact_Roles__c WHERE Opportunity__c = {!Opportunity.Id}";
//Get the records from SF
result = sforce.connection.query(myQuery);
records = result.getArray("records");
//Display records in the page
var tableData = document.getElementById("MyTable").innerHTML;
for(var i = 0; i < records.length; i++){
tableData += "<tr><td>" + records[i].Contact__r.FirstName + "</td><td>" + records[i].Contact__r.LastName + "</td><td>" + records[i].Role__c + "</td></tr>";
}
document.getElementById("MyTable").innerHTML = tableData;
</script>
<table id="MyTable">
<tr><th>First Name</th><th>LastName</th><th>Role</th></tr>
</table>
</apex:page>
Simply style the table to fit your page layout.
Hope this helps!
Below is the code I was using. With the push of the button it created a PDF of an Opportunity with the specific info we need it to display.
But then we wanted the Account page to show (directly on the account page) the exact contacts that were selected in the opportunity contact roles.
After I learned that you cannot simply add a formula that would pull the Contact Roles onto the Account page, I decided to create a custom junction object that would display the Designated Contacts on both the Opportunity and Account page.
Now the problem is how do I get those custom object fields (see them below) to show up on the PDF of the opportunity. Yes I'd love the junction object to display as a related list (with all their associated information along with it, name, designated role, title, email, phone).
ADMIN (primary) ADMIN__c Checkbox
Company Company__c Master-Detail(Account)
Contact Contact__c Lookup(Contact)
Designated Role Designated_Role__c Picklist
Opportunity Name Opportunity_Name__c Master-Detail(Opportunity)
Singular Label Designated Contact Role
Plural Label Designated Contact Roles
Object Name Designated_Contact_Role
API Name Designated_Contact_Role__c
Try adding the <apex:relatedList> below the apex:pageBlock. To find the list name got to Your Name >> Setup >> Create >> Objects >> Designated Contact Role. Click on the Opportunity Name field. The name under Child Relationship Name is the list name, use that name for the list attribute of the apex:relatedList tag. Add __r after the list name.
Ex.
<apex:relatedList list="ChildRelationName__r" pageSize="10">
Here is another option. This will give you a table with the data you want. You can style the table using css. Keep in mind that this code uses jQuery, so you need to add jQuery as a Static Resource.