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
jcaldjcald 

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!

 

k_bentsenk_bentsen

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.

jcaldjcald

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;
}

jcaldjcald
I've updated to "Case" now I'm getting
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;
}
jcaldjcald

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?

jcaldjcald

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;
}

k_bentsenk_bentsen

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;

 

 

jcaldjcald

Thanks for this. It has been saved without errors.

now, Im going to try on the test class.

 

 

jcaldjcald

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;
}

k_bentsenk_bentsen
@isTest
public class ReplyEmailToCaseTest {
    static testMethod void validateChangeOwnership() {
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User testUser = new User(Alias = 'standt', FirstName = 'Fred', Email='standarduser@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='stduser@testorg.com');
        insert testUser;
        Case testC = new Case(/* insert any required case fields here, ex: Subject = 'Test subject', etc.*/);
        insert testC;
        EmailMessage testEM = new EmailMessage(ParentId = testC.Id, Incoming = FALSE, FromAddress = 'stduser@testorg.com');
        insert testEm;
    }
}

 See if that works for you

jcaldjcald

Thanks for this!! I will disseminate the content and hopefully try to understand. Thank you.

k_bentsenk_bentsen

I goofed, change this line as shown below:

 

EmailMessage testEM = new EmailMessage(ParentId = testC.Id, Incoming = FALSE, FromAddress = 'standarduser@testorg.com');