1

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>
1

Revising my answer as I missed this detail

You have two pages for different standradControllers referring to the same extension class:

   <apex:page standardController="Task" extensions="RouteSalesExtension" title="Begin Route Sales">

and

<apex:page standardController="Opportunity" extensions="RouteSalesExtension" title="Route Sales Opp">

The error you are getting is on this line:

this.task1 = (Task)standardController.getRecord();

because when the Opportunity page is displayed, you are casting an Opportunity SObject to a Task

So, how to get around this:

Options

  1. Use separate controllerExtensions, one per SObject
  2. Use a custom controller (not an extension) that can handle multiple pages such as in a wizard metaphor. Note that this may run into viewstate issues if you have lots of data across all possible pages. SFDC VF doc has a good example of a wizard flow via a custom controller
  3. Modify your constructor to examine the ID of the result of getRecord() and cast to the appropriate SObject (Task, Opportunity,...) see here for a somewhat related issue. You would use the ID method getSobjectType()

Which option to choose is a subject for debate.

Option 1 would tend to be used if you expect to add Sobject-specific behavior over time and want to kepp this all in one class per Sobject

Option 2 would tend to be used when you go back and forth between different pages across some logical transaction.

Option 3 would tend to be used when you are providing common functionality for many SObjects across many VF pages

Of course, there are other patterns and the above is not comprehensive (more based on my experience)

Original answer

Your controller method

public PageReference ToOppPage1()
    {
        return Page.RouteSalesOpp1;
    }

should look like:

public PageReference ToOppPage1()
    {
        PageReference resPg = Page.RouteSalesOpp1;
        resPg.setRedirect(true);
        resPg.getParameters().put('someparm','someparmval');  // if needed
        return resPg;
    }

All that said, you can avoid a lot of this by using the URLFOR function on the commandButton as in :

<apex:commandButton 
     action="{!URLFOR($Page.routeSalesOppo1,$ObjectType.Opportunity,[retURL=$Currentpage.URL])}" 
     value="New Opportunity"/>
| improve this answer | |
  • crop1645, thank you. I have tried your suggestions, however I am still getting the same error. – grimsvotn Nov 3 '14 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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