I have a trigger that will populate 3 fields on a case when created with email2case. The case owner needs to be a queue. I've tried a couple of different things but it's not populating. The logs show the correct group id, but in the case it reverts to the default case owner. Any help is appreciated.

    trigger email2CaseTrigger on EmailMessage (after insert) {
    List<Case> cases = new List<Case>();

    Id queueId = [select queue.Id from queueSobject where queue.Name='TechOps Queue'].Id;

    System.debug('******Entering EmailAfterUpdate******');
    EmailMessage newEmail = Trigger.new[0];
    List<Case> c = [SELECT Id, OwnerId, Origin, Case_Origin__c FROM Case WHERE Id = :newEmail.ParentId];
    System.debug('Email to address: ' + newEmail.ToAddress);
    if (newEmail.ToAddress == 'help@company.com') {
        c[0].OwnerId = ownerId;
        System.debug('-------The Owner Id is:\n'+c[0].OwnerId+'-------');
        c[0].Origin = 'Clearent';
        System.debug('-------The Origin is:\n'+c[0].Origin+'-------');
        c[0].Case_Origin__c = 'Email';
        System.debug('-------The Case Origin is:\n'+c[0].Case_Origin__c+'-------');

        update c;

  • When I used the queueId query to populate the OwnerId, I got a Field_Integrity_Exception. And it said the Id was of the improper type. – nwillard Oct 19 '15 at 16:24
  • 2
    nwillard -- (1) I might suggest you rewrite the trigger to be bulkified and edit your post accordingly; use of hard-coded ownerIds is also not best practice; (2) the update Cases; does nothing as this list is never modified (3) if you are fetching the queueid via SOQL, why are you assigning the hard-coded ownerid to the EmailMessage's Case? (4) The MAC is part of the 18 character SFDC ID – cropredy Oct 19 '15 at 17:19
  • @crop1645 I know I need to refactor the code, I guess I should've cleaned it up a bit more before posting. I had the hard coded Id in there as well as the query but only used one to populate the owner field. Thanks for your help as it was helpful. My problem it seems is that I'm fighting with the assignment rules, that's why the owner wouldn't populate with the value I gave it. – nwillard Oct 20 '15 at 14:40

queue.Id is the Id of your queue and Id is the Id of QueueSObject. Your queue must be having only 1 object associated with it. And hence you are able to query in 1 go. If there are 2 objects associated with the queue, your query will return 2 records. Hence id will not be compatible for OwnerId field.

Either use

[select queue.Id from queueSobject where queue.Name='TechOps Queue'].queue.Id 
[select id from group where name = 'TechOps Queue'].Id

Have a look in salesforce documentation for more details: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_queuesobject.htm

  • I used your suggestion, and was getting similar values in the logs as before. The problem I found is that I was fighting with the case assignment rules to set the owner. – nwillard Oct 20 '15 at 14:36

In the end I found another answered question that helped me solve the problem. I was running into a conflict between assignment rules and the trigger populating the owner on the case. Here is a link to the answer from @sfdcfox posted: Assignment Rule Workaround

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.