如何更新子查询字段? salesforce,soql,apex

时间:2012-04-04 04:19:54

标签: subquery salesforce soql

这是一个查询,它将让我所有的联系人都有高清质量的订单。

`Orderc__c[] orders = [SELECT id,customer__c, Customer__r.Number_of_HD_Orders__c,` `Quality_Code__c FROM Orderc__c where Quality_Code__c='HD'];`

然后我改变使用这些代码来更新每个联系人的HD订单数量:

for(Orderc__c o: orders){
    if(o.Customer__r.Number_of_HD_Orders__c==null)
    o.Customer__r.Number_of_HD_Orders__c=0.0;
    o.Customer__r.Number_of_HD_Orders__c++;
}

现在,问题是如何更新联系人。作为“更新订单;”不会更新联系人。

1 个答案:

答案 0 :(得分:0)

您只需将所有联系人添加到新集合中,然后更新:

map<Id, Contact> contacts = new map<Id, Contact>();

for(Orderc__c o: orders) {
    if(o.Customer__r.Number_of_HD_Orders__c == null) {
        o.Customer__r.Number_of_HD_Orders__c=0.0;
    }

    Contact sContact = contacts.get(o.Customer__c);

    if(sContact != null) {
        sContact.Number_of_HD_Orders__c++;
    } else {
        o.Customer__r.Number_of_HD_Orders__c++;
        contacts.put(o.Customer__c, new Contact(id = o.Customer__c, Number_of_HD_Orders__c = o.Customer__r.Number_of_HD_Orders__c));
    }
}

update contacts.values();

请注意,这是非常粗糙的,不会检查更新中的太多联系人等,但它应该让您的思路正确!