function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jojawsjojaws 

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.

 

izayizay

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!

jojawsjojaws

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? 

izayizay

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!

jojawsjojaws

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

 

 

<apex:page renderAs="pdf" standardController="Opportunity">
<head>
 <style type="text/css">
  @page
  {

    /* Landscape orientation */
    size:landscape;

    /* Put page numbers in the bottom right corner of each
    page in the pdf document. */
    @bottom-right {
      content: "Page " counter(page);
    }
  }
  </style>
  </head>

<html>
 <head>
  <style> 
body {font-family: Arial Unicode MS;}
     .companyName {
    font-weight:bold;
    font-size:30px;
    color:red}

  </style>
 </head>
  <left>

<apex:pageBlock title="Membership Account Information">

   <apex:pageBlockSection title="Company Info" columns="1">
                    <apex:outputField value="{!opportunity.AccountID}"/>
                    <apex:outputField value="{!opportunity.Industry__c}"/>
                    <apex:outputField value="{!Opportunity.Account.BillingStreet}"/>
                    <apex:outputField value="{!Opportunity.Account.BillingCity}"/>
                    <apex:outputField value="{!Opportunity.Account.BillingState}"/>
                    <apex:outputField value="{!Opportunity.Account.BillingPostalCode}"/>
   </apex:pageBlockSection>
   
   <apex:pageblockSection title="Designated Contacts"> 
       <apex:pageBlockTable value="{!Opportunity.OpportunityContactRoles}" cellpadding="3" columnsWidth="15%,2%,18%,16%,10%,15%" width="1000px" var="roles">
            <apex:column value="{!roles.Contactid}"/> 
            <apex:column headerValue="ADMIN">
                        <apex:outputField value="{!roles.isprimary}"/> 
                    </apex:column>
            <apex:column value="{!roles.Role}"/> 
<apex:column value="{!roles.Contact.Title}"/> <apex:column value="{!roles.Contact.Email}"/> <apex:column value="{!roles.Contact.Phone}"/> <apex:column value="{!roles.Contact.MobilePhone}"/> </apex:pageBlockTable> </apex:pageblockSection> </apex:pageBlock> </left> <br> </br> <br> </br> <br> </br> <br> </br> <br> </br> {!NOW()} </html> </apex:page>
izayizay

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">

izayizay

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.

 

<apex:page standardController="Account" renderAs="pdf">
    <script type="text/javascript" src="/static/013008/js/functions.js"/>
    <script type="text/javascript" src="/soap/ajax/22.0/connection.js"/>
    <!-- include jQuery -->
    <script type="text/javascript" src="{!URLFOR($Resource.jQuery)}"/>
    
    <script type="text/javascript">
        //alert("In script block");
        $j = jQuery.noConflict();
        $j(document).ready(function(){
            //alert("In ready function");
            //Query to get records
            var myQuery = "SELECT Company__c, Contact__c, Designated_Role__c FROM Designated_Contact_Role__c WHERE  Opportunity_Name__c = '{!Opportunity.Id}'";
            //alert(myQuery);
            //Get the records from SF
            try{
                sforce.connection.sessionId = '{!$Api.Session_ID}';
                result = sforce.connection.query(myQuery);
                records = result.getArray("records");
                //alert(records.length);
                //Display records in the page

                var tableData = "<table><tr><th>Company Name</th><th>Contact Name</th><th>Role</th></tr>";
                //alert(tableData);
                for(var i = 0; i < records.length; i++){

                    tableData += "<tr><td>" + records[i].Company__c + "</td><td>" + records[i].Contact__c + "</td><td>" + records[i].Designated_Role__c + "</td></tr>";

                }
                tableData += "</table>";
                //alert(tableData);
                document.getElementById("MyTable").innerHTML = tableData;
            }catch(e){
                alert(e);
            }
        });
    </script>
    
    <!-- Style -->
    <style type="text/css">
        #MyTable table{
            width: 100%;
        }
        #MyTable td{
            padding: 5px;
        }
    </style>
    
    <head>
<style type="text/css">
  @page
  {

    /* Landscape orientation */
    size:landscape;

    /* Put page numbers in the bottom right corner of each
    page in the pdf document. */
    @bottom-right {
      content: "Page " counter(page);
    }
  }
  </style>
  </head>

<html>
<head>
  <style>
body {font-family: Arial Unicode MS;}
     .companyName {
    font-weight:bold;
    font-size:30px;
    color:red}

  </style>
</head>
  <left>

<apex:pageBlock title="Membership Account Information">

   <apex:pageBlockSection title="Company Info" columns="1">
                    <apex:outputField value="{!opportunity.AccountID}"/>
                    <apex:outputField value="{!opportunity.Industry__c}"/>
                    <apex:outputField value="{!Opportunity.Account.BillingStreet}"/>
                    <apex:outputField value="{!Opportunity.Account.BillingCity}"/>
                    <apex:outputField value="{!Opportunity.Account.BillingState}"/>
                    <apex:outputField value="{!Opportunity.Account.BillingPostalCode}"/>
   </apex:pageBlockSection>
  
   <apex:pageblockSection title="Designated Contacts">
       <apex:pageBlockTable value="{!Opportunity.OpportunityContactRoles}" cellpadding="3" columnsWidth="15%,2%,18%,16%,10%,15%" width="1000px" var="roles">
            <apex:column value="{!roles.Contactid}"/>
            <apex:column headerValue="ADMIN">
                        <apex:outputField value="{!roles.isprimary}"/>
                    </apex:column>
            <apex:column value="{!roles.Role}"/>
            <apex:column value="{!roles.Contact.Title}"/>
            <apex:column value="{!roles.Contact.Email}"/>
            <apex:column value="{!roles.Contact.Phone}"/>
            <apex:column value="{!roles.Contact.MobilePhone}"/>
            </apex:pageBlockTable>
   </apex:pageblockSection>
   
</apex:pageBlock>
    <!-- Table with related records --> <h1>My Table</h1> <div id="MyTable"> </div>
</left> </apex:page>