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
DannyTKDannyTK 

Trigger to Save new Cases for Assignment rule

Good afternoon, 

I'm working on a simple trigger that can save new cases generated by Process Builder in order for them to hit the assignment rules (Cases created via Process builder will not hit the assignment rules until the Case is Edited and Saved again), and have the following:

trigger caseUpdate on Case (after insert) {
List <Case> newCases = new List <Case>();
    for (Case ticket : Trigger.new) {
        ticket.Status = 'Pending Customer Information';        
    }
    update newCases;
    }

Can someone take a look to see if I missed anything, i'm not a developer so wanted to get a more experience eye on this to validate that it looks okay. 

Thanks community
-danny
Best Answer chosen by DannyTK
Vivek DeshmaneVivek Deshmane
Hi Danny,
Please try below code and let me know if it works.

trigger caseUpdate on Case (before insert) {

  if(Trigger.isInsert && Trigger.isBefore){ 
   for (Case ticket : Trigger.new) 
   {
        ticket.Status = 'Pending Customer Information'; 
        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.assignmentRuleHeader.useDefaultRule= true;
        ticket.setOptions(dmo);        
    }
  }
    
Best Regards,
-Vivek

All Answers

Vivek DeshmaneVivek Deshmane
Hi Danny,
Please try below code and let me know if it works.

trigger caseUpdate on Case (before insert) {

  if(Trigger.isInsert && Trigger.isBefore){ 
   for (Case ticket : Trigger.new) 
   {
        ticket.Status = 'Pending Customer Information'; 
        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.assignmentRuleHeader.useDefaultRule= true;
        ticket.setOptions(dmo);        
    }
  }
    
Best Regards,
-Vivek
This was selected as the best answer
DannyTKDannyTK
Thanks for the response Vivek, 

doesn't seem like it worked, i'm wondering if it should be after insert.  I'll keep testing, let me know if you have any other suggestions as well...I appreciate the help.

-danny
DannyTKDannyTK
hey Vivek,

your trigger was slightly modified and it's working now, thanks for your help:

trigger caseUpdate on Case (after insert) 
{
  List<Case> caselist = new List<Case>();

  if(Trigger.isInsert && Trigger.isafter)
  { 
   for (Case ticket: Trigger.new) 
   {
       caselist.add(new Case(id = ticket.id));
        if(ticket.Status == 'Pending Customer Information')
        {        
        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.assignmentRuleHeader.useDefaultRule= true;
        //ticket.setOptions(dmo); 
        Database.update(caselist,dmo);  
        }     
    }
  }
}