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
Abhishek RayAbhishek Ray 

Trigger for assigning the contact name and contact email in email to case

Hi, Need help in trigger.

 

Whenver a ticket is created through email to case, the ticket is created in the name of my company's administrator and contact name and email shows up as her instead of who actually create the ticket. Although the actual contact who emailed for that ticket exist in our salesforce account.

 

How I can create a trigger to assign the contact name and email for that particular person who created the ticket through email to case instead of my company's administrator? Please help.

sanjaypatidarsanjaypatidar

You can probably write a trigger on case Insert event and update the two fields if the case is coming from email to case.

 

let me know if this help.

Abhishek RayAbhishek Ray

Can you please help how can I write trigger which will first check the email id and then compare it in the contact object email id and if it matches, it will assign the contact name of the person of email.

 

I am not really experience in trigger although I created very few.

vinit_sfvinit_sf

Try below code:

 

trigger checkrelatedContactEmail on Case (before insert,before update) {

List<Case> newCase = new List<Case>();
List<Contact> con = new List<Contact>();
List<String> emailList = new List<String>();

for(Case c: trigger.new){
system.debug('*********' + Trigger.new);
emailList.add(c.Contact.Email);
}

con= [select ownerid from Contact where email=:emailList];

for (Case c:trigger.new){
c.ownerid=con[0].ownerid;
}

}

 

I can not test it as I don't have Email to case enabled.Please let me know if this works.

 

Regards,

Vinit

Abhishek RayAbhishek Ray

Hi tried you code and achieved 100% coverage but when I tried deploying it. It failed.  Here is the error:

 

1. Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CaseContactOwnerName: execution of BeforeInsert caused by: System.ListException: List index out of bounds: 0 Trigger.CaseContactOwnerName: line 14, column 1: []", Failure Stack Trace: "Class.CaseOw 

 

2. Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CaseContactOwnerName: execution of BeforeInsert caused by: System.QueryException: Non-selective query against large object type (more than 100000 rows). Consider an indexed filter or contact salesforce.

 

Please help