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
ACDSeeACDSee 

Error: Invalid field Contact for SObject Opportunity

Hello,

 

I am trying to make some edits to the "Simple Quote" application from the appexchange.

 

I am able to included fields for Opportunities and Accounts, but unable to figure out the syntax for the primary contact associated with the opportunity?

 

For example:

 

This works...

{!Opportunity.Account.BillingStreet}
{!Opportunity.QuoteNumber__c}

{!Opportunity.Owner.FirstName}

 

but this won't work (and produces the error in the subject line of this post):

{!Opportunity.Contact.Name}

 

I realize an Opportunity can have multiple Contacts, but ideally I would like to include the information for the Decision Maker in the quote. 

 

Anyone help?

JimRaeJimRae

There is no Contact directly related to the Opportunity.  You will need to query the OpportunityContactRole object with your specific criteria (i.e. Contact Role) and leverage the result returned.

 

ACDSeeACDSee
Thank you for the response, any help on how to do this would be appreciated :)
JimRaeJimRae

You need to modify your VP page tag to include the extensions parameter, like this:

 

 

<apex:page standardController="Opportunity" extensions="quoteEXT" showHeader="false" renderas="pdf">

 

 Then create a simple extension that gets the opportunity, queries the OCR object and populates a value you can use like this:

 

 

public class quoteEXT { public final Contact primContact {get; set;} public final Opportunity o {get; set;} public quoteEXT(ApexPages.StandardController controller) { this.o=(Opportunity)controller.getrecord(); try{ ID primContactid = [select id,contactid from opportunitycontactrole where opportunityid=:o.id and isprimary=true LIMIT 1].contactid; primContact = [select id,name from contact where id=:primContactid limit 1]; }catch(QueryException qe){ system.debug('ERROR Querying contact:'+qe.getmessage()); } } }

 

 You can add more field names to your SOQL query if you need more information about the contact.  and you could change the where clause if you wanted to use the role instead of the isprimary parameter, just make sure you limit the results to 1 record.

 

Now, you have the contact available as the name "primcontact" just like you were doing before, so you could add a VF tag like this to your VF page:

 

 

</td> <td align="right"><font face="Arial" > <b>Quote for {!Opportunity.Account.Name}</b></font><br/> <b>{!primcontact.Name}</b></font><br/> </td>

 

 Hopefully, that will get you started.

 

Good Luck!

 

 

 

ACDSeeACDSee

"create a simple extension"

 

Am I missing something...I can't find an option or section anywhere to create Extensions  

JimRaeJimRae

If you look in the documentation

 

http://www.salesforce.com/us/developer/docs/pages/salesforce_pages_developers_guide.pdf

 

Chapter 6 (starting on page 58), explains this in detail.

ACDSeeACDSee

Aha!

 

Here is the problem...

  1. In the application, click Setup Develop Apex Classes.
  2. Click New.

I don't have the NEW button, even though I am the System Administrator.

 

Thank you for all the help JimRae, I have contacted Saleforce to help me out with this now :)

JimRaeJimRae

You can't create Apex Classes directly in production, that could be your issue.

You need to create them in a development environment (or sandbox) and then deploy them to production along with the appropriate testmethod.

 

Other than that, the only other thing I can think of would be your profile doesn't have the "Author Apex" option selected.

B2AB2A

Hi Guys,

 

I noticed that an extension was used here to get the Primary Contact of the Opportunity Contact Role.  I'm not sure if I read the requirement correctly or not but wouldn't just plain VF do the trick (again sorry if I misread this).  Here is the code I used on my VF page just to get the Primary contact.

 

<i>Opportunity : Primary Contact -</i>
    <apex:outputPanel >
        <apex:repeat var="opcr" value="{!relatedTo.OpportunityContactRoles}">

     
            <apex:outputText rendered="{!opcr.IsPrimary == True}">
                <td><apex:outputField value="{!opcr.ContactId}"/></td>                                                            
            </apex:outputText>
        </apex:repeat>       
    </apex:outputPanel>
<br>

 

tellsapfeltellsapfel

Hi,

 

is there a way around to display the contact name when using Professional Edition without the possibility to create an apex class?

 

thanks for your help