0

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;

      if(aType!='Prospect'){
        alert('You must Enter Prospect, to get your record save in DB');

      }
      else{
       alert('New Record is going to insert');
      /*Below Method is generated by "apex:actionFunction" which will call controller class Method "insertRec" */
       insertAccount();
      }
  }
</script>
<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:pageBlockSection>
    <apex:pageBlockButtons >
    <apex:commandButton value="save" onclick="js();"/> 
    </apex:pageBlockButtons>
  </apex:pageBlock>
</apex:form>
</apex:page>

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

   if(acct.Name!=''){
      insert acct;
   }
   else{
  //Add Error mesaage to Apexpage class
    Apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.ERROR,'Insert failed'); 
    ApexPages.addMessage(msg);
   }

  if(acct.id !=null){
    // Send the user to the detail page for the new account.
    PageReference page = new PageReference('/'+acct.id);
    page.setRedirect(true);
  }
 return page;
 }
}
2
  • At which line do you get this error? Commented 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();" Commented Nov 22, 2016 at 13:49

1 Answer 1

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.

2
  • 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? Commented 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
    Commented 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 .