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
Flint LockwoodFlint Lockwood 

Cannot Update Record Using StandardController Save Method

Hi, 

 

I am trying to update an Account's name using the StandardController save method. For some reason the update is not happening. Here is my code: 

 

public with sharing class UpdateAccountClass{

	public ApexPages.StandardController s;
	public Account a {get; Set;}
	
	public UpdateAccountClass(ApexPages.StandardController stdCon){
		s = stdCon;
		a = (Account)stdCon.getRecord();
		a = [select id, Name from Account where Id =: a.Id];
	}
	
	public PageReference SaveRecord(){
		a.Name = 'Testing Testing';
		PageReference p = null;
		try{	
			p = s.save();
		}catch(Exception e){
			system.debug(e.getMessage());
		}
		return p;
	}
}

 

Thank you in advance. 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
You set A to a new copy of the account (by virtue of running a query). Only the record from stdCon.getRecord() will be updated by the standard controller. Remove that line of code and your page will work as you expect. Also, there is no need to use a try-catch block, as StandardController.save() will not throw an error on failure to save (it returns a null page reference instead).

All Answers

sfdcfoxsfdcfox
You set A to a new copy of the account (by virtue of running a query). Only the record from stdCon.getRecord() will be updated by the standard controller. Remove that line of code and your page will work as you expect. Also, there is no need to use a try-catch block, as StandardController.save() will not throw an error on failure to save (it returns a null page reference instead).
This was selected as the best answer
sambasamba
public UpdateAccountClass(ApexPages.StandardController stdCon){
     a = [select id, Name from Account where Id =: stdCon.getId()];
}

 

Thanks,

Samba

Puja_mfsiPuja_mfsi

Hi,

Please change your code as below:

 

public with sharing class UpdateAccountClass{

      public ApexPages.StandardController s;
      public Account a {get; Set;}

      public UpdateAccountClass(ApexPages.StandardController stdCon){
             s = stdCon;

             a = (Account)s.getRecord();
       }

       public PageReference SaveRecord(){
               PageReference p = null;
               a.Name = 'Testing Testing';
                p= s.save();
                return p;
        }
}

 

Please let me know if u have any problem on same and if this post helps u plz give KUDOS by click on star at left.

Flint LockwoodFlint Lockwood

 


sfdcfox wrote:
You set A to a new copy of the account (by virtue of running a query). Only the record from stdCon.getRecord() will be updated by the standard controller. Remove that line of code and your page will work as you expect. Also, there is no need to use a try-catch block, as StandardController.save() will not throw an error on failure to save (it returns a null page reference instead).

Thank you. This worked