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
ShrutiShruti 

Case Assignment Rule from trigger

Hi,

 

I want to fire  Assignment rule when a case is created via Web Service in Salesforce.

I tried using DML Options but getting the following error:

 

"Execution of AfterInsert caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old:"

 

I am using an after insert trigger..

Please help me.. Its urgent

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

Hey Shruti,
This should do it. Select and update rather than update the references in trigger.new

 

trigger test on Case (after insert) {
  
List<Id> caseIds = new List<Id>{};

        for (Case theCase:trigger.new) 
            caseIds.add(theCase.Id);
        
        List<Case> cases = new List<Case>{}; 
        for(Case c : [Select Id from Case where Id in :caseIds])
        {
            Database.DMLOptions dmo = new Database.DMLOptions();
 
            dmo.assignmentRuleHeader.useDefaultRule = true;
            c.setOptions(dmo);
            
            cases.add(c);
        }

        Database.upsert(cases);
}

All Answers

Prafull G.Prafull G.
Can you post your code ?
ShrutiShruti

The code is:

 

trigger test on Case (after insert) {
   
        for (Case theCase:trigger.new) {
          
                Database.DMLOptions dmo = new Database.DMLOptions();
 
                dmo.assignmentRuleHeader.useDefaultRule = true;
                theCase.setOptions(dmo);
                Database.update(theCase);    
                 
      
    }

 

 

}

 

 

It works if I m updating the case using @ future.. But I don want to use @ future method.

 

I referrred to this post in community for writing the code:

http://boards.developerforce.com/t5/Apex-Code-Development/Trigger-to-Fire-Lead-Assignment-Rules-Does-not-Fire-them/m-p/150985#M21224

Ritesh AswaneyRitesh Aswaney

Hey Shruti,
This should do it. Select and update rather than update the references in trigger.new

 

trigger test on Case (after insert) {
  
List<Id> caseIds = new List<Id>{};

        for (Case theCase:trigger.new) 
            caseIds.add(theCase.Id);
        
        List<Case> cases = new List<Case>{}; 
        for(Case c : [Select Id from Case where Id in :caseIds])
        {
            Database.DMLOptions dmo = new Database.DMLOptions();
 
            dmo.assignmentRuleHeader.useDefaultRule = true;
            c.setOptions(dmo);
            
            cases.add(c);
        }

        Database.upsert(cases);
}

This was selected as the best answer
ShrutiShruti

It works..thank u so much :)

AdnanAdnan

I am using this code for my leads. How can we say only to file if lead source = 'Web'.

 

Thanks in advance.

cedgcedg

Hi,

 

is it possible to do the same on the update event ?

I tried but it seems to provoke an infinite loop, even if using a global class with a boolean to prevent the trigger firing twice.

 

Thanks

Ced

Diego Gutierrez CodinaDiego Gutierrez Codina
Thanks Ritesh, the solution you provided works great for me! ;-)

Diego