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
Sandhya K 10Sandhya K 10 

Update Account field with active contact

I have Created a field email__c on Account and I want to update Update this with the contact that is Active (Active__c checkbox on Contact)
When contact status is active, then I want to deactivate all related account contacts and update active contact email on account email filed . How do I achieve this. I am not knowing how do I deactivate the remaining contacts if the current contact is Active. Please guide me in SOlving this
public class updateAccwithActiveContactEmail {

    public Static void updateAccountwithActiveContact(List<Contact> newconlist){
        
        List<Account> accToUpdate = new List<Account>();
        List<Contact> conToUpdate = new List<Contact>();
       
        Set<Id> accIdSet = new Set<Id>();
        for(Contact c : newconlist){
         accIdSet.add(c.AccountId);   
        }
        system.debug(accIdSet);
        
       List<Account> accWithConlist = [Select Id,Email__c,(Select Id,Active__c,Email,AccountId from Contacts) from Account Where Id IN : accIdSet];
       List<Contact> conlist = accWithConlist[0].Contacts;
        system.debug(accWithConlist);
       system.debug(conlist); 
        system.debug(newconlist);
        
        for(Contact c : newconlist){
         for(Account a : accWithConlist ){
             for(Contact con :conlist ){
                   // if(c.Id == con.Id && c.Active__c == true){
                    if(c.Id != con.Id ){
                    con.Active__c = False ;
                    con.Account.Email__c = c.Email;
                    }
                   
                    
                // accToUpdate.add(a);
                conToUpdate.add(con);
                   // system.debug(accToUpdate);
                    system.debug(conToUpdate);
               }
             // update accToUpdate;  
            }
        }
     }
}

 
Best Answer chosen by Sandhya K 10
Tarun J.Tarun J.
Hello Sandhya,

Try this:
 
public class updateAccwithActiveContactEmail {

    public Static void updateAccountwithActiveContact(List<Contact> newconlist){
        
        List<Account> accToUpdate = new List<Account>();
        List<Contact> conToUpdate = new List<Contact>();		
       
        Set<Id> accIdSet = new Set<Id>();
		Map<Id, Contact> accIdContactMap = new Map<Id, Contact>();
		List<Id> conId = new List<Id>();		//List to store updated Active Contact Id
        for(Contact c : newconlist){
			//accIdSet.add(c.AccountId);  
			accIdContactMap.put(c.AccountId, c);		//Map to hold Account id and associated active contact
			conId.add(c.Id);		//Contact Id updated to Active considering newconlist variable in method parameter has contacts which are active only, else add a condition to check active.
        }
        system.debug(accIdSet);
       
		//Fetching contacts which are not part of newconList and active.
       List<Account> accWithConlist = [Select Id,Email__c,(Select Id,Active__c,Email,AccountId FROM Contacts WHERE ID NOT IN: conId and Active__c = false) from Account Where Id IN : accIdContactMap.keySet()];
       
	    for(Account acc : accWithConList){
			acc.Email__c = accIdContactMap.get(acc.Id).Email;
			
			accToUpdate.(acc);
			
			for(Contact conDeactive : accWithConList.Contacts){
				conDeactive.Active__c = false;
				conToUpdate.add(conDeactive);
			}
		}
		
		try{
			update accToUpdate;
			
			update conToUpdate;
			
		}catch(Exception e){
			System.debug('Error...'+e);
		}
	   
	   
	   
	   /*List<Contact> conlist = accWithConlist[0].Contacts;
       system.debug(accWithConlist);
       system.debug(conlist); 
        system.debug(newconlist);
        
        for(Contact c : newconlist){
         for(Account a : accWithConlist ){
             for(Contact con :conlist ){
                   // if(c.Id == con.Id && c.Active__c == true){
                    if(c.Id != con.Id ){
                    con.Active__c = False ;
                    con.Account.Email__c = c.Email;
                    }
                   
                    
                // accToUpdate.add(a);
                conToUpdate.add(con);
                   // system.debug(accToUpdate);
                    system.debug(conToUpdate);
               }
             // update accToUpdate;  
            }
        }
     }*/
}

-Thanks,
TK

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

All Answers

Tarun J.Tarun J.
Hello Sandhya,

Try this:
 
public class updateAccwithActiveContactEmail {

    public Static void updateAccountwithActiveContact(List<Contact> newconlist){
        
        List<Account> accToUpdate = new List<Account>();
        List<Contact> conToUpdate = new List<Contact>();		
       
        Set<Id> accIdSet = new Set<Id>();
		Map<Id, Contact> accIdContactMap = new Map<Id, Contact>();
		List<Id> conId = new List<Id>();		//List to store updated Active Contact Id
        for(Contact c : newconlist){
			//accIdSet.add(c.AccountId);  
			accIdContactMap.put(c.AccountId, c);		//Map to hold Account id and associated active contact
			conId.add(c.Id);		//Contact Id updated to Active considering newconlist variable in method parameter has contacts which are active only, else add a condition to check active.
        }
        system.debug(accIdSet);
       
		//Fetching contacts which are not part of newconList and active.
       List<Account> accWithConlist = [Select Id,Email__c,(Select Id,Active__c,Email,AccountId FROM Contacts WHERE ID NOT IN: conId and Active__c = false) from Account Where Id IN : accIdContactMap.keySet()];
       
	    for(Account acc : accWithConList){
			acc.Email__c = accIdContactMap.get(acc.Id).Email;
			
			accToUpdate.(acc);
			
			for(Contact conDeactive : accWithConList.Contacts){
				conDeactive.Active__c = false;
				conToUpdate.add(conDeactive);
			}
		}
		
		try{
			update accToUpdate;
			
			update conToUpdate;
			
		}catch(Exception e){
			System.debug('Error...'+e);
		}
	   
	   
	   
	   /*List<Contact> conlist = accWithConlist[0].Contacts;
       system.debug(accWithConlist);
       system.debug(conlist); 
        system.debug(newconlist);
        
        for(Contact c : newconlist){
         for(Account a : accWithConlist ){
             for(Contact con :conlist ){
                   // if(c.Id == con.Id && c.Active__c == true){
                    if(c.Id != con.Id ){
                    con.Active__c = False ;
                    con.Account.Email__c = c.Email;
                    }
                   
                    
                // accToUpdate.add(a);
                conToUpdate.add(con);
                   // system.debug(accToUpdate);
                    system.debug(conToUpdate);
               }
             // update accToUpdate;  
            }
        }
     }*/
}

-Thanks,
TK

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
This was selected as the best answer
Sandhya K 10Sandhya K 10
Hi Tarun,

With few changes I am able to get it done.The change that I made is , In line 19 Kept Active__c = true as got what I needed. Thank you.