+ Start a Discussion
Prallavi DuaPrallavi Dua 

Action function error

Hi,
I am trying to insert the record using Action Function,
I am getting the below error:

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Account Number should be Numeric.: []
Error is in expression '{!Saverec}' in page action_function_demo: Class.ActionFunctionCLS.Saverec: line 11, column 1
Class.ActionFunctionCLS.Saverec: line 11, column 1

Below is my Code:
<apex:page controller="ActionFunctionCLS" id="pg" >
  <script>
   function recSave(){
    var accountType = document.getElementById('pg:fm:pb:pbs:actType').value;
   // alert('accountType -->'+accountType);
    if(accountType != 'Prospect'){
     alert("You Should Select Prospect to Save the Record");
     return false;
    }
    else{
     saveAccount(); //this is the function name which calls our action function from java Script.
     return true;
    }
   }
 
  </script>

 <apex:form id="fm">
  <apex:actionfunction name="saveAccount" action="{!Saverec}" />
   <apex:pageBlock id="pb">
     <apex:pagemessages ></apex:pagemessages>
     <apex:pageblockButtons >
      <apex:commandButton value="Save" onclick="recSave(); return false;" />    
      
      
     </apex:pageblockButtons>
    
     <apex:pageblockSection id="pbs">
       <apex:inputField value="{!actobj.Name}" id="actName"/>
       <apex:inputField value="{!actobj.type}" id="actType"/>
     </apex:pageblockSection>
   </apex:pageBlock>
 </apex:form>
</apex:page>
---------------------------------------------
public class ActionFunctionCLS {
  public Account actObj{get;set;}
   PageReference prf= null;
    public ActionFunctionCLS(){
      actObj = new Account();
    }
 
   
    public pagereference Saverec(){
   if(actobj.Name !=''){
    insert actobj;
   
   }
   else{
     ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please Enter Name.');
     ApexPages.addMessage(myMsg);
   //return prf;
   }
   if(actobj.id !=null){
 
      // Send the user to the detail page for the new account.
      prf = new PageReference('/'+actobj.id);
      prf.setRedirect(true);
   
   }
   return prf;
    
}
}
Can anyone help me to solve this error.
Thanks in avance
Best Answer chosen by Prallavi Dua
BALAJI CHBALAJI CH
Hi Prallavi,

There is a Validation Rule on your Account object which is causing this exception. 
You need to either change your Validation Rule or provide a value for Account Number to insert the Account.

You can try modify your class like below as an example:
public class ActionFunctionCLS {
  public Account actObj{get;set;}
   PageReference prf= null;
    public ActionFunctionCLS(){
      actObj = new Account();
    }
 
   
    public pagereference Saverec(){
   if(actobj.Name !=''){
     actobj.AccountNumber = '5'; //This line Added
    insert actobj;
   
   }
   else{
     ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please Enter Name.');
     ApexPages.addMessage(myMsg);
   //return prf;
   }
   if(actobj.id !=null){
 
      // Send the user to the detail page for the new account.
      prf = new PageReference('/'+actobj.id);
      prf.setRedirect(true);
   
   }
   return prf;
    
}
}
else, you can provide the validation rule which causing the exception to be modified.

Let us know if that helps you.

Best Regards,
BALAJI

 

All Answers

BALAJI CHBALAJI CH
Hi Prallavi,

There is a Validation Rule on your Account object which is causing this exception. 
You need to either change your Validation Rule or provide a value for Account Number to insert the Account.

You can try modify your class like below as an example:
public class ActionFunctionCLS {
  public Account actObj{get;set;}
   PageReference prf= null;
    public ActionFunctionCLS(){
      actObj = new Account();
    }
 
   
    public pagereference Saverec(){
   if(actobj.Name !=''){
     actobj.AccountNumber = '5'; //This line Added
    insert actobj;
   
   }
   else{
     ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please Enter Name.');
     ApexPages.addMessage(myMsg);
   //return prf;
   }
   if(actobj.id !=null){
 
      // Send the user to the detail page for the new account.
      prf = new PageReference('/'+actobj.id);
      prf.setRedirect(true);
   
   }
   return prf;
    
}
}
else, you can provide the validation rule which causing the exception to be modified.

Let us know if that helps you.

Best Regards,
BALAJI

 
This was selected as the best answer
Prallavi DuaPrallavi Dua
Thanks Balaji it was helpfull