2

I have created a vf page with Contact standard controller to create a Contact record.

User will create record in vf page and should be redirected to the related Contact detail page created. Here is the vf page sample code:

<apex:page standardController="Contact" extensions="onboardform">
    <apex:form id="form">
        <apex:outputPanel id="EntryForm">
            <table>
                <tr>
                    <td>
                        <b>First Name:</b>
                        <apex:inputField value="{!con.FirstName}" style="width:225px;" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>Last Name:</b>
                        <apex:inputText value="{!con.LastName}" style="width:225px;" required="true" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <apex:commandButton value="Submit" action="{!submit}" />
                    </td>
                </tr>
            </table>
            </div>
            </div>
        </apex:outputPanel>
    </apex:form>
</apex:page>

Controller Code:

public class onboardform {
    public Contact con {get;set;}
    String conId;
    String stdcon;
    public onboardform(ApexPages.StandardController stdcon) {
        conId = ApexPages.CurrentPage().getParameters().get('Id');
        if (conId != null && conId != '') // if id is in url means record is for edit 
            con = (Contact) stdcon.getRecord();
        else // else you r gonna create new record
            con = new Contact();
    }
    public PageReference submit() {
        if (conId != null && conId != '')
            update con;
        else {
            Contact newContact = new Contact();
            newContact.FirstName = con.FirstName;
            newContact.LastName = con.LastName;
            newContact.HUB_ID__c = con.HUB_ID__c;
            newContact.Approver_Requestor__c = con.Approver_Requestor__c;
            insert newContact;
            conId = ApexPages.CurrentPage().getParameters().get('Id');
            PageReference requestPage = new pagereference(+newContact.id);
            return requestPage;
        }
        return null;
    }
}

When I am entering the details in the vf page , the contact is getting created but not redirected to the related contact detail standard salesforce page and getting error msg

Formula Expression is required on the action attributes

Any suggestions where I am going wrong?

1
  • 2
    You didn't even need a controller for this page...
    – sfdcfox
    Commented Jul 10, 2016 at 21:10

3 Answers 3

0

Your pagereference method has an issue not returning right parameters .

The modified code is as below

 public PageReference submit()
     {
       if(conId != null && conId!='')
       update con;
        else
        {
        Contact newContact = new Contact();
        newContact.FirstName = con.FirstName;
        newContact.LastName = con.LastName;
        insert newContact;
        conId = newContact.id;

        } 
       PageReference requestPage = new pagereference('/'+conId); //Observe the change here .The Id needs to be populated 
        return requestPage;  
     }
7
  • 1
    Thanks Mohit!! I tried this earlier...unfortunaltely I missed the '/' before +newContact.id :)
    – MaxPyne
    Commented Jul 10, 2016 at 21:09
  • See valuable comment from sfdcfox that you do not need a controller if its just inserting contact record . Commented Jul 10, 2016 at 21:12
  • @MohithShrivastava, his code will not work on update Commented Jul 10, 2016 at 21:17
  • @SantanuBoral I am not sure what asker wants on update :) .But if asker wants to refresh ,I agree he needs to remove null and add a pagereference instead Commented Jul 10, 2016 at 21:19
  • 1
    @MohithShrivastava, thats true. Thats why I have tried to make it workable for insert and update. Commented Jul 10, 2016 at 21:21
3

Don't use an extension unless you need to. You can reduce your entire page to just:

<apex:page standardController="Contact">
    <apex:form>
        <apex:pageBlock>
            <apex:pageMessages />
            <apex:pageBlockButtons>
                <apex:commandButton action="{!save}" value="Save" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection>
                <apex:inputField value="{!Contact.FirstName}" />
                <apex:inputField value="{!Contact.LastName}" />
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

This automatically handles inserts and updates, will display error messages if something goes wrong (e.g. a required field is not set), and will redirect to the contact record after saving. Feel free to change the style if you need to.

1

You can change the code and return the PageReference as follows:

public class onboardform
{
    public Contact con{get; set;}
    String conId;
    String stdcon;
    public onboardform(ApexPages.StandardController stdcon) {

     //conId=ApexPages.CurrentPage().getParameters().get('Id'); 
     conId = stdcon.getId();
     if(conId != null && conId!='') 
     {                            // if id is in url means record is for edit 
         con= [SELECT FirstName, LastName, HUB_ID__c,Approver_Requestor__c  FROM Contact WHERE Id=:conId];
     }
      else                                                      // else you r gonna create new record
      con=new Contact();

    }
 public PageReference submit()
     {
       if(conId != null && conId!='')
       {
          update con; 
       }
        else
        {
            Contact newContact = new Contact();
            newContact.FirstName = con.FirstName;
            newContact.LastName = con.LastName;
            newContact.HUB_ID__c = con.HUB_ID__c;
            newContact.Approver_Requestor__c = con.Approver_Requestor__c;
            insert newContact;
            conId = newContact.Id;
        } 
       PageReference requestPage = new ApexPages.StandardController(new Contact(Id=conId)).view();
        return requestPage;
     }
}
4
  • I am getting the error after the changing the page reference as suggested!!
    – MaxPyne
    Commented Jul 10, 2016 at 21:06
  • I have tested and modified your code, there was a problem during update also which I have fixed. Commented Jul 10, 2016 at 21:08
  • Thanks Santanu for pointing out the update issue.Really appreciate your help:)
    – MaxPyne
    Commented Jul 10, 2016 at 21:15
  • Atleast put some mark, fixed your code properly to work both insert and update Commented Jul 10, 2016 at 21:16

You must log in to answer this question.

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