1

Please find my code here.

trigger PM1 on Account (after update) {
    List<Product_Metrics_vod__c> toDelete = new List<Product_Metrics_vod__c>();
    set<id> accsWithChangedRestrictions = new set<id>();
    for(Account acc : trigger.new){
        //if(acc.Restricted_Products_vod__c != trigger.oldMap.get(acc.id).Restricted_Products_vod__c)
            accsWithChangedRestrictions.add(acc.id);
    }

list<Account> accs = [select Restricted_Products_vod__c,id, (select Products_vod__r.Name from Product_Metrics_vod__r) from Account where id in :accsWithChangedRestrictions];

for(Account acc : accs){
    for(Product_Metrics_vod__c av : acc){
        if(acc.Restricted_Products_vod__c.contains(av.Products_vod__r.Name)){
            //found a match
            toDelete.add(av);
        }
    }
}

delete toDelete;

}

Error: Compile Error: Loop must iterate over a collection type: SOBJECT:Account at line 12 column 41

2 Answers 2

5
for(Account acc : accs){
for(Product_Metrics_vod__c av : acc.Product_Metrics_vod__r){ //observe how to pull child records through the inner query
    if(acc.Restricted_Products_vod__c.contains(av.Products_vod__r.Name)){
        //found a match
        toDelete.add(av);
    }
  }
}

The above code should help you

1
  • Thanks Mohith!!!!I will keep it in mind next time!! Salesforce stack exchange is awesome!! Exponentially generating my interest in salesforce!! Commented Aug 11, 2014 at 12:26
2

You did :

for(Account acc : accs){
    for(Product_Metrics_vod__c av : acc){

But acc is not a List but an Account and you can't iterate on it.

Try :

for(Account acc : accs){
        for(Product_Metrics_vod__c av : acc.Product_Metrics_vod__r){
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .