Running Case/Lead assignment rules while inserting a case from Apex.
Note: In this example we deal with Case

For Triggers

From a standard UI, a user can trigger assignment rules by simply checking the "Assign using active assignment rules" checkbox under the Optional section.


//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();

Case newCase = new Case(Status = 'New', Subject="Dummy Subject") ;
//Setting the DMLOption on Case instance
insert newCase ;

Now Case Assignment rules will be triggered

Happy coding! Please comment for any help