Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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:: Error: Compile Error: Loop must iterate over a collection type: SOBJECT:Account at line 12 column 41

share|improve this question

2 Answers 2

up vote 3 down vote accepted
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

share|improve this answer
    
Thanks Mohith!!!!I will keep it in mind next time!! Salesforce stack exchange is awesome!! Exponentially generating my interest in salesforce!! –  SFDC_Geek Aug 11 at 12:26

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){
share|improve this answer
    
Thanks SF_USER!! –  SFDC_Geek Aug 11 at 12:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.