I am pretty new to Visualforce, so I'm sure I'm missing something silly. Essentially I'm trying to create a "wizard" with visualforce pages for our outside sales team. I have created a button on the task object that goes to a VF page with four different buttons: New Contact, New Opportunity, New Task, and New Order.
The New Order button links to an external URL, so that was easy and done. My problem is with the other buttons. If for example I want to create a new opportunity, I'm having issues with the second VF page (for the new opportunity) linking to the original page, because they are different objects. When a user clicks on the New Opportunity button on the original VF page, I would like it to go to a new VF page where it asks the user for the information on the opportunity (the name, stage, close date, etc.) However, when I click on the New Opportunity button, I receive the error "Invalid conversion from runtime type SOBJECT:Opportunity to SOBJECT:Task". I assume this is because the VF page that the user is sent to when they click the New Opportunity button has a standard controller of Opportunity (when the original VF page had a standard controller of Task).
I have tried changing the standard controller on the New Opportunity VF page to Task, however I am then unable to save my VF page because it has input fields for the Opportunity object (and not the Task object). I am linking the specific code I'm talking about below. Any help or advice would be appreciated!
Extension:
public class RouteSalesExtension
{
private ApexPages.StandardController sc;
public Opportunity opp1;
public Task task1 {get; set;}
public RouteSalesExtension(ApexPages.StandardController standardController)
{
this.task1 = (Task)standardController.getRecord();
sc = standardController;
task1 = [select ID, Who.ID, What.ID from Task where Id =: ApexPages.currentPage().getParameters().get('id') limit 1];
}
public PageReference ToOppPage1()
{
return Page.RouteSalesOpp1;
}
Original VF Page:
<apex:page standardController="Task" extensions="RouteSalesExtension" title="Begin Route Sales">
<apex:form >
<apex:pageBlock title="Route Sales Information">
<apex:pageBlockButtons location="top">
<apex:commandButton action="{!SetStartTime}" value="Start Meeting"/>
</apex:pageBlockButtons>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!ToOrderPage1}" value="New Order"/>
<apex:commandButton action="{!ToOppPage1}" value="New Opportunity"/>
<apex:commandButton action="{!ToTaskPage1}" value="New Meeting"/>
<apex:commandButton action="{!ToContactPage1}" value="New Contact"/>
<apex:commandButton action="{!CreateOpportunityforOrder}" value="Create Order"/>
</apex:pageBlockButtons>
<apex:inputField id="Comments" value="{!Task.Description}"
label="Meeting Notes"/>
<apex:outputField value="{!task1.Who.Id}" rendered="false"/>
<apex:outputField value="{!task1.What.Id}" rendered="false"/>
<apex:outputField value="{!task1.Id}" rendered="false"/>
</apex:pageBlock>
</apex:form>
</apex:page>
Page that should go to new opportunity but errors out:
<apex:page standardController="Opportunity" extensions="RouteSalesExtension" title="Route Sales Opp">
<apex:form >
<apex:pageBlock title="Route Sales Information">
<apex:inputField value="{!Opportunity.Name}"
label="Opportunity Name"/>
<apex:inputField value="{!Opportunity.CloseDate}"
label="Close Date"/>
</apex:pageBlock>
</apex:form>
</apex:page>