0

I have the code below. Everything works until it comes to update the the values inside the custom object. I have looked through the debug logs and the required record is also being selected and updated. What isn't happening is the record isnt being updated with the number I require. Attached below is the code.

Apex Class

public class ProductByVan{

// The action function parameter gets set here - this is the input
public String neededID{get; set;}


// The output is set here so it can be re-rendered into the page
public List<VAN__c> outSku {get; set;}

   public PageReference retrieve() {
   // TODO why is this only returning on and anarbitrary one at that?
   // Pass in the skey via a bind variable
   List<VAN__c> skus = [
           select Item_Code__c, VANType__c, ItemDescription__c, Quantity__c
           from VAN__c
           where VANType__c = 'ALBAIK'
           ];
   // Pass out the value
   outSku = skus;
   return null;
}
public void setValue() {

  System.debug('ID: '+neededID);

  List<VAN__c> updateQ = [SELECT Item_Code__c, Quantity__c from VAN__c where Item_Code__c LIKE:neededID AND VANType__c LIKE:'ALBAIK'];
  System.debug(updateQ.size());
  System.debug(updateQ.get(0));
  update  updateQ.get(0);
  }}

The Visualforce page section

<apex:page standardStylesheets="false"  sidebar="false"    
Controller="ProductByVan">
<apex:sectionHeader title="MenaBev Online Sales Portal">
</apex:sectionHeader>
<apex:image url="{!$Resource.logo}" style="position:relative;left:70%"/>
<html> 
......
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable id="VAN_TABLE" value="{!outSku}" var="VAN" style="table">
<apex:column >
<apex:facet name="header">VAN Type</apex:facet>
<apex:outputText value="{!VAN.VANType__c}"/>
</apex:column>
<apex:column >
<apex:facet name="header">Item Code</apex:facet>
<apex:outputText value="{!VAN.Item_Code__c}"/>
</apex:column>
<apex:column >
<apex:facet name="header">Item Description</apex:facet>
<apex:outputText value="{!VAN.ItemDescription__c}"/>
</apex:column>
<apex:column >
<apex:facet name="header">Quantity Needed</apex:facet>
<apex:inputField id="newQ" value="{!VAN.Quantity__c}">
<apex:actionSupport event="onchange" reRender="VAN_TABLE" status="loading" action="{!setValue}">
     <apex:param name="singleID" value="{!VAN.Item_Code__c}" assignto="{!neededID}"/>
                        </apex:actionSupport>
 </apex:inputField>

1

Well your setValue() method isn't really. If I delete the debugs from it, it looks like this:

public void setValue() {
  List<VAN__c> updateQ = [SELECT Item_Code__c, Quantity__c from VAN__c where Item_Code__c LIKE:neededID AND VANType__c LIKE:'ALBAIK'];
  update  updateQ.get(0);
}

Now, if you look at it, you are:

  1. Retrieving some records from Database
  2. Updating the first record of the list you retrieved without having applied no logic whatsoever.

In other words, if you want to update some certain value of the updateQ list, then you have to actually change the value, which you are not doing.

Now, in order to update the VAN__c record with the changed value in the visualforce, you need to get that record from the controller, that is from the variable outSku. The fastest way would be this:

public void setValue() {
    update outSku;
}

But you could consider also looking into the standardListController since it might solve you more issues.

Furthermore, and out of the scope of the question, you can optimize your retrieve() method to look like this:

public PageReference retrieve() {
   outSku = [
        SELECT Item_Code__c, VANType__c, ItemDescription__c, Quantity__c 
        FROM VAN__c 
        WHERE VANType__c = 'ALBAIK'
    ];

   return null;
}
| improve this answer | |
  • How do I get the value from the inputfield into the apex class for the update – R83nLK82 Jul 5 '18 at 13:12
  • @R83nLK82 I edited my answer to include it. – Javier García Manzano Jul 5 '18 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.