function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mayank Deshpande 22Mayank Deshpande 22 

Not able to save the record in visualforce page , kindly suggest whats wrong on code

Apex Code =  ublic with sharing class Saveaccountdetail {
Public Account Account {get;set;}
Public Account Accts {get;set;}
Public Saveaccountdetail (){
Id id= ApexPages.currentPage().getParameters().get('id');
Accts = (id == null)? new Account() : [SELECT Name, Phone, Industry FROM Account WHERE Id = :id];
}
Public PageReference Save()
{try  
{
insert (Accts);
}
catch(System.DMLException e) {
ApexPages.addMessages(e);
return null;
}
PageReference redirectSuccess = new ApexPages.StandardController(Account).view();
return (redirectSuccess);
}
}

Visual Force Page 
<apex:page controller="Saveaccountdetail" tabStyle="Account" > <apex:form > <apex:pageBlock title="New Account"> <apex:pageBlockSection > <apex:inputField value="{!Account.name}" /> <apex:inputField value="{!Account.Industry}" /> <apex:inputField value="{!Account.Phone}" /> </apex:pageBlockSection> <apex:commandButton value="Save" action="{!Save}"/> </apex:pageBlock> </apex:form> </apex:page>
Best Answer chosen by Mayank Deshpande 22
sfdcMonkey.comsfdcMonkey.com
Hi mayank, update your code as below code 
<apex:page controller="Saveaccountdetail" tabStyle="Account" >
    <apex:form >
        <apex:pageBlock title="New Account">
            <apex:pageBlockSection >
                <apex:inputField value="{!Accts.name}" />
                <apex:inputField value="{!Accts.Industry}" />
                <apex:inputField value="{!Accts.Phone}" /> 
            </apex:pageBlockSection>
            <apex:commandButton value="Save" action="{!Save}"/> 
        </apex:pageBlock>
    </apex:form> 
</apex:page>
* use Accts instead of account in inputFields
controller :
public with sharing class Saveaccountdetail {
    Public Account Account {get;set;}
    Public Account Accts {get;set;}
    Public Saveaccountdetail (){
        Id id= ApexPages.currentPage().getParameters().get('id');
        Accts = (id == null)? new Account() : [SELECT Name, Phone, Industry FROM Account WHERE Id = :id];
            }
    Public PageReference Save(){
        try{
        insert (Accts);
        }
        catch(System.DMLException e) {
         ApexPages.addMessages(e);
         return null;
        }
     
        PageReference redirectSuccess = new ApexPages.StandardController(Accts).view();
         return (redirectSuccess);
    }
}
* use Accts in PageReference redirectSuccess

i hope it helps you.
      Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
    thanks 
http://sfdcmonkey.com  (http://sfdcmonkey.com )

All Answers

sfdcMonkey.comsfdcMonkey.com
Hi mayank, update your code as below code 
<apex:page controller="Saveaccountdetail" tabStyle="Account" >
    <apex:form >
        <apex:pageBlock title="New Account">
            <apex:pageBlockSection >
                <apex:inputField value="{!Accts.name}" />
                <apex:inputField value="{!Accts.Industry}" />
                <apex:inputField value="{!Accts.Phone}" /> 
            </apex:pageBlockSection>
            <apex:commandButton value="Save" action="{!Save}"/> 
        </apex:pageBlock>
    </apex:form> 
</apex:page>
* use Accts instead of account in inputFields
controller :
public with sharing class Saveaccountdetail {
    Public Account Account {get;set;}
    Public Account Accts {get;set;}
    Public Saveaccountdetail (){
        Id id= ApexPages.currentPage().getParameters().get('id');
        Accts = (id == null)? new Account() : [SELECT Name, Phone, Industry FROM Account WHERE Id = :id];
            }
    Public PageReference Save(){
        try{
        insert (Accts);
        }
        catch(System.DMLException e) {
         ApexPages.addMessages(e);
         return null;
        }
     
        PageReference redirectSuccess = new ApexPages.StandardController(Accts).view();
         return (redirectSuccess);
    }
}
* use Accts in PageReference redirectSuccess

i hope it helps you.
      Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
    thanks 
http://sfdcmonkey.com  (http://sfdcmonkey.com )
This was selected as the best answer
v varaprasadv varaprasad
Hi Mayank,

Please check code :
 
public with sharing class Saveaccountdetail {
Public Account Account{get;set;}

Public Saveaccountdetail (){
 Account = new Account();
 system.debug('==Account=='+Account);
}

Public PageReference Save(){
 system.debug('==Account=='+Account);
  if(Account != null)
    insert Account;
    PageReference redirectSuccess = new ApexPages.StandardController(Account).view();
   return redirectSuccess;
  }
}

Hope this helps you.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
PulaK_PrabhakaRPulaK_PrabhakaR
Hi,
  You have taken "Accts" as getter setter variable in class but used standard "Account" as a variable on the page that's why you are not able to save your record. So please modify your code as follows:

      Class:
  public with sharing class Saveaccountdetail {
    Public Account Account {get;set;}
    Public Account Accts {get;set;}
    Public Saveaccountdetail (){
        Id id= ApexPages.currentPage().getParameters().get('id');
        Accts = (id == null)? new Account() : [SELECT Name, Phone, Industry FROM Account WHERE Id = :id];
    }
    Public PageReference Save() {
        try {
            insert (Accts);
        }
        catch(System.DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        PageReference redirectSuccess = new PageReference('/' + Accts.Id);
        return (redirectSuccess);
    }
}


     Page:
<apex:page controller="Saveaccountdetail" tabStyle="Account" > 
    <apex:form > 
        <apex:pageBlock title="New Account"> 
            <apex:pageBlockSection > 
                <apex:inputField value="{!Accts.name}" /> 
                <apex:inputField value="{!Accts.Industry}" /> 
                <apex:inputField value="{!Accts.Phone}" /> 
            </apex:pageBlockSection> 
            <apex:commandButton value="Save" action="{!Save}"/>
        </apex:pageBlock> 
    </apex:form>
</apex:page>

Please mark the answer as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

 
Mayank Deshpande 22Mayank Deshpande 22
thanks to all ...

now code is working fine