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 4 years 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