+ Start a Discussion
Kimberly Allen 15Kimberly Allen 15 

displaying list on unrelated object

Hi,
I am trying to display the opportunitycontactroles list on an object (Deal_Desk_Request__c) related to the opportunity.  I am not getting errors when saving the code, but nothing is appearing in the visualforce page other than the headers.  I am new to apex/visualforce development and not sure what I am doing wrong.  Can someone please help?

Here is the visualforce page:
<apex:page standardcontroller="deal_desk_request__c" extensions="dealdeskcontactrole" >
  <apex:pageblock >
      <apex:pageblocktable value="{!roles}" var="role">
          <apex:column headervalue="Contact" >
                      <apex:outputfield value="{!role.contactid}"/>
                      </apex:column>     
          <apex:column headervalue="Role" >
                      <apex:outputfield value="{!role.role}"/>
                      </apex:column>           
          <apex:column headervalue="Primary" >
                      <apex:outputfield value="{!role.isprimary}"/>
                      </apex:column>           


      </apex:pageblocktable>
  </apex:pageblock>
</apex:page>

Here is the extension class:
public with sharing class dealdeskcontactrole {
list<opportunitycontactrole> roles;
 public final Deal_Desk_Request__c DDR;
    public dealdeskcontactrole(ApexPages.StandardController controller) {
   
        this.DDR = (Deal_Desk_Request__c)controller.getRecord();
    }

    public list<opportunitycontactrole> getroles() {
        try{
        list<opportunitycontactrole> roles = [select id, contactID, Role, Isprimary from opportunitycontactrole where Opportunityid =: DDR.Opportunity__r.id];
         return roles;
        }
        catch(system.exception e){
        return null;
        }     
    }
   }

 
Best Answer chosen by Kimberly Allen 15
GauravendraGauravendra
Hi Kimberly,

The way you have extracted current Deal_Desk_Request__c .i,e,
this.DDR = (Deal_Desk_Request__c)controller.getRecord();
will only give you the ID of Deal_Desk_Request__c . Not all the information. [You can test this by debugging entire DDR in next line in constructor. You will only get the ID].
The error is when in getRoles() you are refering the field that is not fetched in SOQL. So, the DDR.Opportunity__r.id is null.

So, you can add a SOQL query that will fetch the related opportunity. And pass that opp in original query.
public with sharing class dealdeskcontactrole {
    List<opportunitycontactrole> roles{get;set;}
 public final Deal_Desk_Request__c DDR;
    public dealdeskcontactrole(ApexPages.StandardController controller) {
   
        this.DDR = (Deal_Desk_Request__c)controller.getRecord();
		System.debug('Constructor##'+DDR);
    }

    public List<opportunitycontactrole> getroles() {
        try{
		Deal_Desk_Request__c deskOpp = [Select Id,Name,Opportunity__c from Deal_Desk_Request__c where ID=: DDR.Id];
        roles = [select id, contactID, Role, Isprimary from opportunitycontactrole where Opportunityid =: deskOpp.Opportunity__c];
         
        }
        catch(system.exception e){
        roles= null;
        }    
		return roles;		
    }
   }
Hope this helps.
 

All Answers

GauravendraGauravendra
Hi Kimberly,

The way you have extracted current Deal_Desk_Request__c .i,e,
this.DDR = (Deal_Desk_Request__c)controller.getRecord();
will only give you the ID of Deal_Desk_Request__c . Not all the information. [You can test this by debugging entire DDR in next line in constructor. You will only get the ID].
The error is when in getRoles() you are refering the field that is not fetched in SOQL. So, the DDR.Opportunity__r.id is null.

So, you can add a SOQL query that will fetch the related opportunity. And pass that opp in original query.
public with sharing class dealdeskcontactrole {
    List<opportunitycontactrole> roles{get;set;}
 public final Deal_Desk_Request__c DDR;
    public dealdeskcontactrole(ApexPages.StandardController controller) {
   
        this.DDR = (Deal_Desk_Request__c)controller.getRecord();
		System.debug('Constructor##'+DDR);
    }

    public List<opportunitycontactrole> getroles() {
        try{
		Deal_Desk_Request__c deskOpp = [Select Id,Name,Opportunity__c from Deal_Desk_Request__c where ID=: DDR.Id];
        roles = [select id, contactID, Role, Isprimary from opportunitycontactrole where Opportunityid =: deskOpp.Opportunity__c];
         
        }
        catch(system.exception e){
        roles= null;
        }    
		return roles;		
    }
   }
Hope this helps.
 
This was selected as the best answer
Kimberly Allen 15Kimberly Allen 15
Hi Gauravendra,

Thank you so much for your reply and help on the class.  It solved my issue!!

Thanks,
Kimberly