You need to sign in to do that
Don't have an account?
AkshayNaidu07
Create Contact from email-to-case
Hi Everyone,
From a previous post I was able to create a Apex Trigger to create a new Contact from Email/web-to-Case.
It works perfect in Sandbox as it check for the existing contact and link it to case or create a New Contact.
However, I am unable to deploy the Trigger in Production as it gives code coverage of 54%.
Any help on a workaround is deploying the apex trigger in Production Org will be highly appreciated.
Thank you!
Apex Code:
trigger TriggertoCreateContactformCase on Case (before insert) {
List<String> UseremailAddresses = new List<String>();
//First exclude any cases where the contact is set
for (Case c:Trigger.new) {
if (c.ContactId==null &&
c.SuppliedEmail!=''|| c.SuppliedEmail==null)
{
UseremailAddresses.add(c.SuppliedEmail);
}
}
//Now we have a nice list of all the email addresses. Let's query on it and see how many contacts already exist.
List<Contact> listofallContacts = [Select Id,Email From Contact Where Email in:UseremailAddresses];
Set<String> ExstingEmails = new Set<String>();
for (Contact c:listofallContacts) {
ExstingEmails.add(c.Email);
}
Map<String,Contact> emailToContactMap = new Map<String,Contact>();
List<Case> casesToUpdate = new List<Case>();
for (Case c:Trigger.new) {
if (c.ContactId==null &&
c.SuppliedName!=null &&
c.SuppliedEmail!=null &&
c.SuppliedName!='' &&
!c.SuppliedName.contains('@') &&
c.SuppliedEmail!='' &&
!ExstingEmails.contains(c.SuppliedEmail))
{
//The case was created with a null contact
//Let's make a contact for it
String[] Emailheader = c.SuppliedName.split(' ',2);
if (Emailheader.size() == 2)
{
Contact conts = new Contact(FirstName=Emailheader[0],
LastName=Emailheader[1],
Email=c.SuppliedEmail
);
emailToContactMap.put(c.SuppliedEmail,conts);
casesToUpdate.add(c);
}
}
}
List<Contact> newContacts = emailToContactMap.values();
insert newContacts;
for (Case c:casesToUpdate) {
Contact newContact = emailToContactMap.get(c.SuppliedEmail);
c.ContactId = newContact.Id;
}
}
From a previous post I was able to create a Apex Trigger to create a new Contact from Email/web-to-Case.
It works perfect in Sandbox as it check for the existing contact and link it to case or create a New Contact.
However, I am unable to deploy the Trigger in Production as it gives code coverage of 54%.
Any help on a workaround is deploying the apex trigger in Production Org will be highly appreciated.
Thank you!
Apex Code:
trigger TriggertoCreateContactformCase on Case (before insert) {
List<String> UseremailAddresses = new List<String>();
//First exclude any cases where the contact is set
for (Case c:Trigger.new) {
if (c.ContactId==null &&
c.SuppliedEmail!=''|| c.SuppliedEmail==null)
{
UseremailAddresses.add(c.SuppliedEmail);
}
}
//Now we have a nice list of all the email addresses. Let's query on it and see how many contacts already exist.
List<Contact> listofallContacts = [Select Id,Email From Contact Where Email in:UseremailAddresses];
Set<String> ExstingEmails = new Set<String>();
for (Contact c:listofallContacts) {
ExstingEmails.add(c.Email);
}
Map<String,Contact> emailToContactMap = new Map<String,Contact>();
List<Case> casesToUpdate = new List<Case>();
for (Case c:Trigger.new) {
if (c.ContactId==null &&
c.SuppliedName!=null &&
c.SuppliedEmail!=null &&
c.SuppliedName!='' &&
!c.SuppliedName.contains('@') &&
c.SuppliedEmail!='' &&
!ExstingEmails.contains(c.SuppliedEmail))
{
//The case was created with a null contact
//Let's make a contact for it
String[] Emailheader = c.SuppliedName.split(' ',2);
if (Emailheader.size() == 2)
{
Contact conts = new Contact(FirstName=Emailheader[0],
LastName=Emailheader[1],
Email=c.SuppliedEmail
);
emailToContactMap.put(c.SuppliedEmail,conts);
casesToUpdate.add(c);
}
}
}
List<Contact> newContacts = emailToContactMap.values();
insert newContacts;
for (Case c:casesToUpdate) {
Contact newContact = emailToContactMap.get(c.SuppliedEmail);
c.ContactId = newContact.Id;
}
}
Hi Akshay,
Can you post your test class code so that we can modify it to meet the required code coverage.
Regards,
Priya Ranjan
Hi,
Please find the solution of your test class with 100% coverage.
You need to change your code=>
remove ! form ExstingEmails.contains(c.SuppliedEmail))
Please let me know it is working or not?
Please mark it as The Best Answer if it helps you.
Thank You
Thank you for helping me out on this.
@Suraj Tripathi 47 I have created a the test call provided by you and updated trigger with your changes; Code Coverage 93%, Line 29/31 with 1 Failure
Reverted the code back with !ExstingEmails.contains(c.SuppliedEmail)); Code Coverage 70%, Line 22/31 with 0 Failure
I am a not a developer so couldn't debug more on this.
Please help!
Thanks