0

I have a table (Table A) which has Id, Sales Reps, and their Goals. I want to keep 2 input fields on the apex page: field 1 for Sales Rep and field 2 for Goals.

When the user inputs a sales rep in field 1 and inputs corresponding goal in field 2 and hits update button (also on page), the goals for that sales rep should get updated in Table A.

How can I achieve this?

Here is what I have done till now:

Controller:

public with sharing class UpdateValues {

public PageReference save() {
    update Sales_Rep;
    return null;
}

public string regn {get; set;}
public string dist {get; set;}    

public list <SelectOption> getValuesr()
{
    list <selectoption> lsreg = new List<selectoption> ();
    lsreg.add(new SelectOption('', '--None--'));
    Set<String> regionSet = new Set<String>();
    for (User_Stats__C Rgn : [SELECT Region__c FROM User_Stats__C where Region__c != null and Region__c != 0 and Region__c != 1 and District__c != null and District__c != '0'])
    {
        String region = string.valueOf(Rgn.Region__c);
        if(!regionSet.contains(region)) lsreg.add(new selectoption(region, region));
        regionSet.add(region);
    }
    return lsreg;
}

public list <SelectOption> getValues()
{
    list <selectoption> ls = new List<selectoption> ();
    ls.add(new SelectOption('', '--None--'));
    Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;
    Set<String> distSet = new Set<String>();
    for (User_Stats__C dist : [SELECT District__c FROM User_Stats__c where Region__c =: regnInt and District__c != null and Region__c != null and Region__c != 1 and Region__c != 0])
    {
        if(!distSet.contains(dist.District__c)) ls.add(new selectoption(dist.District__c, dist.District__c));
        distSet.add(dist.District__c);
    }
    return ls;
}

    public List<User_Stats__c> Sales_Rep{
        get{
                Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;
                Sales_rep=[Select Id, Sales_Rep__c, Rental_Revenue_Goal__c From User_Stats__c where Region__c =: regnInt and District__c =: dist and Sales_Rep__c != null and Rental_Revenue_Goal__c != null];
            return Sales_Rep;
            }
        set;
        }

} VF Page:

<apex:page controller="UpdateValues">
<apex:form >
         <apex:outputLabel value="Region" />
    <apex:selectList value="{!regn}" size="1" id="Region">
        <apex:selectOptions value="{!Valuesr}" />
        <apex:actionSupport event="onchange" reRender="districtSelect"/>
    </apex:SelectList>

    <apex:outputPanel id="districtSelect">
    <apex:outputLabel value="District"/>
    <apex:selectList value="{!dist}" size="1" id="dist">
        <apex:selectOptions value="{!Values}"/>
        <apex:actionSupport event="onchange" reRender="Inputs"/>
    </apex:selectList>
    </apex:outputPanel>

  <apex:pageblock >
   <apex:pageblockTable value="{!Sales_Rep}" var="rep" id="Inputs">
        <apex:column headerValue="Sales Rep" value="{!rep.Sales_Rep__c}">
        </apex:column>
        <apex:column headerValue="Revised_Goal">
            <apex:inputField value="{!rep.Rental_Revenue_Goal__c}"/>
            <apex:commandButton action="{!save}" value="Save"/>
        </apex:column>            
    </apex:pageBlockTable>
   </apex:pageBlock>

</apex:form>

Here is the error: **Error: Could not resolve the entity from value binding '{!Sales_Rep.Sales_Rep__c}'. can only be used with SObjects, or objects that are Visualforce field component resolvable.
**

Thanks in advance.

1
  • 1
    Can you add in what you have tried so far?
    – Prady
    Jan 6, 2014 at 13:01

1 Answer 1

2

For starters your object is a list so you cannot simply use {!Sales_Rep.xxx} notation.

You need to use a pageblock table or a repeat to display the values:

<apex:pageblock>
<apex:pageblockTable value="{!Sales_Rep}" var="rep">
<apex:column headerValue="Sales Rep" >
<apex:inputField value="{!rep.Sales_Rep__c}/>
</apex:column>
<apex:column headerValue="Goal">
<apex:inputField value="{!rep.Goals__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>

Then you need to add a button to save the records .

In your controller you should change the getter for Sales Rep to only perform the query if the Sales_Rep variable is null. that way when you go to save the records and call the getter to get the values it does not overwrite what was entered by performing a new query.

public List<User_Stats__c> Sales_Rep{
get{
if(Sales_rep == null){
Sales_rep=[
    Select Id, Sales_Rep__c From User_Stats__c];
}
return Sales_Rep;

set;
}


public List<User_Stats__c> Sales_Rep{
        get{
        if(Sales_rep == null){ 
       Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;
                Sales_rep=[Select Id, Sales_Rep__c, Rental_Revenue_Goal__c From User_Stats__c where Region__c =: regnInt and District__c =: dist and Sales_Rep__c != null and Rental_Revenue_Goal__c != null];
}
            return Sales_Rep;
            }
        set;
        }
4
  • Thanks a lot Eric. That solved a major road block. I used the same code as you and added Save button. However, on changing the goals and updating does not update the record. It returns the old value any idea why? I have updated my original question to include this. Thanks.
    – Robin
    Jan 7, 2014 at 6:49
  • You will need to override the standard save method. If you have done that also try adding a rerender to the command button.
    – Eric
    Jan 12, 2014 at 21:01
  • I have edited the question with entire code for VF and controller. Can you help me understand why I am not able to save?
    – Robin
    Jan 13, 2014 at 14:47
  • Most likely because when you call update sales rep it fires the "getter" to get the sale rep variable. Since you are not checking if salesrep == null in the getter it performs a new query and gets the old values. wrap the get part of it in if(sales rep == null){} (See update code) then put the return sales rep outside of that if statement. That way if you have modified salesrep it will return the modified version
    – Eric
    Jan 15, 2014 at 4:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .