function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ruchika Nayyarruchika Nayyar 

error is coming in this code

trigger accountcontact on Account (after insert) {
        list<id> AccountId= new list<Id>();
        for(contact con:Trigger.new){
            accountId.add(con.AccountId);
        }
            map<Id, Account>AccountMap= New map<Id, Account>([Select id,name,myruchika__client_contact__c from Account where Id IN:accountId]);
            list<account> updateaccountlist= new list<Account>();
            for(contact con :trigger.new){
                if(Accountmap !=null&& accountmap.size()>0)  {
                    for(Account acc: Accountmap) {
                        acc.myruchika__client_contact__c=con.Id;
                        updateAccountlist.add(acc);
                        
                    }
                }
            }
           
        if(updateAccountList !=null && updateaccountlist.size()>0)
            update updateAccountlist;
}

error=loop varible must be account type???
mrkimrki
The trigger operates on Account records even if your code seems to expect Contacts. Change the trigger signature to following:
trigger accountcontact on Contact (after insert) {
    // Insert trigger logic here
}
VineetKumarVineetKumar
Try the below code :
Infact the nested for loop is not required
trigger accountcontact on Contact (after insert) {
	list<Id> AccountId= new list<Id>();
	for(contact con:Trigger.new){
		if(con.AccountId != null){
			accountId.add(con.AccountId);
		}
	}
	map<Id, Account>AccountMap= New map<Id, Account>([Select id,name,myruchika__client_contact__c from Account where Id IN: accountId]);
	list<account> updateaccountlist= new list<Account>();
	for(contact con :trigger.new){
		if(AccountMap !=null && AccountMap.size()>0 && AccountMap.containsKey(con.AccountId)){
			Account acc = new Account();
			acc = con.AccountId.get(con.AccountId);
			acc.myruchika__client_contact__c = con.Id;
			updateAccountlist.add(acc);				
		}
	}	   
	if(updateAccountList !=null && updateaccountlist.size()>0)
		update updateAccountlist;
}