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
Jason McCarthy 2Jason McCarthy 2 

I am getting Error: Compile Error: Invalid field OpportunityId for SObject Contact at line 35 column 75

I am trying to add  existing contacts to a related list on Opportunities  as "Job Candidates" through a VF page and kicked off by a custom button. I keep getting the compile Error  Error: Compile Error: Invalid field OpportunityId for SObject Contact at line 35 column 75 .  The functionailty is suppose to search contacts (which i should make for specific record type of "participant"  and "Participant Donor" just haven't gotten there yet. 

What am I missing? 

public with sharing class AddContacttoJobOpptController { 
    public Opportunity opp {get;set;}
      public String selectedConId = '';
    public String selectedOppId = '';
    public AddContacttoJobOpptController(){
        opp = new Opportunity();
        selectedOppId = Apexpages.currentPage().getParameters().get('aid');
            
        
    }
    public Pagereference addContact(){
        Set<String> conIdSet = new Set<String>();
        selectedConId = opp.Candidate__c;
        if(selectedConId != null && selectedConId != ''){
            for(Contact con : [select id from Contact where OpportunityId = :selectedOppId]){
                conIdSet.add(con.id);
            }
            if(conIdSet.contains(selectedConId)){
                apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Info,'The selected contact is already added to strategic account plan. Please select another contact to add.');
                apexpages.addmessage(msg);
                return null;
            }
            else{
                Contact con = new Contact(id=selectedConId, OpportunityId=selectedOppId);
                update con;
                apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Info,'The selected contact has been added to strategic account plan. Now you can select another contact to add.');
                apexpages.addmessage(msg);
                return null;
            }
            
        }
        else{
            apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Info,'Please select contact then click on Add Contact button.');
            apexpages.addmessage(msg);
            return null;
        }
        
    }
    public Pagereference goBack(){
        return new Pagereference('/'+selectedOppId);
    }

}
Best Answer chosen by Jason McCarthy 2
Abhishek BansalAbhishek Bansal
Hi Jason,

By default there is no OpportunityId field available on Contact object.
If you have created a custom field of Opportunity on Contact than please use the API name of that custom field where you are trying to use OpportunityId.
Please replace OpportunityId with API name of Opportunity custom field created on Contact.

Let me know if i can provide you more help on this.

Thanks,
Abhishek

All Answers

Amit Chaudhary 8Amit Chaudhary 8
I guess you want to fatch COntact base on accountid.
NOTE:- OpportunityId is invaild field on Contact. Please change Variable name accordingly.
public with sharing class AddContacttoJobOpptController { 
    public Opportunity opp {get;set;}
      public String selectedConId = '';
    public String selectedOppId = '';
    public AddContacttoJobOpptController(){
        opp = new Opportunity();
        selectedOppId = Apexpages.currentPage().getParameters().get('aid');
            
        
    }
    public Pagereference addContact(){
        Set<String> conIdSet = new Set<String>();
        selectedConId = opp.Candidate__c;
        if(selectedConId != null && selectedConId != ''){
            for(Contact con : [select id from Contact where AccountId = :selectedOppId]){
                conIdSet.add(con.id);
            }
            if(conIdSet.contains(selectedConId)){
                apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Info,'The selected contact is already added to strategic account plan. Please select another contact to add.');
                apexpages.addmessage(msg);
                return null;
            }
            else{
                Contact con = new Contact(id=selectedConId, AccountId=selectedOppId);
                update con;
                apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Info,'The selected contact has been added to strategic account plan. Now you can select another contact to add.');
                apexpages.addmessage(msg);
                return null;
            }
            
        }
        else{
            apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Info,'Please select contact then click on Add Contact button.');
            apexpages.addmessage(msg);
            return null;
        }
        
    }
    public Pagereference goBack(){
        return new Pagereference('/'+selectedOppId);
    }

}


 
Abhishek BansalAbhishek Bansal
Hi Jason,

By default there is no OpportunityId field available on Contact object.
If you have created a custom field of Opportunity on Contact than please use the API name of that custom field where you are trying to use OpportunityId.
Please replace OpportunityId with API name of Opportunity custom field created on Contact.

Let me know if i can provide you more help on this.

Thanks,
Abhishek
This was selected as the best answer
Abhishek BansalAbhishek Bansal
Hi Jason,

By default there is no OpportunityId field available on Contact object.
If you have created a custom field of Opportunity on Contact than please use the API name of that custom field where you are trying to use OpportunityId.
Please replace OpportunityId with API name of Opportunity custom field created on Contact.

Let me know if i can provide you more help on this.

Thanks,
Abhishek
Jason McCarthy 2Jason McCarthy 2
Hi Amit, I do not want accountId involved because I have requirements that are flexible, in most cases the participant is not associated to a specific account other than a household, but there are site driven accounts that are in the naming convention of <site>+Individuals and are not linked to an actual account other than for reporting. 

I chose the best answer from Abhishek, because of the requiements if each contact had an actual account attached to it either answer would be the best. 

Thank you both for your time.

Best, 

Jason
Mani Anand 10Mani Anand 10
I have the same error here, please help resolve:
<messaging:emailTemplate subject="Quote Information" recipientType="User" relatedToType="Opportunity">
<messaging:plainTextEmailBody >


</messaging:plainTextEmailBody>
 <messaging:attachment renderAs="pdf">
<head>
<style>
@page {
  size: auto;   /* auto is the initial value */
  margin-top: 0.0%;
  margin-left: 4mm;
  margin-right: 4mm;
  margin-bottom: 4mm;
}
td{
font-family:Arial,Helvetica,sans-serif;
font-weight:normal;
font-size:15px;
}
.tds{
font-family:Arial,Helvetica,sans-serif;
font-weight:normal;
font-size:15px;
}
td.normal{
font-family:Arial,Helvetica,sans-serif;
font-weight:normal;
font-size:15px;
}
td.nbold{
font-family:Arial,Helvetica,sans-serif;
font-weight:bold;
font-size:15px;
}
</style>
</head>
The Discount on the Quote exceeds the maximums alloted and hence requires approval.

Below is the Opportunity information:
Opportunity Name : {!relatedTo.Name}
Opportunity Account : {!relatedTo.Account}
Close Date: {!relatedTo.CloseDate}
Stage: {!relatedTo.StageName}
Amount: {!relatedTo.Amount}
Link: {!relatedTo.Link}
    <tr>
       <td  width = "14%"><b>Quote Number</b></td>
       <td  width = "10%"><b>Quantity</b></td>
       <td  width = "44%"><b>Expiration Date</b></td>
       <td  width = "44%"><b>Discount</b></td>
       
    </tr> 
    <apex:repeat value="{!relatedTo.Quotes__r}" var="line">
    <tr>
      <td>{!line.QuoteNumber}"/></td>
      <td>{!line.Name}"/></td>
      <td>{!line.ExpirationDate}"/></td>
      
   
    </tr>
    </apex:repeat>
    
    <tr><td>&nbsp;</td></tr>        
    <tr><td>&nbsp;</td></tr>
     
        
</messaging:attachment> 

</messaging:emailTemplate>