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
Avik DattaAvik Datta 

Edit record from VF page with custom controller

Currently I need your expert help to solve one requirement. Hopefully you will help me.
 
Requirement
One VF page which will show a list of User(Manage User) data. And  user will able to update Active field from that list. Means user can uncheck Active field and save so that User will be inactive in database.
 
My Implementation
I have created a custom controller to fetch specific set of user data. Please find below apex code


public class ReportController{
                 public List<User> reportRecord{
                 get{
                            return [SELECT Name,Username,Country_Code_AZ__c,LastLoginDate,Datediffcalc__c,IsActive  FROM User WHERE Datediffcalc__c > 90 and IsActive=true order by Country_Code_AZ__c];   
                 }
                set;
                   }   
      public PageReference QuickSave(){
               return null;   
                 }
}
 Also created a VF page where Active field is editable, please find below page code
 
<apex:page controller="ReportController">
<apex:form >
    <apex:pageBlock title="Users List">
    <apex:commandButton value="Inactive User" action="{!Quicksave}"/>
   
        <apex:pageBlockTable value="{!reportRecord}" var="val">
            <apex:column headerValue="Name" value="{!val.Name}"/>
            <apex:column headerValue="Username" value="{!val.Username}"/>
            <apex:column headerValue="Country Code" value="{!val.Country_Code_AZ__c}"/>
            <apex:column headerValue="LastLoginDate" value="{!val.LastLoginDate}"/>
            <apex:column headerValue="Days Since Last Login" value="{!val.Datediffcalc__c}"/>
            <apex:column headerValue="Active">
                <apex:inputField value="{!val.IsActive }"/>
            </apex:column>
 
        </apex:pageBlockTable>
  </apex:pageBlock>
 
  <apex:commandButton value="Inactive User" action="{Quicksave}"/>
  </apex:form>
</apex:page>
 
 
Problem Statement
Actually I am unable to write edit code under PageReference QuickSave() so that when user unchecked that flag and click on save , the updated value will be saved.
 
Please help me on the above situation or if you have any doc or video which implement same, please forward to me.
Best Answer chosen by Avik Datta
Vinoth Vijaya BaskerVinoth Vijaya Basker
Hi Avik, 

Please find the below code as an example, 
 
<apex:page Controller ="ReportControllerUpdated">
    <apex:form >
    <apex:pageBlock title="Users List">
    <apex:commandButton value="Inactive User" action="{!save}" id="saveButton1"/>
   
        <apex:pageBlockTable value="{!ListofUsers}" var="val">
            <apex:column headerValue="Name" value="{!val.Name}"/>
            <apex:column headerValue="Username" value="{!val.Username}"/>

            <apex:column headerValue="LastLoginDate" value="{!val.LastLoginDate}"/>

            <apex:column headerValue="Active">
                <apex:inputField value="{!val.IsActive}"/>
                
            </apex:column>
 
        </apex:pageBlockTable>
  </apex:pageBlock>
 
  <apex:commandButton value="Inactive User" action="{!save}" id="saveButton2"/>
  </apex:form>
</apex:page>
 
public class ReportControllerUpdated
{
   public List<User> userList = new List<User>();
   public List<User> getListofUsers()
   {
       userList =  [SELECT Name,Username,LastLoginDate,IsActive FROM USER WHERE isActive = True];
       return userList;
   }
   
   public PageReference Save(){
       update userList;
       return null;
   }
   
   
}

and Make changess based on your requirement.


Thanks,
Vinoth 

All Answers

Vinoth Vijaya BaskerVinoth Vijaya Basker
Hi Avik, 

Please find the below code as an example, 
 
<apex:page Controller ="ReportControllerUpdated">
    <apex:form >
    <apex:pageBlock title="Users List">
    <apex:commandButton value="Inactive User" action="{!save}" id="saveButton1"/>
   
        <apex:pageBlockTable value="{!ListofUsers}" var="val">
            <apex:column headerValue="Name" value="{!val.Name}"/>
            <apex:column headerValue="Username" value="{!val.Username}"/>

            <apex:column headerValue="LastLoginDate" value="{!val.LastLoginDate}"/>

            <apex:column headerValue="Active">
                <apex:inputField value="{!val.IsActive}"/>
                
            </apex:column>
 
        </apex:pageBlockTable>
  </apex:pageBlock>
 
  <apex:commandButton value="Inactive User" action="{!save}" id="saveButton2"/>
  </apex:form>
</apex:page>
 
public class ReportControllerUpdated
{
   public List<User> userList = new List<User>();
   public List<User> getListofUsers()
   {
       userList =  [SELECT Name,Username,LastLoginDate,IsActive FROM USER WHERE isActive = True];
       return userList;
   }
   
   public PageReference Save(){
       update userList;
       return null;
   }
   
   
}

and Make changess based on your requirement.


Thanks,
Vinoth 
This was selected as the best answer
Avik DattaAvik Datta
Great, its working now. Can you please tell me about id="saveButton1" used in vf page also id="saveButton2" used in lower section button.
And once we click on the button all userlist will be updated, correct?
Vinoth Vijaya BaskerVinoth Vijaya Basker
If you need to use the ids of two buttons, you can use them. Else, you can remove it.


Thanks,
Vinoth 
Vinoth Vijaya BaskerVinoth Vijaya Basker
Yes. You are right. The userlist will be updated, once we clicked the button.

Thanks,
Vinoth
Avik DattaAvik Datta
So we can also perform multiple record update at a time .. through this code...?
Thanx again for your great help.
Vinoth Vijaya BaskerVinoth Vijaya Basker
Yes. We can update multiple records(In this case, inactive multiple users) at a time through this code. 

You can mark my post as best answer, if it helps you. So it may help others in future.


Thanks,
Vinoth
Avik DattaAvik Datta
Sure thanks.