You need to sign in to do that
Don't have an account?
sam patil
help me to write test class on conatct trigger
public static void setEmailDomain(List<Contact> contactNewList) {
Set<Id> accountIds = new Set<Id>();
for(Contact con : contactNewList) {
if(con.AccountId != null) {
accountIds.add(con.AccountId);
}
}
List<Account> accountList = new List<Account>();
List<Account> accountToUpdate = new List<Account>();
if(!accountIds.isEmpty()) {
accountList = [SELECT Id, Email_Domain__c, (SELECT Id, Email FROM Contacts WHERE Email != null)
FROM Account WHERE Id IN : accountIds];
for(Account acc : accountList) {
String emailDomainString = '';
if(!acc.Contacts.isEmpty()) {
for(Contact con : acc.Contacts){
if(emailDomainString.contains(con.Email.subStringAfter('@'))) {
emailDomainString = emailDomainString + con.Email.subStringAfter('@') + ', ';
}
}
acc.Email_Domain__c = emailDomainString;
accountToUpdate.add(acc);
}
}
if(!accountToUpdate.isEmpty()) {
update accountToUpdate;
}
}
}
}
Set<Id> accountIds = new Set<Id>();
for(Contact con : contactNewList) {
if(con.AccountId != null) {
accountIds.add(con.AccountId);
}
}
List<Account> accountList = new List<Account>();
List<Account> accountToUpdate = new List<Account>();
if(!accountIds.isEmpty()) {
accountList = [SELECT Id, Email_Domain__c, (SELECT Id, Email FROM Contacts WHERE Email != null)
FROM Account WHERE Id IN : accountIds];
for(Account acc : accountList) {
String emailDomainString = '';
if(!acc.Contacts.isEmpty()) {
for(Contact con : acc.Contacts){
if(emailDomainString.contains(con.Email.subStringAfter('@'))) {
emailDomainString = emailDomainString + con.Email.subStringAfter('@') + ', ';
}
}
acc.Email_Domain__c = emailDomainString;
accountToUpdate.add(acc);
}
}
if(!accountToUpdate.isEmpty()) {
update accountToUpdate;
}
}
}
}
Try Below Code Please Mark It As Best Asnwer If It Helps
Thank You!