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 

Having trouble selecting existing contacts to add them to specific opportunities.

Having trouble with selecting existing contacts to add to an opportunity in a seperate Related List labled "Candidates" . I can get the contacts and select them. It just doesnt add them to the opportunity. Im sure this is something simple that I am missing.

Here's the class

public with sharing class AddContacttoJobOpptController { 
    public Opportunity opp {get;set;}
    public Contact con {get;set;}
    public String selectedConId = '';
    public String selectedOppId = '';

    String selectedOpportunityID = null;

    public AddContacttoJobOpptController(){

   
   
        opp = new Opportunity();
           selectedOppId = Apexpages.currentPage().getParameters().get('aid');
            
        
    }
    public Pagereference addContact(){  
        Set<String> conIdSet = new Set<String>();
        selectedConId = Con.Candidate__c ; 
        selectedOppId = Opp.Id ; 
       
        if(selectedConId != null && selectedConId != ''){
            for(Contact con : [select id from Contact where Candidate__c = :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 the Job Opportunity. Please select another contact to add.');
                apexpages.addmessage(msg);
                return null;
            }
            else{
                Contact con = new Contact(id=selectedConId);
                update con;
                apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Info,'The selected contact has been added to the Job Opportunity. 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);
    }

}

and VisualForce Page: 
<apex:page controller="AddContacttoJobOpptController">
    <apex:form >
        <apex:pageBlock title="Search Existing Contact to Add">
            <apex:pageMessages />
            <apex:pageBlockButtons >
                <apex:commandButton value="Add Contact" action="{!addContact}"/>
                <apex:commandButton value="Go Back" action="{!goBack}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">                
                <apex:inputField label="Search Contact" value="{!Opp.JobOpportunity_Lookup__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
ProlayProlay
It should be INSERT con, not UPDATE con.