2

In my org we are getting receiving leads from a third party site, for which we have written a webservice class. But when a new lead is created through this webservice Lead assignement rule is not enforced. I googled a bit and found way to enforce these rules using After Insert Trigger, but i want to enforce them in webserive class as soon as lead is created.

Thanks for your time!

2 Answers 2

3

From the webservice class if you run this type of logic, it will enforce the Lead assignment rule.

List<AssignmentRule> lstAssignmentRule 
    = [SELECT id 
        FROM AssignmentRule 
    WHERE SobjectType = 'Lead' 
    AND Active = true LIMIT 1];

AssignmentRule objAssignmentRule = new AssignmentRule();
Database.DMLOptions dmlOpts = new Database.DMLOptions();
if(lstAssignmentRule.size() > 0)
{
    objAssignmentRule = lstAssignmentRule[0];
    dmlOpts.assignmentRuleHeader.assignmentRuleId= objAssignmentRule.id;
}
List<Lead> lstLead = new List<Lead>();
Lead objLead = new Lead();
objLead.status = 'New';
//assign other attributes

objLead.setOptions(dmlOpts);
lstLead.add(objLead);
insert lstLead;
7
  • 1
    Thanks....worked like charm....! You just saved lot of time for me. Commented Dec 18, 2017 at 15:16
  • Alternatively: dmlOpts.assignmentRuleHeader.useDefaultRule = true;.
    – sfdcfox
    Commented Dec 18, 2017 at 18:33
  • @sfdcfox, yes that is correct, sometimes if Admin doesn't mark as default then possibly my approach will work. Secondly in the page layout settings, Admin has to mark assignment rule as default. Commented Dec 18, 2017 at 18:35
  • 1
    Yeah. I just don't feel like one should be selecting a random rule. You should always know which rule you want to use.
    – sfdcfox
    Commented Dec 18, 2017 at 18:36
  • @sfdcfox, this is not random as only one assignment rule can be active. We cannot make multiple rules to be activated, so my code will work perfectly. Commented Dec 19, 2017 at 4:04
3

If you were using the SOAP API, the AssignmentRuleHeader is availiable in order include it in your HTTP call, but since you're using the REST API, then you have to enforce it manually in APEX.

With the following code you can apply the rule in the webservice:

Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;
theLead.setOptions(dmo);

Nonetheless, depending on your organization, it might be more convenient to include it in the very endpoint you developed or actually in the lead trigger. If you're going to have to do this in multiple web services, then to avoid code duplication I'd just include it in a trigger. That way you know it will be done always regardless where the lead comes from.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .