+ Start a Discussion
ShadowlessKickShadowlessKick 

Custom Object Record Type Selection

Need some ideas as to how to prompt the user for a record type.  This is on a custom object which is being called from the opportunity.  The existing custom button calls a visual force page that does nothing but executes a class.  The class scapes the opporutnity and populates the custom object with a numerous amount of data from the opporutnity.  The Salesforce out of the box recordtypeselect.jsp does exaclty what I need but I do not know how I would force that in front of my existing process.

 

<apex:page standardController="Opportunity" extensions="createProjectFromOpportunity" action="{!autoRun}">   
<apex:sectionHeader title="Access Add Project Page"/>   
<apex:outputPanel > If you see this page, something went wrong. Print this page and send the error to the Help Desk.  
</apex:outputPanel> 
</apex:page>

 

 

Best Answer chosen by Admin (Salesforce Developers) 
GlynAGlynA

I just realized that the VF page uses the standard controller for Opportunity so that it can be used as a custom VF button on the Opportunity detail page.  By converting to an OnClick JS button, the page can now use the standard controller for your custom object (or you can convert your controller extension into a controller and not use a standard controller at all).  The JS code for the button would become:

 

navigateToUrl("setup/ui/recordtypeselect.jsp?ent=MyObject__c&retURL=/{!Opportunity.Id}&save_new_url=%2Fapex%2FMyVFPage%3Foppid={!Opportunity.Id}");

I *think* that your controller will be constructed with the new custom object record (with record type already initialized), and it can get the url parameter 'oppid' with which to query the Opportunity record.  I haven't actually tried this - it's all theoretical.  Let me know if you try it out and if you have any problems making it work.  Thanks!

 

-Glyn

All Answers

GlynAGlynA

@WorkingForYou,

 

I saw that your question has gone unanswered and I thought I might be able to help.  I think the code below will work as an OnClick JavaScript button that navigates to the recordtypeselect.jsp page for your custom object, and then continues on to your VF page that does the real work.  The only thing that confuses me is that your VF page code uses the standard controller for Opportunity and not for your custom object.  But the code will probably do what you want:

 

navigateToUrl("setup/ui/recordtypeselect.jsp?ent=MyObject__c&retURL=/{!Opportunity.Id}&save_new_url=%2Fapex%2FMyVFPage%3Fid={!Opportunity.Id}");

That should be all on one line in the button definition.  You need to replace 'MyObject__c' with the name of your custom object, and 'MyVFPage' with the name of your VF page.  This button will work from an Opportunity detail page.  It should navigate to the record type selection page for the custom object.  If the operation is cancelled, it will return to the detail page of the Opportunity.  If the record type selection is saved, it will navigate to '/apex/MyVFPage?id={!Opportunity.Id}'.

 

Because you are not going to the edit page for the custom object, the new custom object that recordtypeselect.jsp creates (along with the record type selection itself) is lost.

 

Perhaps you could tell me more details about what the record type does, which object it is on, how it affects the overall process, etc.

 

-Glyn

 

 

GlynAGlynA

I just realized that the VF page uses the standard controller for Opportunity so that it can be used as a custom VF button on the Opportunity detail page.  By converting to an OnClick JS button, the page can now use the standard controller for your custom object (or you can convert your controller extension into a controller and not use a standard controller at all).  The JS code for the button would become:

 

navigateToUrl("setup/ui/recordtypeselect.jsp?ent=MyObject__c&retURL=/{!Opportunity.Id}&save_new_url=%2Fapex%2FMyVFPage%3Foppid={!Opportunity.Id}");

I *think* that your controller will be constructed with the new custom object record (with record type already initialized), and it can get the url parameter 'oppid' with which to query the Opportunity record.  I haven't actually tried this - it's all theoretical.  Let me know if you try it out and if you have any problems making it work.  Thanks!

 

-Glyn

This was selected as the best answer
ShadowlessKickShadowlessKick
Had marked this post as answered but some clarification should be made.

The JavaScript OnClick looks like this:

var TheURL = "setup/ui/recordtypeselect.jsp?ent=01I500000007Qqe&save_new_url=%2Fapex%2FcreateProjectFromOpportunity%3Fid={!Opportunity.Id}&retURL=%2F{!Opportunity.id}";
window.open( TheURL,"_blank","menubar=yes, titlebar=yes, status=yes, toolbar=yes, scrollbars=yes, resizable=yes, top=1, left=1,  height=600" );


During the process of using "setup/ui/recordtypeselect.jsp" a visual for page is called with a controller attached to it (createProjectFromOpportunity). The purpose of the controller is to inherit fields to the new object from the object it is related to.  It was found that becasue the recordtypeselect was directed to a visualforce page the process lost the record type selection.  The good thing about it was that the redirection holds the record type id in it's URL.  The parameter is "RecordType" The parameter needs to be captured in the controller on the visual force page.

The visual force page:

<apex:page standardController="Opportunity" extensions="createProjectFromOpportunity" action="{!autoRun}">  
<apex:sectionHeader title="Access Add Project Page"/>  
<apex:outputPanel > You tried calling the createProjectFromOpportunity Apex Controller from a button.  If you see this page, something went wrong. Print this page and send the error to HDNotifier. 
</apex:outputPanel>
</apex:page>


The Controller (Simplified):

public with sharing class createProjectFromOpportunity {

//Define the Project Object
Custom_object__c theProject = new Custom_object__c();
Opportunity theOpportunity = new Opportunity();  
String theProjectID;
String theRecordType;
// Constructor - this only really matters if the autoRun function doesn't work right    
public createProjectFromOpportunity(ApexPages.StandardController stdController) {       
this.theOpportunity = (Opportunity)stdController.getRecord();    
}
// Code invoked on page load.     
public PageReference autoRun()
{          
String theOpportunityId = ApexPages.currentPage().getParameters().get('id');
String theRecordType = ApexPages.currentPage().getParameters().get('RecordType');          
if (theOpportunityId == null)
{            
// Display the Visualforce page's error message if no Id is passed over            
return null;        
}          
for (Opportunity theOpportunity:[select Id from Opportunity where id =:theOpportunityId Limit 1])
{
theProject.Opportunity__c = theOpportunity.id;    
theProject.Start_Date__c = Date.Today();
theProject.RecordTypeId = theRecordType;
Insert theProject;
theProjectID = theProject.id;
}  
       
// Redirect the user to the Project Page  
PageReference pageRef = new PageReference('/' + theProjectId);        
pageRef.setRedirect(true);        
return pageRef;      
}

}

The URL that calls the visual force page: 

https://c99.salesforce.com/apex/createProjectFromOpportunity?id=000111222333444&RecordType=555666777888999&ent=12345678978946