3

I'm using LiveHelpNow to create Cases and it works fine, I do capture the LiveHelpNow Agent name/SF user Name to a custom field Case.Case_owner__c. However I run a loop to compare it to User object and match it against existing users in SF User Object. Also I check if the case was assigned to Queue I need my Trigger to run after the case got assigned to the queue and replace the Queue owner to the user name I captured and matched with the salesforce user name. code is not giving any error but no Case is being creating. Any Idea?

Note: Im using DmlOption to turn off the case assignment rule for cases only coming for LiveHelpNow.

trigger LiveHelpAfterInsert on Case (after update){
    Map<String, Id> cases = new Map<String, Id>();
    Set<String> ownerName = new Set<String>();
    for(Case record: Trigger.new)
    {
        if(String.ValueOf(record.OwnerId).startsWith('00G'))
        {
            cases.put(record.Case_owner__c, null);
            ownerName.add(record.Case_owner__c);
        }
    }
    for(User record:[select Id, Name from User where Name IN:cases.keySet()])
    {
        cases.put(record.Name, record.Id);
    }
    Database.DMLOptions options = new Database.DMLOptions();  
    options.AssignmentRuleHeader.useDefaultRule = false; 
    for(Case record : Trigger.new)
    { 
        record.setOptions(options);
        record.OwnerId = cases.get(record.Case_owner__c);           
    } 
}
3
  • Just a recommendation, when you are formatting code you have pasted in, select the whole block and hit CTRL + K.
    – Adrian Larson
    Jul 15, 2016 at 20:06
  • You can only set DMLOptions on a record you are going to perform a DML Operation on, not one that's mid-transaction.
    – Adrian Larson
    Jul 15, 2016 at 20:13
  • is it possible to set the case assignment rule to false in a trigger (Before Insert)?
    – Templar
    Jul 15, 2016 at 20:33

1 Answer 1

0

You need to run your trigger on AfterUpdate, NOT as an AfterInsert trigger if your Cases are being assigned to a Queue via an Assignment Rule. AfterInsert triggers execute immediately before Assignment Rules fire. So at the point in the execution context where the trigger you've written above executes, any new Case records that are created wouldn't have been assigned to a Queue for your current trigger to find them and reassign them to new owners.

Presumably there would be workflow that occurs on Case that would cause an AfterUpdate trigger to run in the same execution context which would allow your trigger to perform the reassignment. If not, you'll need to create some.

See Triggers and Order of Execution.

2
  • After update will always disallow the use from changing the ownership manually and in this case you're disabling a native Salesforce feature.
    – Templar
    Jul 18, 2016 at 11:51
  • Is there a way for you to change your Case Assignment Rule to pick up this change in moving to LiveHelp such that it assigns them into to your LiveHelp instead? Order of Execution clearly prevents an AfterInsert trigger from doing what you're attempting to do.
    – crmprogdev
    Jul 18, 2016 at 12:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .