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
jvandevejvandeve 

VisualForce page and custom controller work but don't save values to database

Hi all,

 

I'm a Salesforce admin and not a developer, so please be gentle! My situation is as follows. I want a specific profile to be able to modify some specific fields on users, but without giving them access to setup!

 

Created a VFpage using a custom controller. This is the custom controller:

 

 

public class alfabet {

    public PageReference quicksave() {
        return null;
    }

   
  


     
    private List<userSet> userSetList{get;set;}
    private string userListQuery;
    private set<user> selectedUser;
    
    public List<string> alphabet{get;set;}
    public boolean fals{get;set;}   
    
    public alfabet(){
      fals=false;
      alphabet=new string[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'All' };  
      userSetList = new List<userSet>();
      selectedUser = new set<user>();
      userListQuery= 'select id,name,Regio__c,Regio_User__c,Profile.Name,FTE__c,Scorecard_Activity__c,Scorecard_Sales_Category__c,Scorecard_Sector_Region__c from user where IsActive=True limit 1000';    
    }
    
     Private void updateSelectedUser(){
        for(userSet cs:userSetList ){
           if(cs.checked)
               selecteduser.add(cs.ouser);
           else{
               if(selecteduser.contains(cs.ouser))
                   selecteduser.remove(cs.ouser);
               }
       }   
    } 
    
      public ApexPages.StandardSetController standardSetCon {
        get {
            if(standardSetCon == null) {
                standardSetCon = new ApexPages.StandardSetController(Database.getQueryLocator(userListQuery));
                // sets the number of records in each page set
                standardSetCon .setPageSize(25);
            }
            return standardSetCon ;
        }
        set;
    }
 
    public List<userSet> getCurrentList() {
       updateSelectedUser(); 
       userSetList = new List<userSet>();       
        for (user category : (List<user>)standardSetCon.getRecords()){        
            if(selectedUser.contains(category))          
            userSetList.add(new userSet(category,'true'));
            else
            userSetList.add(new userSet(category));
        }
        fals=false;
        return userSetList;
    } 
    
    public PageReference refreshList2() {       
       standardSetCon = null;     
       string s;
       if(apexpages.currentpage().getparameters().get('alpha') == 'All')
           s='%';
       else
           s= apexpages.currentpage().getparameters().get('alpha')+'%';
       
       userListQuery= 'select id,name,Regio__c,Regio_User__c,Profile.Name,FTE__c,Scorecard_Activity__c,Scorecard_Sales_Category__c,Scorecard_Sector_Region__c from user where IsActive=true AND name like' +'\''+s +'\''+ ' limit 5000';           
        return null;
    }
    
   
    
    public List<user> getDisplaySelectedList(){
        updateSelectedUser();
        List<user> displaycon = new list<user>();
        displaycon.addall(selecteduser);
        return displaycon;
    }
       
    public class userSet { 
        public Boolean checked{ get; set; }
        public user ouser { get; set;} 

        public userSet(){
            ouser = new user();
            checked = false;
        } 
        public userSet(user c){
            ouser = c;
            checked = false;

        } 
        public userSet(user c,string s){
            ouser = c;
            checked = true;

        } 
    }
     
  
}

 

The VF page shows filterlinks above the page a b c d e f g.... to show users beginning with that letter. The records are preceded by checkboxes. When a check a couple of users and I click a button "Display Selected", the selected users are shown in a second block with a couple of updatable picklistfields from the user object. When I change the values of the different entries and picklists and then hit "Update Users", I would like the fields on the userrecord to be updated, but it doen't update them. And I've been searching for hours now and can't find the solution.

 

This is the code of the Visual Force page:

<apex:page Controller="alfabet" tabstyle="User">
<style type="text/css">
      .loadingIcon {
            background-image: url(/img/loading.gif);
            width: 16px;
            height: 16px;
        }
     </style>
<script type="text/javascript">
function checkAll(cb,cbid)
        {
            var inputElem = document.getElementsByTagName("input");                      
            for(var i=0; i<inputElem.length; i++)
            {   
                
                 if(inputElem[i].id.indexOf(cbid)!=-1){                                         
                inputElem[i].checked = cb.checked;
                }
            }
        }
