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
34213421 

update outside the loop

I have a Batch Class where I am looking for Contact matches to Account and would like to update Contact with associating Account.

How do I write update outside the loop? I want to update the Account in the map to the contact from initial loop

Here is the code
 
Set<String> emailDomains = new Set<String>();
 Set<String> emailDomains001 = new Set<String>();
        Set<Id> ctId = new Set<Id>();

    List<Contact> ctlist = [select Id, FirstName,LastName,Email,Business_Name__c,Member_Type__c from Contact where Email <> null and LastModifiedDate = N_DAYS_AGO:5];    

        for (Contact ct : ctlist){
     
     if(ct.Type__c == 'Test'){
                ct.AccountId = '0015467Dvbg';
                update ct;
            }
     else if(ct.Account.Name == ct.Name__c && ct.Type__c <> 'Test'){
         
            emailDomains.add(ct.Email.split('@').get(1));

            } 
  
     else if (ct.Account.Name <> ct.Name__c && ct.Type__c <> 'Test' ){
          
                emailDomains001.add(ct.Email.split('@').get(1));

            }

        Map<String,Account> emailDomainToAccountMap = new Map<String,Account>();

        List<Account> acclist = [select Id, Name,Domain__c from Account where Domain__c <> null and Domain__c IN :emailDomains];
        List<Account> acclist1 = [select Id, Name,Domain__c from Account where Domain__c <> null and Domain__c IN :emailDomains001];

            
        if(!emailDomains.isEmpty() && (!emailDomains.contains('gmail.com') || !emailDomains.contains('yahoo.com') || !emailDomains.contains('outlook.com') || !emailDomains.contains('hotmail.com'))){
            
            for (Account a : acclist){
                emailDomainToAccountMap.put(a.Domain__c,a);
            }
        }
            
        }

 
Raj VakatiRaj Vakati
Set<String> emailDomains = new Set<String>();
Set<String> emailDomains001 = new Set<String>();
Set<Id> ctId = new Set<Id>();
Map<String,Account> emailDomainToAccountMap = new Map<String,Account>();

List<Contact> ctlist = [select Id, FirstName,LastName,Email,Business_Name__c,Member_Type__c from 
Contact where Email <> null and LastModifiedDate = N_DAYS_AGO:5];   

List<Contact> conUpdate = new List<Contact>();
 
	for (Contact ct : ctlist){
		if(ct.Type__c == 'Test'){
			ct.AccountId = '0015467Dvbg';
			conUpdate.add(ct);
		}
		else if(ct.Account.Name == ct.Name__c && ct.Type__c <> 'Test'){
	 
		emailDomains.add(ct.Email.split('@').get(1));

		} 

 else if (ct.Account.Name <> ct.Name__c && ct.Type__c <> 'Test' ){
	  
			emailDomains001.add(ct.Email.split('@').get(1));

		}
	}


	List<Account> acclist = [select Id, Name,Domain__c from Account where Domain__c <> null and Domain__c IN :emailDomains];
	List<Account> acclist1 = [select Id, Name,Domain__c from Account where Domain__c <> null and Domain__c IN :emailDomains001];

		
	if(!emailDomains.isEmpty() && (!emailDomains.contains('gmail.com') || !emailDomains.contains('yahoo.com') || !emailDomains.contains('outlook.com') || !emailDomains.contains('hotmail.com'))){
		
		for (Account a : acclist){
			emailDomainToAccountMap.put(a.Domain__c,a);
		}
	}
	
	if(conUpdate.size()>0){
		update conUpdate ;
		
	}

 
34213421
Hi,
The emaildomains in contacts are mapped with emailDomainToAccountMap, but where are we updating that contact with Account? I mean in the second else if conditions and third else if condition, I was not sure how to map that with the Account retreived through map
Raj VakatiRaj Vakati
On line number 41 

 if(conUpdate.size()>0){
}
34213421
Yesh but conupdate in your case just had the hard coded account name. I would like to update contacts in the else conditions in 16 and 22. I would like to map that contact with the Account lists map in 37 and update those contacts as well