Here I am trying to insert a Record. I have 2 input Field on the Form.when I click on the save Button,it just calling a JavaScript function:js(). This javaScript function take form inputField values, then check type is Prospect or not.If Type is Prospect then, Js function calling controller class method "insertRec" through apex actionFunction Tag.Now this insertRec method is a type of pageReference. This method will do 2 things. First, it inserts a record and while inserting if any errors occur then it add the Error message. second after insertion is complete it will redirect to the detail page of the newly created record.

Where i am going wrong with this code?

Below is my code:

VisualForce code

<apex:page id="Pg" controller="ActionFunctionController">
<script type="text/javascript" >
  function js(){
      var aname= document.getElementById("{!$component.FM.PB.PBS.actName}").value;
      var aType = document.getElementById("{!$component.FM.PB.PBS.actType}").value;

        alert('You must Enter Prospect, to get your record save in DB');

       alert('New Record is going to insert');
      /*Below Method is generated by "apex:actionFunction" which will call controller class Method "insertRec" */
<apex:form id="FM">
  <apex:actionFunction name="insertAccount" action="{!insertRec}" reRender="PB"/>
  <apex:pageBlock id="PB">
     <apex:pageMessages />
         <apex:pageBlockSection id="PBS">
         <apex:inputField value="{!acct.Name}" id="actName"/>
         <apex:inputField value="{!acct.Type}" id="actType"/>
    <apex:pageBlockButtons >
    <apex:commandButton value="save" onclick="js();"/> 

Apex code

public class ActionFunctionController {
  // Custom Controller
  public Account acct{get;set;}
  PageReference page;

 public ActionFunctionController(){
    Account acct = new Account();

 public PageReference insertRec(){
  // Logic
  // Move to any mentioned page

      insert acct;
  //Add Error mesaage to Apexpage class
    Apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.ERROR,'Insert failed'); 

  if(acct.id !=null){
    // Send the user to the detail page for the new account.
    PageReference page = new PageReference('/'+acct.id);
 return page;
  • At which line do you get this error? Nov 22, 2016 at 13:39
  • 2
    Your code can't compile. You have a duplicate variable: acct. You declare it at the third line and in your constructor: "Account acct = new Account();". You need to replace the previous line by: "this.acct = new Account();" Nov 22, 2016 at 13:49

1 Answer 1


This code defines a local variable whose scope is solely within the constructor that is set to reference a new Account object:

public Account acct{get;set;}
public ActionFunctionController(){
    Account acct = new Account();

The code shadows (hides) the class property of the same name (acct) so that its value is left as null resulting in a NullPointerException.

As Martin commented, the fix is to change the code from a declaration of a variable (that starts with the variable's type) to a reference to the field:

public Account acct{get;set;}
public ActionFunctionController(){
    acct = new Account();

In this particular case this.acct or just acct work equally well.

  • Thank you, Keith C and Martin Lezer for finding and solving issues on my code. Now according to my code, after clicking save button the record will insert and it will redirect to the detail page. Instead of this, it just inserts the record but it is not redirecting to its detail page. Any ideas? Nov 23, 2016 at 6:46
  • @Architapatnaik Similar problem: you are setting a local variable but returning the class property. Return the local variable inside the if straight after you set its value.
    – Keith C
    Nov 23, 2016 at 9:32

You must log in to answer this question.

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