Visualforce:如何从单个表创建多个sObject

时间:2012-04-02 14:54:34

标签: salesforce apex-code visualforce

我有2个自定义对象:'Package'和'Order'。 'Order'包括'Package'字段和其他一些字段。我需要在一个表中为多个“Order”实例指定不同的字段值。当在表(ID = null)中呈现时,“Order”对象不存储在数据库中,并且它与“Package”对象的关系是只读的。问题是如何将所有输入的数据绑定到正确的“订单”对象?

我有以下visualforce源代码。可能是我应该添加一些隐藏字段来指定Order和paticular Package之间的关系,但我不明白如何正确地做。

<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}"/>

另见我的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;
}

1 个答案:

答案 0 :(得分:0)

在处理多种类型的SObject时,我不会使用StandardSetController。我会考虑在我自己的班级combines both中管理Apex。