7

I created a visualforce "web-to-lead" form that creates a lead and spits back out who the lead was assigned to (owner) on a Thank You page. My problem is that it will not let me activate the Lead Assignment rules. I have written a trigger to run the assignment rules but for whatever reason it's not working. I know that all the code has run in the trigger as well through debugging because it wrote a value to a field.

Trigger

trigger LeadAsnTrigger on Lead (before update) 
{
    for (Lead newLead : Trigger.new)
      if (newLead.WF_Lead_Owner__c != NULL)
      { 
          if(newLead.WF_Lead_Owner__c == 'Unknown'){

                //Fetching the assignment rules on case
                AssignmentRule AR = new AssignmentRule();
                AR = [select id from AssignmentRule where SobjectType = 'Lead' and Active = true limit 1];

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

                dmlOpts.assignmentRuleHeader.useDefaultRule = true; 

                newLead.setOptions(dmlOpts);

                newLead.web2leadTriggerHiddenCondition__c = 'CREATED';
          }



      }   
}

VF Page

<apex:page standardController="Lead"
           extensions="myWeb2LeadExtension"
           title="Contact Us" showHeader="false"
           standardStylesheets="true">
 <apex:composition template="{!$Site.Template}">
  <apex:define name="body">
   <apex:form >
    <apex:messages id="error"
                   styleClass="errorMsg"
                   layout="table"
                   style="margin-top:1em;"/>
      <apex:pageBlock title="" mode="edit">
        <apex:pageBlockButtons >
           <apex:commandButton value="Save"
                               action="{!saveLead}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="Contact Us"
                               collapsible="false"
                               columns="1">
         <apex:inputField value="{!Lead.Salutation}"/>
         <apex:inputField value="{!Lead.Title}"/>
         <apex:inputField value="{!Lead.FirstName}"/>
         <apex:inputField value="{!Lead.LastName}"/>
         <apex:inputField value="{!Lead.Email}"/>
         <apex:inputField value="{!Lead.Phone}"/>
         <apex:inputField value="{!Lead.Company}"/>
         <apex:inputField value="{!Lead.Street_Name__c}"/>
         <apex:inputField value="{!Lead.City__c}"/>
         <apex:inputField value="{!Lead.State_Province__c}"/>
         <apex:inputField value="{!Lead.Zip_Postal_Code__c}"/>
         <apex:inputField value="{!Lead.Country__c}"/>
        </apex:pageBlockSection>
     </apex:pageBlock>
   </apex:form>
  </apex:define> 
 </apex:composition>
</apex:page>

VF Page controller

public class myWeb2LeadExtension {

    private Lead weblead;

    public myWeb2LeadExtension(ApexPages.StandardController
                                stdController) {
       weblead = (Lead)stdController.getRecord();
       weblead.WF_Lead_Owner__c  = 'Unknown';



    }

     public PageReference saveLead() {
       try {
       insert(weblead);
       }
       catch(System.DMLException e) {
           ApexPages.addMessages(e);
           return null;
       }
       PageReference p = Page.ThankYou;
       p.setRedirect(true);
       p.getParameters().put('id',weblead.id);
       p.getParameters().put('ownerid',weblead.ownerid);
       return p;
     }

     public Lead getLead() {
        if(weblead == null) 
            weblead = [select id, ownerId from Lead 
                       where id = :ApexPages.currentPage().getParameters().get('id')];
        return weblead; 
    } 
}
2
  • You could remove the trigger altogether and add the insert/dml options logic in the controller instead as BrittishBoy suggested. Also, that query for the assignment rule doesn't serve any purpose as you are using the default rule. – PepeFloyd Feb 15 '14 at 0:53
  • Can you check if there are any WF rules or triggers that are causing an update to the lead ? I checked it in my org and its working fine. Debug logs is the best way to find the problem. – Keepcalmncode Feb 21 '14 at 8:26
6

I don't know this for sure, but I would think the DML options need to be set before you insert the record. I would move this code:

EDIT - So the code below works in my environment with an active rule using the standard country field to assign the lead - so I think it might be an issue with your assignment rule, or with some other workflow/trigger interfering?

public class myWeb2LeadExtension {

    private final Lead weblead;

    public myWeb2LeadExtension(ApexPages.StandardController stdController) {
       weblead = (Lead)stdController.getRecord();
       weblead.WF_Lead_Owner__c  = 'Unknown';
    }

     public PageReference saveLead() {
       try {
        Database.DMLOptions dmlOpts = new Database.DMLOptions();
        dmlOpts.assignmentRuleHeader.useDefaultRule = true; 
        weblead.setOptions(dmlOpts);
        update weblead;
        }
       catch(System.DMLException e) {
           ApexPages.addMessages(e);
           return null;
       }
       PageReference p = Page.ThankYou;
       p.setRedirect(true);
       p.getParameters().put('id',weblead.id);
       p.getParameters().put('ownerid',weblead.ownerid);
       return p;
     }

}

Page:

<apex:page standardController="Lead" extensions="myWeb2LeadExtension" title="Contact Us" showHeader="false">
   <apex:form >
    <apex:messages id="error"
                   styleClass="errorMsg"
                   layout="table"
                   style="margin-top:1em;"/>
      <apex:pageBlock title="" mode="edit">
        <apex:pageBlockButtons >
           <apex:commandButton value="Save" action="{!saveLead}"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="Contact Us"
                               collapsible="false"
                               columns="1">
         <apex:inputField value="{!Lead.Salutation}"/>
         <apex:inputField value="{!Lead.Title}"/>
         <apex:inputField value="{!Lead.FirstName}"/>
         <apex:inputField value="{!Lead.LastName}"/>
         <apex:inputField value="{!Lead.Email}"/>
         <apex:inputField value="{!Lead.Phone}"/>
         <apex:inputField value="{!Lead.Company}"/>
         <apex:inputField value="{!Lead.Country}"/>
        </apex:pageBlockSection>
     </apex:pageBlock>
   </apex:form>
</apex:page>
1
  • I tried that. bypassed the trigger and just set my controller like you mentioned. Weird, it's still not working. It's not grabbing the new owner. I wonder what could be doing this – user988 Feb 17 '14 at 18:16
3
+25

Can you see the default Lead Assignment Rule being applied in the debug log? It will appear against the WF_CRITERIA_BEGIN Event and the Id of the Lead Assignment Rule will be in the Message column (with the 01Q key prefix).

The following minimal code can be run as Anonymous Apex to inspect the log and see the assignment rule being applied.

Database.DMLOptions dmlOpts = new Database.DMLOptions();
dmlOpts.assignmentRuleHeader.useDefaultRule = true;

Lead testLead = new Lead();
testLead.Company = 'test Company';
testLead.LastName = 'test LastName';

testLead.setOptions(dmlOpts);

insert testLead;

E.g. I grabbed my Lead Assignment Rule Id from the query string in the Salesforce UI. - 01Q70000000IWrY

Lead Assignment Rule being applied in the debug log

You can also see the optional email template being applied here with the 00X key prefix.


Does your Lead Assignment Rule have any criteria that might prevent it from being applied? Check under the Rule Entries.

0

Whenever you create a lead from page layout, you need to edit the properties of layout as Lead Assignment Checkbox - Default [ alone not Show on edit page ] and whenever you create a lead through code, you need to this just before you insert or update lead record.

Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;
Database.update(Lead Record, dmo);

However, Make sure, Lead assignment rule is active and given conditions are correct and the order is as it is supposed.

This will work in all the cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.