You need to sign in to do that
Don't have an account?

test class required for trigger on contact
Problem:
Match the contact email field of newly created contact to the website field of ACCOUNT and assign that contact to account using trigger.
////Can anyone provide test class considering all the cases positive negative bulk for this trigger.
-------Trigger------
trigger assign contact on Contact(before insert, before update){
assignContact.triggerClass(Trigger.New);
}
--------related class-------------
public class assignContact {
public static void triggerClass(List<Contact> ContactList) {
Map<String, List<Contact>> domainVsContactMap = new Map<String, List<Contact>>();
for(Contact c: ContactList) {
if(c.Email != null) {
List<String> domainsplit=c.email.split('@',2);
String domain=domainsplit[1];
system.debug('Domain---->>>: '+domain);
if(domainVsContactMap.containsKey(domain)) {
List<Contact>temp=domainVsContactMap.get(domain);
temp.add(c);
domainVsContactMap.put(domain, temp);
}
else {
domainVsContactMap.put(domain, new List<Contact>{c});
}
}
}
if(!domainVsContactMap.isEmpty()) {
system.debug('Not Empty');
List<Account> accList=new List<Account>();
accList=[SELECT Id, Website FROM Account WHERE Website In:domainVsContactMap.keySet()];
system.debug('accList'+accList);
for(Account acc : accList) {
system.debug('Account: '+acc);
List<Contact>conList=new List<Contact>();
conlist=domainVsContactMap.get(acc.Website);
for(Contact c : conList) {
system.debug('Contact: '+ c);
c.AccountId = acc.Id;
}
}
}
}
}
Match the contact email field of newly created contact to the website field of ACCOUNT and assign that contact to account using trigger.
////Can anyone provide test class considering all the cases positive negative bulk for this trigger.
-------Trigger------
trigger assign contact on Contact(before insert, before update){
assignContact.triggerClass(Trigger.New);
}
--------related class-------------
public class assignContact {
public static void triggerClass(List<Contact> ContactList) {
Map<String, List<Contact>> domainVsContactMap = new Map<String, List<Contact>>();
for(Contact c: ContactList) {
if(c.Email != null) {
List<String> domainsplit=c.email.split('@',2);
String domain=domainsplit[1];
system.debug('Domain---->>>: '+domain);
if(domainVsContactMap.containsKey(domain)) {
List<Contact>temp=domainVsContactMap.get(domain);
temp.add(c);
domainVsContactMap.put(domain, temp);
}
else {
domainVsContactMap.put(domain, new List<Contact>{c});
}
}
}
if(!domainVsContactMap.isEmpty()) {
system.debug('Not Empty');
List<Account> accList=new List<Account>();
accList=[SELECT Id, Website FROM Account WHERE Website In:domainVsContactMap.keySet()];
system.debug('accList'+accList);
for(Account acc : accList) {
system.debug('Account: '+acc);
List<Contact>conList=new List<Contact>();
conlist=domainVsContactMap.get(acc.Website);
for(Contact c : conList) {
system.debug('Contact: '+ c);
c.AccountId = acc.Id;
}
}
}
}
}
Try the Below code.
************************************* class *********************************
Thanks.
Akshay