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
Aishwarya_TodkarAishwarya_Todkar 

Can't find a way of how to test case owner changed to queue

Hi,
I have created a case assignment rule where whenever case of specific record type  is created it is assigned to a Queue.
But I'm not able to test it in test class. Kindly help me with the solution for this.
Best Answer chosen by Aishwarya_Todkar
Pavit SiddhuPavit Siddhu
Hello, try this
 If you set the DMLOptions to use the case assignment rules before an insert or update then the case assignment rules will fire during tests.
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;
theCase.setOptions(dmo);
insert theCase;

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved.

Thanks and Regards
Pavit Siddhu

All Answers

Pavit SiddhuPavit Siddhu
Hello
Aishwarya27
I think you are asking about to use assignment rule by apex.
A solution is using Database.DMLOptions. The Database.DMLOptions class can provide extra information during a transaction; for example, specifying the truncation behaviour of fields or assignment rule information. For example, the script below is fetching the assignment rules of Case and then creating the DMLOptions for the "Assign using active assignment rules" checkbox.
//Fetching the assignment rules on case
AssignmentRule AR = new AssignmentRule();
AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];

//Creating the DMLOptions for "Assign using active assignment rules" checkbox
Database.DMLOptions dmlOpts = new Database.DMLOptions();
dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;

Case newCase = new Case(Status = 'New') ;
//Setting the DMLOption on Case instance
newCase.setOptions(dmlOpts);
insert newCase ;


Now when the Case is inserted using this script, the assignment rules get triggered.
Aishwarya_TodkarAishwarya_Todkar
Thanks @Pavit Siddhu,
I tried this before, but facing same issue.
I am getting the expected results in debugs If I used debugs only without adding asserts. But when I added both debugs and asserts then asserts are failed and debugs are unexpected, getting null values.
Pavit SiddhuPavit Siddhu
Hello, try this
 If you set the DMLOptions to use the case assignment rules before an insert or update then the case assignment rules will fire during tests.
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;
theCase.setOptions(dmo);
insert theCase;

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved.

Thanks and Regards
Pavit Siddhu
This was selected as the best answer
Aishwarya_TodkarAishwarya_Todkar
Thanks @Pavit Siddhu, I will be posting a new question please help me out with that also.