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