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
sp13sp13 

update record in visualforce page table

how can i update a record in my visualforce page table?
my table is like this:
Name        UnitPrice(inputfields)            Link
Fuel1                  50.00                        Update Price
Fuel2                  60.00                        Update Price

this is my vf page:
<apex:page showHeader="false" sidebar="false" standardController="Fuel__c" extensions="FuelUpdateCX">
    <apex:form >
        <apex:messages />
        <apex:pageBlock >
            <apex:pageBlockTable value="{!Fuel}" var="f">
                <apex:column value="{!f.Name}"/>
                <apex:column headerValue="Unit Price">
                    <apex:inputField value="{!f.Unit_Price__c}"/>
                </apex:column>
                <apex:column width="10%">
                    <apex:commandLink action="{!updatePrice}" style="padding:1px 2px; font-weight:bold;">Update Price</apex:commandLink>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

and this is the class:
public with sharing class FuelUpdateCX {
    public FuelUpdateCX(ApexPages.StandardController controller) {}
    public List<Fuel__c> getFuel() {
            return[select id, Name, Unit_Price__c from Fuel__c ORDER BY Name];
    }
    public pageReference updatePrice() {
        //update fuel here
        return new PageReference('/apex/FuelUpdate');
    }
}
should i use wrapper? how can i update the price here?
Elie.RodrigueElie.Rodrigue
Add an Id parameter to your controller : public String SelectedFuelId {get;set;}

Then use the <apex:param tag

<apex:commandLink action="{!updatePrice}" style="padding:1px 2px; font-weight:bold;">
<apex:param name="SelectedFuelId"
                value="{!f.Id}"
                assignTo="{!SelectedFuelId}"/>
Update Price</apex:commandLink>

Then update price is called, SelectedFuelId will be populated with the id of the fuel object you click the link of.
sp13sp13
hi Elie. i tried it but didn't work. btw, i have a problem in the updatePrice method. the line update _getFuel or SelectedFuelId_. should i call the getFuel in this line? tnx for the help :)
Elie.RodrigueElie.Rodrigue
You need to do another query that will do where Id=:SelectedFuelId

* You could replace SelectedFuelId by the complete object also to avoid having to query it again.
sp13sp13
can you please check my updatePrice method? this is the updatePrice method:
public PageReference updatePrice() {
        Fuel__c newFuel = new Fuel__c();
        newFuel = [select id, name, Unit_Price__c from Fuel__c where id=:SelectedFuelId];
        update newFuel;
        return new PageReference('/apex/FuelUpdate');
    }
what do you think the problem is?
Elie.RodrigueElie.Rodrigue
You are not updating any value of the object.  You would need to do newFuel.Unit_Price__c = something before calling the update dml operation.