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

Change case ownership from email reply
I'm trying to see if this can be done. Supposively, I need an apex trigger. My issue is that I need to change case ownership from my first intial response from the email to case. Meaning, if there is a new case in the queue and tech1 replies to the case. I want tech1 to be the owner of that case. Since he responded first to the case. Is this possible with an apex trigger?
I've taken Vinit apex trigger and tried to customize it but realized i didnt know what to do with or where to put it or what objects to create..etc.etc.I'im a newbie to apex world.
trigger ReplyEmailToCase on EmailMessage (after insert) {
List<String> frmadd = new List<String>();
List<Id> cseIds = new List<Id>();
User con = new User();
List<Case> cse = new List<Case>();
List<Case> updatedCseList = new List<Case>();
for(EmailMessage msg : Trigger.new){
frmadd.add(msg.FromAddress);
cseIds.add(msg.parentid);
}
con = [SELECT OwnerId FROM User WHERE Email in:frmadd limit 1];
cse = [SELECT ownerid,id, Description, caseNumber FROM Case WHERE Id in: cseIds];
for(Case cs:cse){
cs.ownerid = con.ownerid;
updatedCseList.add(cs);
}
update updatedCseList;
}
Can someone point to the right direction? Thanks!
It's not very pretty, but the above code should work. What is the issue you are having? Are you asking where it needs to go? If so, then you'll need to paste it into your sandbox by going to Setup > App Setup > Customize > Cases > Email Messages > Triggers, click the "New" button, and insert the code. After that, you'll need to deploy the trigger to your production environment. You can read about deployments here http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_deploy.htm You may also need to write a test class to insure that the trigger code is adequately covered.
Thanks for pointing me in the direction. I've pasted the trigger in my sandbox but now I'm getting this errors. I thought OwnerId and User are standard fields. Do I need to create the custom fields?
Error: Compile Error: No such column 'OwnerId' on entity 'User'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 12 column 7
trigger ReplyEmailToCase on EmailMessage (after insert) {
List<String> frmadd = new List<String>();
List<Ids> cseIds = new List<Id>();
User con = new User();
List<Case> cse = new List<Case>();
List<Case> updatedCseList = new List<Case>();
for(EmailMessage msg : Trigger.new){frmadd.add(msg.FromAddress);
cseIds.add(msg.parentid);
}
// Isn't "OwnerId" and "User" a standard field? //
con = [SELECT OwnerId FROM User WHERE Email in:frmadd limit 1];
cse = [SELECT OwnerId,id, Description, caseNumber FROM Case WHERE Id in: cseIds];
for(Case cs:cse){
cs.OwnerId = con.OwnerId;
updatedCseList.add(cs);
}
update updatedCseList;
}
Error: Compile Error: Invalid type: Ids at line 3 column 6
trigger ReplyEmailToCase on EmailMessage (after insert) {
List<String> frmadd = new List<String>();
List<Ids> cseIds = new List<Id>();
Case con = new Case();
List<Case> cse = new List<Case>();
List<Case> updatedCseList = new List<Case>();
for(EmailMessage msg : Trigger.new){frmadd.add(msg.FromAddress);
cseIds.add(msg.parentid);
}
con = [SELECT OwnerId FROM Case WHERE Email in:frmadd limit 1];
cse = [SELECT OwnerId,id, Description, caseNumber FROM Case WHERE Id in: cseIds];
for(Case cs:cse){
cs.OwnerId = con.OwnerId;
updatedCseList.add(cs);
}
update updatedCseList;
}
cleaned up <Ids> to <Id>
but now im getting:
Error: Compile Error: No such column 'Email' on entity 'Case'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 12 column 7
I guess I need to see if these fields exist first. Is there an easy way to see if these fields exist?
updated. hopefully it should be correct now.
trigger ReplyEmailToCase on EmailMessage (after insert) {
List<String> frmadd = new List<String>();
List<Id> cseIds = new List<Id>();
Case con = new Case();
List<Case> cse = new List<Case>();
List<Case> updatedCseList = new List<Case>();
for(EmailMessage msg : Trigger.new){frmadd.add(msg.FromAddress);
cseIds.add(msg.parentid);
}
con = [SELECT OwnerId FROM Case WHERE Email_From_Address__c in:frmadd limit 1];
cse = [SELECT OwnerId,id, Description, caseNumber FROM Case WHERE Id in: cseIds];
for(Case cs:cse){
cs.OwnerId = con.OwnerId;
updatedCseList.add(cs);
}
update updatedCseList;
}
Actually, change the line:
Case con = new Case();
back to:
User con = new User();
Change the query line:
con = [SELECT OwnerId FROM Case WHERE Email in:frmadd limit 1];
to:
con = [SELECT Id, Email FROM User WHERE Email in:frmadd limit 1];
Change the assignment line:
cs.OwnerId = con.OwnerId;
to:
cs.OwnerId = con.Id;
Thanks for this. It has been saved without errors.
now, Im going to try on the test class.
Can someone tell me how to do a basic test ?
@isTest
public class ReplyEmailToCaseTest {
static testMethod void validateChangeOwnership() {
// setup
List<String> frmadd = new List<String>();
List<Id> cseIds = new List<Id>();
User con = new User();
List<Case> cse = new List<Case>();
List<Case> updatedCseList = new List<Case>();
// verify trigger??
for(EmailMessage msg : Trigger.new){frmadd.add(msg.FromAddress);
cseIds.add(msg.parentid);
}
// verify the results
con = [SELECT Id, Email FROM User WHERE Email in:frmadd limit 1];
cse = [SELECT OwnerId,id, Description, caseNumber FROM Case WHERE Id in: cseIds];
// verify trigger update??
for(Case cs:cse){
cs.OwnerId = con.Id;
updatedCseList.add(cs);
}
// verify updates
update updatedCseList;
}
See if that works for you
Thanks for this!! I will disseminate the content and hopefully try to understand. Thank you.
I goofed, change this line as shown below:
EmailMessage testEM = new EmailMessage(ParentId = testC.Id, Incoming = FALSE, FromAddress = 'standarduser@testorg.com');