You need to sign in to do that
Don't have an account?

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
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
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
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);
}
It works..thank u so much :)
I am using this code for my leads. How can we say only to file if lead source = 'Web'.
Thanks in advance.
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