+ Start a Discussion
djbakerman.ax666djbakerman.ax666 

page redirect based on case record type select list

Hello -

 

I'm trying to write a visualforce page that will redirect a user to the Case Record Type's page layout for a given case record type.  For example, if the case record type is "change" then the page redirects to the "change" case edit page.  I seem to be stuck with the redirect after the submit button is pressed - nothing seems to happen.  Below is the code:

 

public with sharing class RecordTypeListCon {
private List<SelectOption> items;

// property that reads the value from the Component attribute
public string sObjectType
{
get;
set;
}

//public RecordTypeListCon(ApexPages.StandardController controller) {
  //      this.controller = controller;
//}

public List<SelectOption> getItems() {
 List<SelectOption> items = new List<SelectOption>();

 //default value
 items.add(new SelectOption('','--Select Record Type --'));

 //query force.com database to get the record type of the requested object.
 for(RecordType rt: [select id,name from recordtype where sobjecttype=:sObjectType]) {
      items.add(new SelectOption(rt.id,rt.name));
  }
 return items;
}

public System.PageReference action()
{


  PageReference nextPage = new PageReference('/500/e?RecordTypeID='apexpages.currentPage().getParameters(Values(getItems())););

  nextPage.setredirect(true);
  return nextPage;
 
 
}

}

 

Below is the VF page:

 

 <apex:page controller="RecordTypeListCon">
<h1>Please select a case record type:</h1>
<p>
</p>
 <apex:form >
<c:RecordTypeListCon value="{!items}" sObjectType="Case"></c:RecordTypeListCon>
<apex:commandButton value="Go" action="{!action}" rerender="dynamic"/>
</apex:form>
</apex:page>

 

Best Answer chosen by Admin (Salesforce Developers) 
SwarnasankhaSwarnasankha

Could you elaborate a little bit more on the requirement because if I have understood your requirement correctly then what you are trying to achieve using a VF Page is a sales force default behavior which doesn't need any custom coding.

 

With regards to the issue posted by you; on your apex page within the apex form tags, type this:

 

<apex:outputLabel value="Record Type of New Record" />

<apex:selectList id="Case_RT_Selector" value="{!SelectedCaseRecordTypeID}" size="1">

<apex:selectOptions value="{!Items}"/>

</apex:selectList>

 

In your controller, define a getter-setter for SelectedCaseRecordType as follows:

String SelectedCaseRecordTypeID ='';

 

public void setSelectedCaseRecordTypeID(String s)

{

SelectedCaseRecordTypeID = s;

}

public String getSelectedCaseRecordTypeID()

{

return SelectedCaseRecordTypeID ;

 

Rename your button action to something else - for example RTSelector (**it is advisable not to name the custom functions similar to any default SFDC function/attribute**)

 

public Pagereference RTSelector()

{

PageReference nextPage;

nextPage = new PageReference('/500/e?RecordType' + SelectedCaseRecordTypeID);

nextPage.setRedirect(true); return nextPage;

}

 

I hope this helps.

All Answers

SwarnasankhaSwarnasankha

Could you elaborate a little bit more on the requirement because if I have understood your requirement correctly then what you are trying to achieve using a VF Page is a sales force default behavior which doesn't need any custom coding.

 

With regards to the issue posted by you; on your apex page within the apex form tags, type this:

 

<apex:outputLabel value="Record Type of New Record" />

<apex:selectList id="Case_RT_Selector" value="{!SelectedCaseRecordTypeID}" size="1">

<apex:selectOptions value="{!Items}"/>

</apex:selectList>

 

In your controller, define a getter-setter for SelectedCaseRecordType as follows:

String SelectedCaseRecordTypeID ='';

 

public void setSelectedCaseRecordTypeID(String s)

{

SelectedCaseRecordTypeID = s;

}

public String getSelectedCaseRecordTypeID()

{

return SelectedCaseRecordTypeID ;

 

Rename your button action to something else - for example RTSelector (**it is advisable not to name the custom functions similar to any default SFDC function/attribute**)

 

public Pagereference RTSelector()

{

PageReference nextPage;

nextPage = new PageReference('/500/e?RecordType' + SelectedCaseRecordTypeID);

nextPage.setRedirect(true); return nextPage;

}

 

I hope this helps.

This was selected as the best answer
djbakerman.ax666djbakerman.ax666

I created 3 case record types.  When someone goes to the case tab and hits the new case button, I want a page that shows the record types in a select box, and from there go to the proper page layout.

 

Does your code also work for a custom component? My component looks like:

 

<apex:component controller="RecordTypeListCon">

<apex:attribute name="sObjectType" description="" type="String" required="true" default="Case" assignTo="{!sObjectType}"></apex:attribute>

<apex:attribute name="value" description="" type="String" required="true" assignTo="{!value}"></apex:attribute>

<apex:selectList value="{!value}" size="3" id="value">

<apex:selectOptions value="{!items}"></apex:selectOptions>

</apex:selectList>

</apex:component> 

SwarnasankhaSwarnasankha

The code that I gave you does not use the custom component. It will render a picklist on the VF Page with the LOVs retrieved from the controller class named Items. Is there any specific reason why you want to use a component to render the picklist.

 

More importantly, why are you try to recreate a standard function of Sales force using a VF Page?

djbakerman.ax666djbakerman.ax666

There is no reason for the custom component.  I saw someone do that in another forum and thought that was how to do this.

 

I also didn't realize this was a standard function of salesforce.  I didn't know how to do it that way, so I thought it (case record type select) had to be done with a VF page. 

 

Thank you! 

SwarnasankhaSwarnasankha
I hope you have figured out the way of handling your requirement using the standard functionality; if not, let me know and I would be more that glad to help.
djbakerman.ax666djbakerman.ax666

Michael,

 

That was not what I was intending, but similiar.  That page is close, but the link to the new record is not correct.  It would be something like /500/e?RecordType=xxxxxxx, where xxxxxx is the recordtype ID.

 

In order for any of this to work, first, a user's profile must be allowed to access the record type.  This is achieved by going to the profile of the given user or users (if the record type is for multuple profiles), and editing the record type settings. 

 

-Dan