Search code examples
salesforceapex-codevisualforce

Visualforce: how to create multiple sObjects from the single table


I have 2 custom objects: 'Package' and 'Order'. 'Order' includes 'Package' field and some other fields. I need to specify different field values for several 'Order' instances in one table. 'Order' object is not stored in database when rendered in table (ID = null), and its relation with 'Package' object is read-only. The question is how to bind all inputted data to proper 'Order' objects?

I have the following visualforce source. May be I should add some hidden field to specify relation between Order and paticular Package, but I don't understant how to do it right.

<apex:pageBlockTable value="{!products}" var="product">
            <apex:column headerValue="Product Type">
                <apex:outputLink value="/{!product.Package__r.ID}">{!product.Package__r.name}</apex:outputLink>
            </apex:column>
            <apex:column value="{!product.package__r.X12NC__c}"/>
            <apex:column value="{!product.package__r.Product_Description__c}"/>
            <apex:column value="{!product.package__r.Stock__c}"/>
            <apex:column value="{!product.package__r.Expected__c}"/>
            <apex:column value="{!product.package__r.Product_Status__c}"/>                
            <apex:column value="{!product.package__r.Max_Qty__c}"/>                
            <apex:column headerValue="Quantity">                    
                <apex:inputField value="{!product.amount__c}"/>
            </apex:column>
        </apex:pageBlockTable>
        <apex:commandButton value="Make order" action="{!makeOrder}"/>

Also see fragment of my Controller:

public class OrdersController {

public String X12NC { get; set; }

public String productType { get; set; }

public List<max__Order__c> orders = new List<max__Order__c>();    

public Set<max__Order__c> ordered{ get; set; }

public Integer orderedNumber { get { return ordered.size(); } }

public OrdersController() {
    System.debug(Logginglevel.INFO, 'Start constructor');
    this.X12NC = ApexPages.currentPage().getParameters().get('x12nc');
    this.productType = ApexPages.currentPage().getParameters().get('productType');
    this.ordered = new Set<max__Order__c>();
    System.debug(Logginglevel.INFO, 'productType  = ' + this.productType);
    System.debug(Logginglevel.INFO, 'x12nc = ' + this.X12NC);
}    

public ApexPages.StandardSetController conn {
    get {            
        if (conn == null) {
            System.debug(Logginglevel.INFO, 'Creating new connection!');
            String pType = this.productType == null ? '%': '%' + this.productType + '%';
            String x12nc = this.x12nc == null ? '%': '%' + this.x12nc + '%';

            System.debug(Logginglevel.INFO, 'pType  = ' + pType);
            System.debug(Logginglevel.INFO, 'x12nc  = ' + x12nc);
            conn = new ApexPages.StandardSetController(Database.getQueryLocator([
                select name, max__X12NC__c, max__Product_Description__c, max__Expected__c, max__Stock__c, max__Product_Status__c, max__Max_Qty__c from max__Package__c 
                where max__X12NC__c like :x12nc    
                and name like :pType                
                limit 2000
            ]));
            conn.setPageSize(20);                
        }
        return conn;
    }

    set;
}

// Initialize setCon and return a list of records
public List<max__Order__c> getProducts() {
    System.debug(Logginglevel.INFO, 'getProducts() call');
    orders.clear();
    for (max__Package__c pckg: (List<max__Package__c>) conn.getRecords()) {
        max__Order__c order = new max__Order__c(max__Package__r = pckg);
        orders.add(order);
    }

    return orders;
}

public PageReference search() {
    System.debug(Logginglevel.INFO, 'Call search method!');
    this.ordered.clear();
    this.conn = null;
    return null;
}

public PageReference makeOrder() {
    System.debug(Logginglevel.INFO, 'Make order');
    for (max__Order__c order: orders) {
        System.debug(Logginglevel.INFO, 'Inspecting order = ' + order);
        if (order.amount__c != null && order.amount__c > 0) {
            System.debug(Logginglevel.INFO, 'Add ' + order + ' to shopping cart');
            System.debug(Logginglevel.INFO, 'Order package = ' + order.package__c);
            ordered.add(order);
        }
    }

    System.debug('The number of objects added in shopping cart: ' + ordered.size());
    return null;
}

Solution

  • I wouldn't go with StandardSetController when dealing with more than one type of SObject. I'd consider managing the Apex myself in my own class which combines both.