2

I have a custom controller that has been working for years (literally), and for tens of pages. Basically, the page displays a form with a number of lead fields, and when the user clicks a command button that has an action of the save method in my custom controller, it saves the lead, creates a campaign member, and then redirects the user to a page defined in the visualforce page.

The way we create the vf pages, is usually to find one with similar fields, clone, make the necessary changes, and save.

Today I took a form that was created last week and working well. I cloned and created two more pages, but neither of these pages are acting as expected. When a user (me for now) clicks the submit button, the page just reloads with the same information filled out, and no objects are created.

Controller Code:

public lead_WebFormInsert() {
    lead =  new Lead();
    cm = new CampaignMember();
}
....

public PageReference save() {
    try {
        insert(lead);
        System.debug( '\r\nJust created lead: ' + lead.id + '\r\n');
        //Now if set, create campaign member record.
        cm.setOptions(dmo);
        cm.LeadId = lead.Id;
        cm.Status = '1';
        try {
            insert(cm);
        }
        catch(Exception e){
            //exception code here
        }
        PageReference secondPage = new PageReference(
                ApexPages.currentPage().getParameters().get('retUrl') );
        secondPage.setRedirect(true);
        System.debug( ' about to redirect to: ' +
                ApexPages.currentPage().getParameters().get('retUrl'));
        return secondPage;
    }
    catch(System.DMLException e) {
        //notify admin code here         
        ...
        PageReference secondPage = new PageReference('/apex/InsertFail?m=' + e  );
        secondPage.setRedirect(true);
        return secondPage;
    }
}

VisualForce Code:

<apex:form onsubmit="return doCheckForm()">
    <div style='color:red;'>All items marked with a star are required information</div>
        <apex:outputLabel value="*First Name" for="FirstName"></apex:outputLabel>
        <apex:inputField required="false" value="{!Lead.FirstName}" id="FirstName"/>
        <br/><br/>

        <apex:outputLabel value="*Last Name" for="LastName"></apex:outputLabel>
        <apex:inputField required="false" value="{!Lead.LastName}" id="LastName"/>
        <br/><br/>

        <apex:outputLabel value="*Home Phone" for="HomePhone"></apex:outputLabel>
        <apex:inputField required="false" value="{!Lead.Home_Phone__c}" id="HomePhone"/>
        <br/><br/>

    ...

        <apex:commandButton action="{!save}" value="Submit"/>
.....

Does anyone have any suggestions?

1
  • Welcome to SFSE, hgolov.
    – Mike Chale
    Jul 14, 2013 at 19:04

2 Answers 2

2

I have had similar problems many times in the past. It would be worth looking into whether there is any page-level validation "silently" failing which would prevent the save method in your controller being hit:

  1. Add an <apex:pageMessages/> somewhere in your page just in case there are some hidden page messages that may be relevant.
  2. Add something like "System.assert(false)" in the first line of your save method in the controller. If this method is successfully being hit then you will get a Visualforce exception when you click the save button which will help you determine whether this method is running or not.
  3. Use the developer console to generate a debug log for when you click the save button. Search the log for tokens such as "exception", "error" etc.
1
  • Thank you! Adding a page message did the trick - I was attempting to set a date field in javascript with the value {!Now}, but it was the wrong format.
    – hgolov
    Jul 15, 2013 at 6:04
0

You've only shown us part of your Visualforce page, but I don't see where you've set the retUrl parameter your controller is expecting (see the below excerpt of your code):

PageReference secondPage = new PageReference(ApexPages.currentPage().getParameters().get('retUrl') );
     secondPage.setRedirect(true);
     System.debug( ' about to redirect to: ' +ApexPages.currentPage().getParameters().get('retUrl'));
     return secondPage;
1
  • Sorry, I left that part out. But I had debugged with a javascript alert and seen that it was set correctly. I also debugged that in the controller and saw that.
    – hgolov
    Jul 15, 2013 at 6:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .