Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a visualforce list page that lists opportunity products and some of their fields. One of these fields is a lookup to a custom object. On each row in the visualforce page, the user is given the option to lookup an existing record of the custom object or to create a new one. If the user chooses to create a new one, they are given a select list of the different record types, they select one and click Create.

Here is the section of code in my visualforce page where the select list is:

<apex:column headerValue="Record Type">
    <apex:selectList value="{!RecordType}" multiselect="false" size="1">
        <apex:selectOptions value="{!rectypes}"/>
    </apex:selectList><br/>                     
</apex:column>

Here is the section of code in the controller that deals with the record types:

public List<SelectOption> getrectypes() {
   List<SelectOption> options = new List<SelectOption>();
   options.add(new SelectOption('','--Select Record Type --'));
   for(RecordType rt:[select id,name from RecordType where sobjecttype='cust_object__c']){
     options.add(new SelectOption(rt.id,rt.name));    
   }
     return options;
   }

public String getRecordType() {
    return recType;
}


public void setRecordType(String recType) {
    this.recType= recType;
}

Here's the problem: I'm having trouble referencing the value of the record type that is selected from the select list. Further down in my page I have the Create link. Once the user clicks create, I need the id of the record type in the controller so that I can redirect the user to the appropriate page layout. Any idea how to get this? Any help is appreciated!

VF page:

<apex:commandLink action="{!onCreate}" value="Create">
</apex:commandLink>

Controller:

public PageReference onCreate(){
    return new PageReference('/a0W/e&RecordType=' + RecTypeID);
}
share|improve this question
2  
Doesn't the recType variable in your code hold that ID? –  Michael Welburn Dec 27 '13 at 14:58
    
I thought that might work too, but when I tried replacing RecTypeID with recType I get this error: Unable to Access Page The value of the "id" parameter contains a character that is not allowed or the value exceeds the maximum allowed length. Remove the character from the parameter value or reduce the value length and resubmit. If the error still persists, report it to our Customer Support team. Provide the URL of the page you were requesting as well as any other related information. –  Katie Swol Dec 27 '13 at 15:07
    
And from what I can see in the URL, it is saying RecordType=null –  Katie Swol Dec 27 '13 at 15:09
1  
You might try putting a debug statement in your set method to make sure it is getting called. Based on your snippet of code, if that is being called correctly, the recType variable should have the correct value. –  Michael Welburn Dec 27 '13 at 15:19
add comment

2 Answers 2

A few things look odd. Instead of using a seprate getter and setter for getRecordType, modify your controller to replace the following:

public String getRecordType() {
    return recType;
}


public void setRecordType(String recType) {
    this.recType= recType;
}

With

public Id recType {get; set;}

Note, I did change the type here to "Id" instead of String. Next, I think {!RecordType} may be a special variable, especially if you are using a standard controller in your visualforce page. So, change the following:

<apex:selectList value="{!RecordType}" multiselect="false" size="1">

to:

<apex:selectList value="{!recType}" multiselect="false" size="1">

Lastly, change your onCreate method to look at recType instead of RecTypeID

public PageReference onCreate(){
    return new PageReference('/a0W/e&RecordType=' + recType);
}

If none of this works, throw some System.debugs in the getter / setters and onCreate method and check your Debug Logs for any errors or indications as to why your code isn't working.

HTH, - DL

share|improve this answer
    
Thanks for the suggestion! I'm a little confused as to where recTypeId comes in to play, should this be recType? –  Katie Swol Dec 27 '13 at 15:37
    
D'oh, sorry I changed it to recType. Good catch. –  James Loghry Dec 27 '13 at 15:38
    
Thanks, unfortunately I'm still getting null for recType. I'm going to give those System.debugs a try. –  Katie Swol Dec 27 '13 at 15:41
add comment
up vote 4 down vote accepted

After a day of pulling my hair out and trying everything, I found an answer. I had to remove multiselect="false" from my selectlist. I'm not sure why this made a difference, but this attribute was not needed since it appears that multiselect defaults to false anyways. Now I am getting the id back instead of null- thanks!

share|improve this answer
2  
Ah, interesting. If set to true, it needs a list of Strings for the value attribute. It must think that the string "false" is actually true. I wonder what would happen if you used multiselect="{!IF(1==1,false,false)}" for instance. Glad you figured it out! –  James Loghry Dec 27 '13 at 20:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.