1

I have been struggling with this for a while now and need some help. Below I have table of orders being displayed using <apex:repeat>. By doing this I can create a table for each order I want to display.

The problem I am having is in the <apex:form> section. I have two fields "Teacher" and "Teacher Wage" that I want the user to fill in and then save. For the teacher field this is a list of values for them to select and for teacher wage it is input text field. Whenever I click Save I the values selected and inputted are not being past over to the controller.

I have added debug logs to my code and can see the only value I can take across to the controller is the order Id.

Any ideas?

Visualforce Code:

<apex:pageBlock title="Order Dan Testing" >
        <apex:repeat value="{!aorders}" var="o" rows="100">
            <table id="classes" class="ordertable">
                <tr id="{!o.OrderNumber}" class="tableheaderRow" >
                    <th rowspan="2"><button class="tbutton" value="{!o.OrderNumber}">+</button></th>
                    <th> Class Id </th>
                    <th> Account Name </th>
                    <th> Programme </th>
                    <th> Programme Code </th>
                    <th> Class Start Date </th>
                    <th> Class End Date </th>
                    <th> Class Amount </th>
                    <th> Status </th>
                    <th> Teacher </th>
                    <th> </th>
                    <th> Teacher Wage </th>
                    <th> </th>
                    <th> </th>
                </tr> 

                <tr class="tabledataRow" >   
                    <td>  <apex:outputLink value="/{!o.id}">{!o.OrderNumber}</apex:outputLink> </td>
                    <td> <apex:outputField value="{!o.AccountId}"/>  </td>
                    <td> <apex:outputField value="{!o.Programme__r.Name}"/> </td>
                    <td> <apex:outputField value="{!o.Programme_Code__c}"/> </td>
                    <td> <apex:outputField value="{!o.EffectiveDate}"/> </td>
                    <td> <apex:outputField value="{!o.EndDate}"/> </td>
                    <td> <apex:outputField value="{!o.Total_Cost_Of_Class__c}"/> </td>
                    <td> <apex:outputField value="{!o.Status}"/> </td>
                    <td> <apex:outputField value="{!o.ClassTeacher__r.Name}"/> </td>


                    <apex:form >
                        <td> <apex:selectList value="{!selectedVal}" size="1"> 
                            <apex:selectOptions value="{!openPresentationOptions}" /> 
                            </apex:selectList></td>

                        <td> 
                            <apex:inputText value="{!o.Teacher_Wages__c}"/> </td>
                        <td>

                            <apex:commandLink action="{!saveS}" value="saveS">
                                <apex:param name="saveOrder" value="{!o.id}"/>
                            </apex:commandLink>
                        </td>
                    </apex:form>


                </tr>  
            </table>

APEX:

public String selectedVal{get;set;}  

    public List<SelectOption > getopenPresentationOptions(){
        List<SelectOption> options = new List<SelectOption>();
        options.add( new SelectOption('0000','Choose Teacher'));
        for( Contact teacher : [ SELECT Id, Name FROM Contact WHERE Type__c = 'Teacher' order by Id desc] ) {
            options.add( new SelectOption(teacher.Id,teacher.name));
        }
        return options;
    }

public List<Order> aorders {

        get{
        aorders = [
            SELECT Id, OrderNumber, AccountId, EffectiveDate , EndDate, Status, Total_Cost_Of_Class__c,
            ClassTeacher__r.Name, Teacher_Wage__c, Programme__r.Name, Programme_Code__c, ClassTeacher__r.Id
            FROM Order
            order by OrderNumber desc];
        return aorders;
        }
        set ;
    }

public void saveS() {

        string oid = ApexPages.currentpage().getParameters().get('saveOrder'); 
        Order singleRec;
        for (Order x: aorders) {
            if (oid == x.id) {
                singleRec = x;
                break;              
            }
        }
        if (singleRec != null)

         System.debug('order ' +singleRec);
        System.debug('order number ' +singleRec.OrderNumber);
    System.debug('TEACHER WAGE ' +singleRec.Teacher_Wage__c);
    System.debug('TEACHER ' +singleRec.ClassTeacher__c);

        }

For the teacher wage I have also tried changing the inputText value to use {!TeacherWageFromPage} and create a getter and setters in the controller but still when I check the debug log after run it shows as NULL.

public Decimal getTeacherWageFromPage() {
        return teacherWage;

    }

    public void SetTeacherWageFromPage(Decimal s) {
        teacherWage = s; 
    }
  • 1) why are you placing apex:form inside each table row? Normal practice would be one apex:form surrounding entire table ; 2) your getOpenPresentationOptions() returns an invariant list yet it will be called for every row. Build once if property is null, otherwise return the list – cropredy Mar 6 '15 at 22:03
1

Because of the way you've got the getter for aorders written the query for data is performed every time you reference that member.

So, in your saveS method the for loop which is iterating over aorders is causing the query to be executed again and new data to be retrieved from the database. This query is overwriting the data which has been posted from the form in the page.

One possible solution is to check within the getter to see if the query has been executed already. If it has, return that data. If it has not, execute the query and then return. (example below)

Another solution would be to remove the query from the getter and place it in the controller's constructor. I would suggest doing the same thing for the getopenPresentationOptions getter. Doing this can also improve your page performance, depending on how many times these queries are being executed during the page lifecycle.

public List<Order> aorders {

    get {

        // check to see if this member is already populated with the results
        // of a previous call to the getter before executing the query
        if (aorders != null) {
            return aorders;
        }

        aorders = [
        SELECT Id, OrderNumber, AccountId, EffectiveDate , EndDate, Status, Total_Cost_Of_Class__c,
        ClassTeacher__r.Name, Teacher_Wage__c, Programme__r.Name, Programme_Code__c, ClassTeacher__r.Id
        FROM Order
        order by OrderNumber desc];
        return aorders;
    }
    set ;
}
| improve this answer | |
  • Hi Mark, thanks for the comments. I have tried your first suggestion but I am still seeing the debug log showing my inputText is null. A follow up question - when I post the form data, should the SETTER in the aorders be triggered? I have added debug text eg: set { system.debug('set aorder'); { but I never see this being logged after I post the form – yellowSF Mar 6 '15 at 18:42
  • I have also tried the 2nd suggestion of moving the query into the controller's constructor but no luck. Could it be something related to the way the VisualForce is written? – yellowSF Mar 6 '15 at 19:08
  • Probably should be private set, no? Might be worth pointing out a reference on the Lazy Load pattern (such as this one). The pattern there is probably more commonly seen (one unconditional return statement). Still +1! – Adrian Larson Feb 20 '16 at 23:59
1

Your page design is a wee irritating and feels like it needs a design spec more than code fixes:

  • First of all, in what context is it shown?
  • Why is there one table for each order?
    • With this many columns — even having empty headers (?) —
      a table doesn't seem to be the appropriate layout.
      • Try boxes (/ divs).
  • Why would your save function iterate all orders if you're only changing one at a time?
    • What's your intention concerning singleRec?
  • Your nesting of an apex:form feels something between semantically wrong and awkward.
| 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.