1

I've created a simple VFP that uses an apex:repeat to render a list of custom objects. These objects are displayed as a preview image with a quantity field. This is going to be used to allow people to order any number of each of the items from the same page. These items are simple promotional items so think of things like pens, notebooks, water bottles, etc.

The controller for the VFP creates a List property to populate the repeat. This part works fine. However the command button I added doesn't appear to save the records.

What am I doing wrong here? It seems like it should be pretty straightforward since I already have a handle on the list of objects that were rendered.

The controller code:

public class Promo_Controller {
public List<Promotional_Item__c> promoItems{
        get { return promoItems;} set { promoItems = value;}
}
public Promo_Controller(){
    this.promoItems = new List<Promotional_Item__c>();
    this.promoItems = [Select Name, Quantity__c, Description__c, Image__c, Type__c FROM Promotional_Item__c ORDER BY Type__c];
}
public void submitOrder(){
    update this.promoItems;
}

And the VFP:

<apex:form styleClass="body" id="promoItemForm">
    <apex:repeat value="{!promoItems}" var="curItem" id="rptPromoItems" >
        <apex:image styleClass="preview {!curItem.Type__c}" url="{!curItem.Image__c}"/>
        <apex:outputText styleClass="titleBox" value="{!curItem.Description__c}" id="desc"/>
        <apex:inputText styleClass="quantity" value="{!curItem.Quantity__c}" id="count" size="1"/> 
    </apex:repeat>

<apex:commandButton id="submit" action="{!submitOrder}" value="Order"/>
</apex:form>

EDIT: removed the immediate="true" as suggested.

  • 1
    Remove the immediate="true" and try saving the records. – battery.cord Jun 13 '16 at 20:13
  • 1
    No form parameters are sent to the controller when using immediate=true. As battery said, remove that and try it. – Antonio Manente Jun 13 '16 at 20:15
  • Removed it, same problem. I'm checking the logs and I can see the original SOQL request happening to populate the repeat. However I don't see anything suggesting the submitOrder() function ever fires, much less executes the update statement. – torpy Jun 13 '16 at 20:30
  • Is this actual code snippet? Because the constructor Promo_Controller name is different than class name Listrak_UPS_Promo_Controller . – Saroj Bera Jun 14 '16 at 7:20
  • @SarojBera Yes, just failed attempt at sanitizing for public consumption. – torpy Jun 14 '16 at 12:43
2

You have a couple of issues here

public void submitOrder(){
   update this.promoItems;
}

is void and does no redirect. Hence <apex:commandButton id="submit" action="{!submitOrder}" value="Order"/> without rerender="promoItemForm" will not refresh the page.

If the DML statement update this.promotItems could potentially (via a trigger/workflow) change other values you want to display on the page, then you need to refetch the list of objects on each ajax refresh.

If this were the case, you'd want to change your controller to look like this:

public class Listrak_UPS_Promo_Controller {
  public List<Promotional_Item__c> promoItems{
        get { this.promoItems = [Select Name, Quantity__c, Description__c, Image__c, Type__c 
                       FROM Promotional_Item__c ORDER BY Type__c];
              return this.promoItems;} 
        set;}
  }
  public Promo_Controller(){
  }
  public void submitOrder(){
    update this.promoItems;
  }

you still need the rerender on the commandButton with the above.

| improve this answer | |
  • What I'm not understanding here is what is keeping the update from firing?It seems to me that keeping the setter from doing anything should let the submitOrder() to happen as expected but even with your changes no records are updated. – torpy Jun 14 '16 at 12:39
  • 1
    @torpy - made a small change above – cropredy Jun 14 '16 at 13:56
0

Surprisingly enough the answer here is much simpler than expected. I blame my faulty understanding of the the VFP/Controller relationship.

In short the only thing that appears to be necessary is to have the property that contains the List of objects declared in the Controller and to have the function the commandButton calls run the update DML.

Nothing else appears to be necessary to do in the accessor functions as long as the constructor creates the list you need.

Many thanks for the comments.

| improve this answer | |

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.