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

assign contact to account on basis of domain
I want to write trigger for folloeing use case
Match the contact email field of newly created contact to the website field of ACCOUNT and assign that contact to account using trigger.
Match the contact email field of newly created contact to the website field of ACCOUNT and assign that contact to account using trigger.
try this and let me know if it works for you. Works for your mark ur answer.
Thanks
Niraj
Its giving error on insert of contact.
Hi Supriya,
Just Try this Below Code For Requirement --
--------------------Trigger Code---------------------------
Thanks
Akshay
This one is inserting only the contact record. and Related account is not getting assosciated.
Just Re-Check It Once In my Org Its working Completely Fine The Contact gets associated with its Matching Website Account.
It working fine in my org any how you update this line of code
List<Contact> tempContact; // At Line no 5.
With this code:
List<Contact> tempContact = new List<Contact>();
iit will work now.
if it works for you, mark your answer to help others.
Thanks
Niraj
Please try the following:
-------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;
}
}
}
}
}
Please let me know if you have any query.
Thanks
Ajay Dubedi
Its good to know that it works for you.
Below is the test class considering the negative and positive scenario.
If it completes your requirement then please mark this as Best Answer to help others.
Please let me know if you have any query.
Thanks!
Ajay Dubedi
But assignContactBasedOnAccountDomain_PositiveTest
assignContactBasedOnAccountDomain_NegativeTest these two test methods are failing.
Can you suggest some solution?
The possible cause of this is due to some other trigger running in your org. Because in my org it's working totally fine, if you really want to check it then you can try to deactivate all the trigger on account and contact object.
Please respond with the error message or status message, so that I can figure this out.
Thanks!
Ajay Dubedi