Search code examples
salesforceapexvisualforce

Need to update Roles(Custom FIeld) of the Employee(Custom) those are Checked


Roles are assigned to each Employee and each employee has a checkbox. Whenever checkbox is checked and Role selected from Dropdown(Roles from Picklist), New Role Should be assigned to Employee after clicking on Update Button. How do i get it.. I've assigned ID to checkbox and trying like if Checkbox==true then perform certain condition but it is not working..Please guide me

VF Page

<apex:page extensions="EmployeeSelectClassController13" standardController="Employee__c" sidebar="false">
    <apex:form >
        <apex:pageBlock >    
            <apex:pageBlockSection title="Employees" collapsible="false">
                <apex:pageBlockTable value="{!wrapEmployeeList}" var="empWrap" id="table" title="All Employees">
                    <apex:column >
                        <apex:inputCheckbox value="{!empWrap.selected}" id="inputCheckbox"/>
                    </apex:column>
                    <apex:column value="{!empWrap.emp.Name}" id="inputName" />
                    <apex:column value="{!empWrap.emp.Role__c}" id="inputRole" />      
                </apex:pageBlockTable>    
                <apex:pageBlockSection id="InfoId" columns="1" >
                    <apex:selectList size="1" value="{!SelectedValue}">
                        <apex:selectOptions value="{!statusOptions}"/>
                    </apex:selectList>
                </apex:pageBlockSection>
                <apex:commandButton value="Update" id="inputButton" action="{!updateRole}" />
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form> 
</apex:page>

Apex Controller

public class EmployeeSelectClassController13 {
    public List<wrapEmployee> wrapEmployeeList {get; set;}
    public List<Employee__c> empRecord {get;set;}
    public string selectedValue { get;set; }
    public List<SelectOption> statusOptions { get;set; }//TO get Roles from Employee

    public EmployeeSelectClassController13(ApexPages.StandardController controller){
        if(wrapEmployeeList == null){
            wrapEmployeeList = new List<wrapEmployee>();
            for(Employee__c empList:[Select Name,Role__c from Employee__c]){
                wrapEmployeeList.add(new wrapEmployee(empList));
                autorun();
            }
        }
    }
    public class wrapEmployee{
        public Employee__c emp {get; set;}
        public Boolean selected {get; set;}

        public wrapEmployee(Employee__c e) {
            emp = e;
            selected = false;
        }
    }

    //To get all Roles(PickList) from Employee Object
    public void autoRun()
    {

        Schema.DescribeFieldResult statusFieldDescription = Employee__c.Role__c.getDescribe();
        statusOptions = new list<SelectOption>();

        for (Schema.Picklistentry picklistEntry : statusFieldDescription.getPicklistValues())
        {
            statusOptions.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel()));
        }
    }
    public void updateRole()
    {

    }

}

Solution

  • Something like this? You don't need IDs in the Visualforce unless you do some JavaScript magic. This can be done completely in Apex.

    public void updateRole(){
        List<Employee__c> toUpdate = new List<Employee__c>();
        for(wrapEmployee wrap : wrapEmployeeList){
            if(wrap.selected){
                wrap.emp.Role__c = SelectedValue;
                // wrap.selected = false; // you could choose to untick checkboxes now?
                // or after successful update loop through them one more time and then untick
                toUpdate.add(wrap.emp);
            }
        }
        update toUpdate;
    }