+ Start a Discussion
Mohd NabeelMohd Nabeel 

IN operator must be used with an iterable expression.. What should i do in this..

trigger BillingAddressMod on Account (after update) {
    public static boolean flag=true;
    If(flag==true) {
        account acc = [select id, BillingPostalCode from account where id in : trigger.new];
        List<contact> contList = [Select id, MailingPostalCode from Contact];
        Set <Id> setContact = new Set<id>();
        for(Contact cont: contList){
            setContact.add(cont.MailingPostalCode);
        }
        Map<id,Contact> mapAccount = [Select MailingPostalCode from Contact where AccountId in: acc];
        
        for (contact con : contList)
        {
            if(acc.BillingPostalCode != con.MailingPostalCode)
            {
                contList.add(con);
            }
            
        }
        If(contList.size()>=1)
        {
            acc.OutofZip__c = true;   
        }
        flag=false;
        
        update acc;
        
    }
}

 
Best Answer chosen by Mohd Nabeel
Erik Hamelink 2Erik Hamelink 2
you are right my bad, this should be:
for(Contact con : accsWithContactMap.get(acc.Id).Contacts){

 

All Answers

Ajay K DubediAjay K Dubedi
Hi Nabeel,

I have gone through your code. (In) Operator can only be used with lists while in your code in line number 10, 
you are using (In) operator with a single account object(acc).
You should either use (=) instead of (In) or convert acc object into a list of account objects. 


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Mohd NabeelMohd Nabeel
Hi Erik on line 9 it is giving me an error that Loop must iterate over collection : Account
Erik Hamelink 2Erik Hamelink 2
you are right my bad, this should be:
for(Contact con : accsWithContactMap.get(acc.Id).Contacts){

 
This was selected as the best answer
Erik Hamelink 2Erik Hamelink 2
Hi there,

I'm not really sure what you are trying to do here but I gave it a go changing your code to something that might work for you
 
trigger BillingAddressMod on Account (before update) {
    public static boolean flag=true;
    If(flag==true) {
		//these are the accounts with their contacts
        Map<Id, Account> accsWithContactMap = new Map<Id, Account>([select id, BillingPostalCode, (select Id, MailingPostalCode from Contacts) from account where id in : trigger.new]);
        
        for (Account acc : (List<Account>)Trigger.new)
        {
			for(Contact con : accsWithContactMap.get(acc.Id).Contacts){
            	if(acc.BillingPostalCode != con.MailingPostalCode)
            	{
                	// do something to the account
					// no need to do an update as this is in before context
					acc.OutofZip__c = true;
            	}
			}
        }
        flag=false;   
    }
}

Please mark this as the correct answer if you were able to do what you where planning with this code
Mohd NabeelMohd Nabeel
Thanku Erik it works..
Erik Hamelink 2Erik Hamelink 2
That's great!
please mark my answer as the correct answer for future reference
Mohd NabeelMohd Nabeel
Hi Erik, the above code is working fine but the thing is i am woring on the related objects so in this context after event should be invoked but you are using before event. In this case if a field doesnt get updated in that scenario also the checkbox will get true which shouldnt be.. but when i am taking an after event it is giving me an error after read only..
Erik Hamelink 2Erik Hamelink 2
All data used in this trigger is available and updated in the before update context as I'm using the Trigger.new list. I'm not sure what you are refering to as "not updated".