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
SFDC-NOOBSFDC-NOOB 

Visualforce Records not updating on Save

Hello,

 

I am still very new to APEX.  I am trying to develop a simple visualforce page that lists all the accounts the active user owns.  Ideally, the user should be able to edit and save the records.  When the user clicks save, the record in the database should update.  Currently, the edit and save buttons do not work but I can make changes when double clicking but they do not save.  Any help is greatly appreciated.

 

My code so far:

 

*********************Visualforce page************************

 

<apex:page controller="MyController" >
     
      <apex:form >
     
        <apex:pageBlock title="My Accounts">
       
           
                <apex:pageblocktable value="{!myaccounts}" var="acct">
                    <apex:column value="{! acct.name}"/>
                    <apex:column value="{! acct.Preferred_Mode_S__c}"/>
                    <apex:column value="{! acct.type}"/>
                     <apex:inlineEditSupport event="ondblclick"/>
                  
                </apex:pageblocktable>
                <apex:commandButton value="Save" action="{!save}"/>
            <apex:commandbutton value="Edit" action="{!edit}"/>
        </apex:pageblock>
  </apex:form>
</apex:page>

 

 

*****************************Controller*****************************

 

public class MyController {

         public List<Account> getMyAccounts(){        

List<Account> a = new List<Account>();        

a = [select Id,name,type,preferred_mode_s__c,macro_industry__c,micro_industry__c from Account WHERE (owner.id =: userinfo.getuserid())];            

return a;           

}   

 

public PageReference edit() {        

return null;    

}

    public PageReference save() {        

return null;    

}

}        

Best Answer chosen by Admin (Salesforce Developers) 
Alex.AcostaAlex.Acosta

My personal preference when I have to do something like this is to display each column with an inputfield, and a text output when showing read only.

 

IE:

<apex:page controller="MyController" >
     
      <apex:form id="theForm">
     
        <apex:pageBlock title="My Accounts">
       
           
                <apex:pageblocktable value="{!myaccounts}" var="acct">
                    <apex:column>
                         <apex:facet name="header">{!$ObjectType.Account.fields.Name.label}</apex:facet>
                         <apex:outputField value="{!acct.Name}" rendered="{!!isEditEnabled}" />
                         <apex:inputField value="{! acct.Name}" rendered="{!isEditEnabled}"/>
                    </apex:column>
                  
                </apex:pageblocktable>
                <apex:commandButton value="Save" action="{!save}" rerender="theForm"/>
            <apex:commandbutton value="Edit" action="{!edit}" rerender="theForm"/>
        </apex:pageblock>
  </apex:form>
</apex:page>

 

 

your new class:

public class MyController {
	public List<Account> myAccounts;
        public Boolean isEditEnabled;
	
	public MyController(){
		myAccounts = new List<Account>();
                isEditEnabled = false;
	}

    public List<Account> getMyAccounts(){
		myAccounts = [select Id,name,type,preferred_mode_s__c,macro_industry__c,micro_industry__c from Account WHERE (owner.id =: userinfo.getuserid())];            
		return myAccounts;           
	}   
	 
	public PageReference edit() { 
isEditEnabled = true; return null; } public PageReference save() { update myAccounts;
isEditEnabled = false; return null; } public Boolean getIsEditEnabled(){ return isEditEnabled; } }

 

I also tested this on my org and it worked fine. No idea why records are being reverted... do you have any triggers on account?

All Answers

Alex.AcostaAlex.Acosta
Probably not the best way, but on your save method, if you wish to update all the (account) records you have, do this for your save method:

public PageReference save(){
update a;
return null;
}
SFDC-NOOBSFDC-NOOB
When I do that I get "Error: MyController Complie Error: Variable does not exist: a at line 17 column 16.

Line 17 is "update a;"
Alex.AcostaAlex.Acosta

Sorry I thought you had your variable a declared outside... it should look like this

 

public class MyController {
	public List<Account> myAccounts;
	
	public MyController(){
		myAccounts = new List<Account>();
	}

    public List<Account> getMyAccounts(){
		myAccounts = [select Id,name,type,preferred_mode_s__c,macro_industry__c,micro_industry__c from Account WHERE (owner.id =: userinfo.getuserid())];            
		return myAccounts;           
	}   
	 
	public PageReference edit() {        
		return null;    
	}
	public PageReference save() {  
		update myAccounts;
		return null;    
	}
}        

 

