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
Michael Foster 5Michael Foster 5 

Challenged Invoking Lead Assignment Rule From Apex Code

Invoking Lead Assignment Rule From Apex Code

I am not having success. Can anyone help. I am an admin. Not a developer. New to APEX. I am running a app with an Apex Class Email Processor. Do I plug the code in the existing Apex Class Email Processor or create a new Apex Class? Do I use an existing or create a Visual Force page? Other? I tried adding to existing and the assignment rule is not working. I tried creating a new Apex Class, the code as is is not accepted. I also created a Visual Force page and the rule does not trigger. 

//Fetching the assignment rules on case
02AssignmentRule AR = new AssignmentRule();
03AR = [select id from AssignmentRule where SobjectType = 'Case' and Active = true limit 1];
04 
05//Creating the DMLOptions for "Assign using active assignment rules" checkbox
06Database.DMLOptions dmlOpts = new Database.DMLOptions();
07dmlOpts.assignmentRuleHeader.assignmentRuleId= AR.id;
08 
09Case newCase = new Case(Status = 'New') ;
10//Setting the DMLOption on Case instance
11newCase.setOptions(dmlOpts);
12insert newCase ;
AmulAmul
http://developer.force.com/cookbook/recipe/running-case-assignment-rules-from-apex


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



Use below trigger to fire the assignment rule on case creation via API:

trigger CaseAssignment on Case (after insert) {
    //Variable decleration
    List<Case> caseList = new List<Case>();
    User integrationUserObj = new User();
   
    if(Trigger.isAfter && Trigger.isInsert){
        //Fetching the integration user details
        integrationUserObj = [SELECT Id, Name FROM User where Name = 'Integration User' LIMIT 1];
           
        for (Case caseObj : Trigger.new) {
            if (caseObj.OwnerId  == integrationUserObj.Id) {
                caseList.add(new Case(id = caseObj.id));
            }
        }
   
        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.assignmentRuleHeader.useDefaultRule = true;
        Database.update(caseList, dmo);
    }
}
Michael Foster 5Michael Foster 5
Thank you. Sorry I wasnt clear. The goal is to tigger Lead assignment. Do I need to transcribe and change each instance of Case to Lead or case to lead? 
Michael Foster 5Michael Foster 5
Thanks again. Should I be creating an Apex class and Trigger. When creating the class I get the following error Error: Compile Error: unexpected token: 'AssignmentRule' at line 2 column 0 for: 

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

 
KAVITHA TUMMALAKAVITHA TUMMALA
Hi Michael, I have run into same issue as you. Can you please let me know the procedure you followed to fix the issue. Thanks.