Activity Forums Salesforce® Discussions How To Force Lead Assignment Rule Via Apex While Updating Or Adding The Lead?

  • How To Force Lead Assignment Rule Via Apex While Updating Or Adding The Lead?

    Posted by Aman on September 20, 2018 at 2:58 PM

    How To Force Lead Assignment Rule Via Apex While Updating Or Adding The Lead?

    Parul replied 7 years, 4 months ago 3 Members · 2 Replies
  • 2 Replies
  • shariq

    Member
    September 20, 2018 at 2:59 PM

    Hi,

    To enforce Assignment Rules in Apex you will need to perform following steps:

    1. Instantiate the “Database.DMLOptions” class.
    2. Set the “useDefaultRule” property of “assignmentRuleHeader” to True.
    3. Finally call a native method on your Lead called “setOptions”, with the Database.DMLOptions instance as the argument.

    // to turn ON the Assignment Rules in Apex

    Database.DMLOptions dmlOptn = new Database.DMLOptions();

    dmlOptn.assignmentRuleHeader.useDefaultRule = true;
    leadObj.setOptions(dmlOptn);

    Thanks

  • Parul

    Member
    September 20, 2018 at 4:55 PM

    Adding some code snippet:

    AssignmentRule AR = new AssignmentRule();
    AR = [select id from AssignmentRule where SobjectType = 'Lead' 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;

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

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos

Salesforce Admin Certification 2020 Questions Explained with References - Part II

Video in

How to prepare and pass Salesforce Admin Certification with Practice Exam Questions 2020 practice questions and answers for the Salesforce Administration Salesforce Certified Admin Exam Questions Salesforce Administrator Certification Exam…