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 

New Error: Invalid field initializer: con.Candidate_Opportunity__c at line 43 column 61

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: Invalid field initializer: con.Candidate_Opportunity__c at line 43 column 61.  I have created a field on the Contact object "Candidate_Opportunity__c" and named the variable. 

What am I missing? 

public with sharing class AddContacttoJobOpptController { 
    public Opportunity opp {get;set;}
    public Contact con {get;set;}
    public String selectedConId = '';
    public String selectedOppId = '';
    String selectedOpportunityID = con.Candidate_Opportunity__c;
    public AddContacttoJobOpptController(){
   
   
        opp = new Opportunity();
 
        selectedOppId = Apexpages.currentPage().getParameters().get('aid');
            
        
    }
    public Pagereference addContact(){
        Set<String> conIdSet = new Set<String>();
        selectedConId = opp.Candidate__c;
        selectedOppId = opportunity.Id;
        selectedOpportunityID = con.Candidate_Opportunity__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, con.Candidate_Opportunity__c = 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);
    }

}
Ajay K DubediAjay K Dubedi
Hi,

Why are you using con.fieldname?
if the custom field is created on  contacts then you can assign the value to it by simply Fieldaname__c='variable';
try just giving the fieldname.

Thanks.