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
dmotedmote 

Referencing Contact in Custom Controller

Ok...here is my problem.  I want to have a user select a contact related to an account from a Select List.  My understanding is that the selected option is returned as a string, which in this case is the contact ID.  How, in the controller do I then reference the contact.  I know how to reference the contact in the Apex Page, but I want to be able to see the contact information on my next Apex Page and save the contact to a new opportunity that I am creating next.

 

 

public class testController {
   public Account getAccount() {
        return [select id, Name, Phone from Account
        where id = :ApexPages.currentPage().getParameters().get('id')];
    }
    
   Contact contact;
    
    public String selectedContact {get;set;}
    
    public List<SelectOption> contactList;
    
    public List<SelectOption> getContact () {
        if (contactList == null) {
            List<Contact> contactee = [select id, name,  contact.accountid from Contact where contact.accountid = :ApexPages.currentPage().getParameters().get('id')];
            
        contactList = new List<SelectOption>();
        for (Contact c : contactee) {
           contactList.add(new SelectOption(c.id,c.name));
        }
    }
    return contactList;
    }

  public PageReference step2() {
     string acctid = ApexPages.currentPage().getParameters().get('id');
     PageReference newPage = new pageReference('/apex/newQuote2?id=' + acctid);
     return newPage.setRedirect(true);

   }
   


}

 

 

Thanks for any and all assistance.

 

David

_Prasu__Prasu_

could you post your VFPage code too?

dmotedmote

ok...here is the page

 

 

<apex:page controller="testController" tabStyle="Opportunity">
  <apex:pageMessages id="msgs"/>
  <script>
  function confirmCancel() {
      var isCancel = confirm("Are you sure you wish to cancel?");
      if (isCancel) return true;
  
     return false;
  }  
  </script>

  <apex:sectionHeader title="New Customer Quick Quote" subtitle="Step 1 of 3"/>
    <apex:form >
      <apex:pageBlock title="Customer Information" mode="edit">
              <apex:pageBlockButtons >
          <apex:commandButton action="{!step2}" value="Next"/>
          <apex:commandButton action="{!cancel}" value="Cancel" 
                              onclick="return confirmCancel()" immediate="true"/>
        </apex:pageBlockButtons>

      <apex:pageBlockSection title="Account Information">
                        
        <!-- Within a pageBlockSection, inputFields always display with their
             corresponding output label. -->  
              You are viewing the {!account.name} account. <p/>
      </apex:pageBlockSection>
      <apex:pageBlockSection title="Contacts">
         Please Select a Contact.<p/>
         <apex:selectList value="{!selectedContact}" multiselect="false">
            <apex:selectOptions value="{!contact}"/>
         </apex:selectList>
         </apex:pageBlockSection>
         
    </apex:pageBlock>      

 
  </apex:form>
</apex:page>

 

 

dmotedmote

Am I not making sense here?  Can someone please help me.  I simply want to select a contact from a contact list and be able to assign an opportunity to the contact.

 

This is really frustrating.

 

Thanks,

David

_Prasu__Prasu_

 

you will get the contact id in the "selectedcontact" variable. You will need to update the opportunity record  with that selectedcontact assign to opportunity's contact lookup field.