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
pdostapdosta 

Assign Lookup Value based on Custom VF Object Picklist

I have a need to create a Picklist based on a Custom Object, then based on the selection in the Picklist, update a Lookup Relationship field (Lookup__c).  I have the Picklist code and the Visualforce page, but I cannot figure out how to update the Lookup field.  The requirement is to update the Lookup field with the Id of the selection of the Picklist.  Below are my code, can anyone help?

 

Class

 

public class enrollSave{
    
    String mpi;

    public String getCoa() { return this.mpi; }

    public void setCoa(String s) { this.mpi = s; }
    
        public List<SelectOption> getCoas() {
          List<SelectOption> optionList = new List<SelectOption>();
          optionList.add(new SelectOption('','- None -'));
        
          for (Custom_Object_A__c coa : [SELECT Name FROM Custom_Object_A__c ORDER BY Name]){
            optionList.add(new SelectOption(coa.id,coa.Name));
          }
          return optionList;     
        }
    
    ApexPages.StandardController controller;
    public enrollSave(ApexPages.StandardController con){
        controller = con;
    }            
 
    public PageReference save() {
        controller.save();
        return page.aeskenrollconfirm;
    }
}

 Visualforce

 

<apex:page showHeader="false" standardController="Custom_Object_A__c" extensions="enrollSave" standardStylesheets="false">
        <apex:form >
            <apex:pageBlock >
                    <apex:pageblockSectionItem >
                        <apex:outputLabel value="Select Option" for="options"/>
                            <apex:selectList value="{!coa}" size="1" id="options">
                            <apex:selectOptions value="{!coas}"/>
                            </apex:selectList>
                    </apex:pageblockSectionItem>
                    <apex:pageblockSectionItem >
                            <apex:inputField value="{!Lookup__c}" size="1" id="relationship" rendered="false">
                    </apex:pageblockSectionItem>
                </apex:pageBlockSection>
            <apex:pageblockButtons location="bottom">
                <apex:commandButton action="{!Save}" value="Submit Enrollment" />
            </apex:pageblockButtons>
            </apex:pageBlock>
        </apex:form>
</apex:page>

 

 

 

ahab1372ahab1372

can you just include the Lookup field in the VF page? Why the "detour" via a picklist?

 

Also, if you do not include the Lookup field in the VF page, t will not be included in the standardController query, which means the field will not be available for update. You would have to query it in your extension

SteveBowerSteveBower

 

 

1. You don't have a handle to the instance of the Custom_Object_A__c record that the page is dealing with. 

 

So if you define, in the constructor, a location Custom_Object_A__c instance:

 

Custom_Object_A__c myObj = (Custom_Object_A__c)controller.getRecord();

 

then you will have a handle on that record an you can set:

 

myObj.Lookup__c to the id you get back.

 

 

Then the control.save() will save the myObj record with the new Lookup__c value.

 

 

Hope it helps, Best, Steve.

 

p.s. A Tip, in your getcoas code, first check to see if your OptionList is empty before you rebuild it.  It doesn't look like you're trying to do anything dynamic in the query, so there's no reason to rebuild it every time you go back to the page.

 

p.p.s.  Instead of having a pageBlockSection on your page with non-rendered content (which might mess up your formatting), you could just do:

<apex:outputPanel rendered="false">

{!Lookup__c}

</apex:outputPanel>

pdostapdosta

 

I tried the following, but I am not getting the result.  Where do I set the Lookup field?
public class enrollSave{
    
    String mpi;

    public String getCoa() { return this.mpi; }

    public void setCoa(String s) { this.mpi = s; }
   
 public SafeKey_Enrollment__c skEnroll = (SafeKey_Enrollment__c)controller.getRecord();

        public List<SelectOption> getCoas() {
          List<SelectOption> optionList = new List<SelectOption>();
          optionList.add(new SelectOption('','- None -'));
        
          for (Custom_Object_A__c coa : [SELECT Name FROM Custom_Object_A__c ORDER BY Name]){
            optionList.add(new SelectOption(coa.id,coa.Name));
          }
          return optionList;     
        }
    
    ApexPages.StandardController controller;
    public enrollSave(ApexPages.StandardController con){
        controller = con;
    }            
 
    public PageReference save() {
        myObj.Lookup = coa.id
        controller.save();
        return page.aeskenrollconfirm;
    }
}

 

 

pdostapdosta

Also, I am creating a new record and trying to set the Lookup by using the Picklist.  I do not want the User to use the Lookup as I only want certain records available.

SteveBowerSteveBower

Well, I would change this line:

 

 public SafeKey_Enrollment__c skEnroll = (SafeKey_Enrollment__c)controller.getRecord();

 

into

 

public SafeKey_Enrollment__c skEnroll;

 

and add

 

skEnroll = (SafeKey_Enrollment__c)controller.getRecord();

 

after the  controller=con; line in the constructor.

 

 

 

Then change this line:

myObj.Lookup = coa.id;

 

to

 

skEnrol.Lookup__c = coa.id;

 

(Although I don't see where you're defining coa, perhaps you mean " ... = getCoa()", or " .... = mpi" ?)

pdostapdosta

I totally agree with your recommendations, but the item I cannot seem to get to work is where the controller knows which selection the User has picked and then populating the Id of that selection to the Lookup field.  I've looked all over and cannot find any examples of this.  Forgive me for all the questions...as you can tell, I'm a beginner.

SteveBowerSteveBower

Well, you have this code:

 

    String mpi;

public String getCoa() { return this.mpi; }

public void setCoa(String s) { this.mpi = s; }

 

 

which defines a String, and the public getters and setters.

 

I might instead just do:  

 

public String coa {get; set;}

 

and you have the Value parameter on the SelectList tag set as:

 

<apex:selectlist value="{!coa}">

 

 

So, when the user selects one of the selectOptions below, the Value of the selected one will be placed by the controller via the setcoa() call.

 

Note that your controller isn't going to see that until you trigger some action which brings you back to the controller. 

 

I suggest you become familiar with the System.debug() call and use the Debug Logs or the System Log to start looking at what your controller is doing.

 

For example, what are you getting in the "mpi" variable now when the user hits save.

 

You should also throw try/catch blocks around your code in case something you're doing throws an exception.

 

Best, Steve.