SFDC-NOOBSFDC-NOOB

I'm not throwing an error anymore.  :D   However, the record still does not update when clicking "Save."  It may have something to do with nothing happening when I click "Edit."  In order to edit I have to double click the field I wish to edit.

 

 

***************Controller******************

 public class MyController {
public List<Account> myAccounts;

public MyController(){
myAccounts = new List<Account>();
}

    public List<Account> getMyAccounts(){
myAccounts = [select Id,name,type,preferred_mode_s__c,macro_industry__c,micro_industry__c from Account WHERE (owner.id =: userinfo.getuserid())];           
return myAccounts;          
}  

public PageReference edit() {       
return null;   
}
public PageReference save() { 
update myAccounts;
return null;   
}
}       

 

 

 

**************************VF Page************************

<apex:page controller="MyController" >
     
      <apex:form >
     
        <apex:pageBlock title="My Accounts">
       
           
                <apex:pageblocktable value="{!myaccounts}" var="acct">
                    <apex:column value="{! acct.name}"/>
                    <apex:column value="{! acct.Preferred_Mode_S__c}"/>
                    <apex:column value="{! acct.type}"/>
                     <apex:inlineEditSupport event="ondblclick"/>
                  
                </apex:pageblocktable>
                <apex:commandButton value="Save" action="{!save}"/>
            <apex:commandbutton value="Edit" action="{!edit}"/>
        </apex:pageblock>
  </apex:form>
</apex:page>

Alex.AcostaAlex.Acosta

well currently edit does nothing...

 

but since you have this line inside your vf page...

<apex:inlineEditSupport event="ondblclick"/>

 

You can double click on a field and it will give you an input box. If you change the value on that input box and then hit save, your record should update.

Alex.AcostaAlex.Acosta

also keep note, if you don't have a value you can't use the inline edit functionality... at least not from what i've experienced in the past.

SFDC-NOOBSFDC-NOOB

That's what I thought too but when I save, it reverts back to the previous value.

 

You are correct, I cannot edit a field that is blank.  Not being able to edit the field if there is no value is not ideal.  Some of the fields will not have values and I would like users to be able to add a value if the field is blank.  Do you know of a good alternative to inline editing?

 

Thank you for your help.

Alex.AcostaAlex.Acosta

My personal preference when I have to do something like this is to display each column with an inputfield, and a text output when showing read only.

 

IE:

<apex:page controller="MyController" >
     
      <apex:form id="theForm">
     
        <apex:pageBlock title="My Accounts">
       
           
                <apex:pageblocktable value="{!myaccounts}" var="acct">
                    <apex:column>
                         <apex:facet name="header">{!$ObjectType.Account.fields.Name.label}</apex:facet>
                         <apex:outputField value="{!acct.Name}" rendered="{!!isEditEnabled}" />
                         <apex:inputField value="{! acct.Name}" rendered="{!isEditEnabled}"/>
                    </apex:column>
                  
                </apex:pageblocktable>
                <apex:commandButton value="Save" action="{!save}" rerender="theForm"/>
            <apex:commandbutton value="Edit" action="{!edit}" rerender="theForm"/>
        </apex:pageblock>
  </apex:form>
</apex:page>

 

 

your new class:

public class MyController {
	public List<Account> myAccounts;
        public Boolean isEditEnabled;
	
	public MyController(){
		myAccounts = new List<Account>();
                isEditEnabled = false;
	}

    public List<Account> getMyAccounts(){
		myAccounts = [select Id,name,type,preferred_mode_s__c,macro_industry__c,micro_industry__c from Account WHERE (owner.id =: userinfo.getuserid())];            
		return myAccounts;           
	}   
	 
	public PageReference edit() { 
isEditEnabled = true; return null; } public PageReference save() { update myAccounts;
isEditEnabled = false; return null; } public Boolean getIsEditEnabled(){ return isEditEnabled; } }

 

I also tested this on my org and it worked fine. No idea why records are being reverted... do you have any triggers on account?

This was selected as the best answer
SFDC-NOOBSFDC-NOOB

Alex,

 

Thank you for your time and effort.  With a few minor adjustments to the code you provided, I got exactly what I want.  The records are editable and update on save.

 

Kudos,

Josh

Abhishek PurohitAbhishek Purohit
I need to Update my List of Opportunities please help