Here is the query which will get me all the contacts have HD quality orders.
`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'];`
then I change use these code to update the number of HD orders for each 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;
o.Customer__r.Number_of_HD_Orders__c++;
}
now, the question is how can I update the contacts. as "update orders;" will not update the contacts.
You just need to add all the contacts to a new collection and then update that:
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();
Note this is very rough, doesn't check for too many contacts in an update etc., but it should get your thinking in the right direction!
update mymap.values();
Matt Lacey 2012-04-04 05:29