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
steve_andersensteve_andersen 

Getting a Contact lookup for Opportunity Contact Role ?

I've got a wizard for creating a new Opp which I referenced in a previous post. I want the user to have the option of selecting an existing Contact to be the primary Contact on the Opp. On my VF page I need a Contact lookup and then on Save I'll create the ContactRole for the Opportunity and use that Contact Id.

Problem is that I don't think I can get an inputField lookup without instantiating an object and then binding to the field. I can instantiate an Opportunity Contact Role object, but Contact is required on that object so the lookup on my VF page has the big red required flag. Selecting a Contact isn't required for my wizard, so that won't work.

My workaround is to instantiate a completely unrelated custom object that happens to have a Contact lookup field on it. I catch the Contact Id in there, and then us it to create the Contact role. It works. But it's ugly. Sharing this code now just got harder because there is now this dependence on a custom object that has nothing to do with this solution.

Any ideas? Thanks!

Steve
jlojlo
Steve, at first I thought that you were going to have to use a different 'proxy' object to get the appropriate Contact lookup field permissions. (For other folks, there's a good explanation of what this looks like in this post.) I did a little experiment though, and setting the required attribute to false seems to work:

<apex:page controller="ProxyObjectDemoController" tabStyle="Opportunity">
<apex:sectionHeader title="New Opportunity and Contact Role"/>

<apex:form >
<apex:pageBlock title="Opportunity Information" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>

<apex:pageBlockSection title="Opportunity Information">
<apex:inputField id="opportunityName" value="{!opportunity.name}"/>
<apex:inputField id="opportunityCloseDate" value="{!opportunity.closeDate}"/>
<apex:inputField id="opportunityStageName" value="{!opportunity.stageName}"/>
</apex:pageBlockSection>

<apex:pageBlockSection title="Contact Role Information">
<apex:inputField id="contact" value="{!role.contactId}" required="false"/>
<apex:inputField id="contactRole" value="{!role.role}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 
public class ProxyObjectDemoController {

    Opportunity o;
    OpportunityContactRole r;
   
    public Opportunity getOpportunity() {
        if(o == null) o = new Opportunity();
        return o;
    }
    
    public OpportunityContactRole getRole() {
        if(r== null) r= new OpportunityContactRole();
        return r;
    }
        
    
    public PageReference save() {
    
        insert o;
        
        if(r.contactId != null){
   r.opportunityId = o.id;

         insert r;
        }
        
        PageReference opptyPage = new PageReference('/' + o.id);
        opptyPage.setRedirect(true);
        return opptyPage;
    }
}

 


steve_andersensteve_andersen
That does work! Thanks, it's exactly what I needed.

Steve