</script>
<apex:form id="form">
<br/>
<div style="align:right;">
<apex:repeat value="{!alphabet}" var="a">
<apex:commandLink value="{!a}" action="{!refreshList2}" rerender="form" style="{!if($CurrentPage.parameters.alpha=a,'font-weight:bold','')}" status="stat">
<apex:param name="alpha" value="{!a}"/>
</apex:commandLink>
&nbsp;|&nbsp;
</apex:repeat>
</div>
<br/>
<apex:pageBlock id="block">
<apex:pageBlockButtons >
<apex:commandButton rendered="{!standardsetcon.hasprevious}" value="Previous" action="{!standardsetcon.previous}" rerender="block,block2" status="stat"/>
<apex:commandButton rendered="{!standardsetcon.hasnext}" value="Next" action="{!standardsetcon.next}" rerender="block,block2" status="stat"/>
</apex:pageBlockButtons>
<apex:actionStatus id="stat">
<apex:facet name="start"> 
<apex:outputPanel layout="block" styleClass="message infoM4">
<apex:panelGrid columns="2" styleClass="messageTable" columnClasses="messageCell" style="padding:0px;margin:0px;">
<apex:panelGroup >
<img class="loadingIcon" src="/s.gif"/>
</apex:panelGroup>
<apex:panelGroup >
<div class="messageText">Please wait...</div>
</apex:panelGroup>
</apex:panelGrid>
</apex:outputPanel>
</apex:facet>
<apex:facet name="stop">
<apex:pageBlockTable value="{!CurrentList}" var="c" id="table">
<apex:column >
<apex:facet name="header"><apex:inputcheckbox onclick="checkAll(this,'check')" value="{!fals}" /></apex:facet>
<apex:inputcheckbox value="{!c.checked}" id="check">
</apex:inputcheckbox>
</apex:column>
<apex:column value="{!c.ouser.name}" headerValue="Name"/>
<apex:column value="{!c.ouser.Profile.Name}" headerValue="Profile"/>
<apex:column value="{!c.ouser.Regio__c}" headerValue="Region"/>
<apex:column value="{!c.ouser.Regio_User__c}" headerValue="Region User"/>
</apex:pageBlockTable>
</apex:facet>
</apex:actionStatus>
</apex:pageBlock>
<apex:pageBlock title="Selected Users" id="block2">
<apex:commandButton value="Display Selected" rerender="block2"/>
<apex:pageBlockTable value="{!DisplaySelectedList}" var="c">
<apex:column value="{!c.name}" headerValue="Name"/>
<apex:column headerValue="FTE">
            <apex:inputField value="{!c.FTE__c}">
        </apex:inputfield>
        </apex:column>
<apex:column headerValue="Activity">
            <apex:inputField value="{!c.Scorecard_Activity__c}">
        </apex:inputfield>
        </apex:column>
<apex:column headerValue="Sales Category">
            <apex:inputField value="{!c.Scorecard_Sales_Category__c}">
        </apex:inputfield>
        </apex:column>
<apex:column headerValue="Sector / Region">
            <apex:inputField value="{!c.Scorecard_Sector_Region__c}">
        </apex:inputfield>
        </apex:column>
</apex:pageBlockTable>
<apex:commandButton action="{!quicksave}" value="Update Users" />
</apex:pageBlock>
</apex:form>  
</apex:page>

 

Till here everything works fine and it does exactly what it has to do, but it doesn't write the changes back to the database! HELP

 

I would really appreciate someone to write down the exact code (I guess in the controller) to be adapted or added to get the button to save those values in their respective records.

 

kindly appreciated!

Sonam_SFDCSonam_SFDC

After going through your code I understand that it is the

getDisplaySelectedList() function which is storing the value of the updated users and the list of users it is returning(displaycon) are the updated ones.

 

So the quicksave function should be as follows:

 

public PageReference quicksave() {
List<user> updateduserlist =getDisplaySelectedList();
update updateduserlist ;
return null;
}

 

Pls try and let me know if this updates DB.

jvandevejvandeve
Hi Sonam,

I Added the code and now when I click the button I get the following error:
System.ListException: Duplicate id in list: 00520000001EdgkAAC
Error is in expression '{!quicksave}' in component <apex:commandButton> in page scorecard_new

Class.Pagin_alpha.quicksave: line 5, column 1
Sonam_SFDCSonam_SFDC

In the following method:

 

public List<user> getDisplaySelectedList(){
updateSelectedUser();
List<user> displaycon = new list<user>(); < remove this line and INITIALIZE outside the loop
displaycon.addall(selecteduser);
return displaycon;
}

 

LIKE :

List<user> displaycon = new list<user>();

public List<user> getDisplaySelectedList(){
updateSelectedUser();
displaycon.addall(selecteduser);
return displaycon;
}

 

and in the quicksave method put:

 

public PageReference quicksave() {
update displaycon;
return null;
}

jvandevejvandeve
Hi Sonam,

so i changed the quicksave to:

public PageReference quicksave() {
update displaycon;
return null;
}

and changed the methoded like you said to:

List<user> displaycon = new list<user>();
public List<user> getDisplaySelectedList(){
updateSelectedUser();
displaycon.addall(selecteduser);
return displaycon;
}


Yet I still receive the following message when clicking the "Update Users" button:

System.ListException: Duplicate id in list: 00520000001EdgkAAC
Error is in expression '{!quicksave}' in component <apex:commandButton> in page scorecard_new

Class.Pagin_alpha.quicksave: line 4, column 1
Sonam_SFDCSonam_SFDC

Hi,

 

If you don't mind, could you please send me a private message with the following details so I can take a closer look at the code:

1)Sandbox ORG ID

2)Username

3)Login access to Salesforce support

 

jvandevejvandeve
Hi Sonam,

PM has been sent with the required details. Please have a look how you can help me.
I really appreciate your actions.

Thanks in advance.