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
Glenn at MvistaGlenn at Mvista 

Reassign case via Apex Trigger on a field update

Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;

for (Lead l:leads){
l.setOptions(dmo);
}
Update leads;

Hi,

 

I have seen lots of code on reassigning Leads that looks like the above.  Is there an equivalent way for cases.  Here is my problem.  We create a case for all unknown contacts who send an email to our support alias, but we store it in a sales queue and create a lead using an Apex trigger.  When sales converts that lead into a contact at some point, we then insert the new ContactId into the original case and would then like to assign the case from the Sales Queue to the appropriate Support Queue (based on the criteria of the Contact etc).

 

This code would be ideal, except that if I change it over to assign a case, like this:

 

 

QueueSobject qId = [select QueueId from QueueSobject where QueueSobject.Queue.Name = 'Pending Sales'];

List<Case> cases = new List<Case>();

    Database.DMLOptions dmo=new Database.DMLOptions();
    dmo.assignmentRuleHeader.UseDefaultRule=True;

    for(Case c:trigger.new){

        if(c.OwnerId == qId.QueueId && c.ContactId != null){

            c.setOptions(dmo);
            cases.add(c);
        }
    }
   update cases;

 

In my before Update trigger I get this error:

 

 

Error: Apex trigger Case_Lead_Reassign caused an unexpected exception, contact your administrator: Case_Lead_Reassign: execution of AfterUpdate

caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.Case_Lead_Reassign: line 18, column 6

 

Is there something wrong?  Can you not do this on Cases like you can with Leads?  Is there a better way to reassign a Case?

 

I tried an after Update trigger, I replaced "update cases;" with "Database.update(cases);" with no luck.

sebcossebcos

Hi,

unfortunately this is not the best fitting use case for DMLOptions.

In a nutshell, the problem is that you are trying to update records that the trigger is processing.

This can normally be done in a before trigger by simply assigning values to the trigger.new elements but you are not allowed to call certain DML operations on the records as per :

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_triggers_context_variables_considerations.htm?SearchType=Stem .

In the above, you will find that in after triggers nothing is allowed .

 

Another option to achieve your requirement is via workflow rules and workflow field updates.  

It requires no Apex code and it is easy to configure.

Simply add a condition for the workflow to fire like this:

(Case: Case Owner EQUALS Pending Sales) AND (Case: Contact Email NOT EQUAL TO null)

And update the owner field with a field upate as required.

I can imagine that you would like to leverage the existing assignment rule but this is currently not possible with workflow but there is an idea you can vote on:

https://sites.secure.force.com/success/ideaView?id=08730000000BrqWAAS

 

If you absolutely need to reuse the assignment rule you might want to look into using a Visualforce solution where you override the update of a case with a different queue using the code you mention above.

Please review this example to see how it can be used:

http://wiki.developerforce.com/index.php/Creating_Custom_Web-To-Case_Forms_Using_Visualforce_and_Sites .


Glenn at MvistaGlenn at Mvista

Thanks sebcos,

 

That is unfortunately what I already suspected, but was hopeful.  I voted for the idea and have my fingers crossed.

 

For the community, my solution, which is quite gnarly, is going to be to insert a new OwnerId on the trigger that inserts the new ContactId.  To do that, I am going to have to code a lot of the assignmnet rule logic into the trigger (e.g., if country = USA then x, else if country = Japan then y, or if email address contains @gmail.com, etc).  Then I will have to monitor any changes to the assignment rules that are made and update the Apex as needed as well.  Oh well, hope they incorporate the idea soon.

 

Thanks again for the help,

Glenn