function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael CarrierMichael Carrier 

Get lead assignment rules to fire from a Visual force page

Hello All,
I am very new to writing code and am having a hard time getting assignment rules to fire once a lead is entered into a visualforce page. I ahve found a few examples out there where folks suggest DML code, however I am not sure if I can put on the Visual Force page or if I should create a seperate class (or if I should create a trigger to fire after the lead is created). Salesfroce suggested I create a class and gave me the following code:
public Lead sample {get; set;}
    public AssignLeadsUsingAssignmentRules(ApexPages.StandardController controller) 
               {
        sample = new Lead();        
    }
   public void LeadAssign()     {             
        Database.DMLOptions dmo = new Database.DMLOptions();             
        dmo.assignmentRuleHeader.assignmentRuleId= '01Q0m0000006EH9';                     
        sample.setOptions(dmo); 
        insert sample; 
    } 
Below is my Visual Force page:
<apex:page standardController="Lead" showHeader="false" sidebar="true" extensions="AssignLeadsUsingAssignmentRules">
<apex:form >
     <apex:pageBlock title="Lead Information">
       <apex:pageBlockSection >
           <apex:pageBlockSection >
           <apex:inputField value="{!Lead.Lead_Referred_By__c}" required="True"/> 
           <apex:inputField value="{!Lead.Club2__c}" required="True"/>
           <apex:inputField value="{!Lead.company}" required="True"/>
           <apex:inputField value="{!Lead.NumberOfEmployees}" required="True"/>
           <apex:inputField value="{!Lead.FirstName}" required="True"/>
           <apex:inputField value="{!Lead.LastName}" required="True"/>
           <apex:inputField value="{!Lead.Phone}" required="True"/>
           <apex:inputField value="{!Lead.Email}" required="False"/>
           <apex:inputField value="{!Lead.Title}" required="True"/>
           <apex:inputField value="{!Lead.Additional_Comments__c}"/>
           </apex:pageBlockSection>
       </apex:pageBlockSection>  
            <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save"/>
                </apex:pageBlockButtons>
</apex:pageBlock>
 </apex:form>
</apex:page>

My issue is that the CLub2__c field is used to dirive a Territory filed on the lead, the assignemnt rules fires off of the Territory field. Any code, tips , suggestions, prayers are greatly appreciated.
Best Answer chosen by Michael Carrier
Abdul KhatriAbdul Khatri
See if these changes help resolve your issue
 
public Lead sample {get; set;}
       
    public AssignLeadsUsingAssignmentRules(ApexPages.StandardController controller) 
    {
        sample = new Lead();        
    }
    
    public PageReference save() {
        Database.DMLOptions dmo = new Database.DMLOptions();             
        dmo.assignmentRuleHeader.assignmentRuleId= '01Q0m0000006EH9';                     
        sample.setOptions(dmo);         
        insert sample;
        return new PageReference('/' + sample.Id);
    }
Visualforce Page changes
<apex:page standardController="Lead" showHeader="false" sidebar="true" extensions="AssignLeadsUsingAssignmentRules">
<apex:form >
     <apex:pageBlock title="Lead Information">
       <apex:pageBlockSection >
           <apex:pageBlockSection >
           <apex:inputField value="{!sample.Lead_Referred_By__c}" required="True"/> 
           <apex:inputField value="{!sample.Club2__c}" required="True"/>
           <apex:inputField value="{!sample.company}" required="True"/>
           <apex:inputField value="{!sample.NumberOfEmployees}" required="True"/>
           <apex:inputField value="{!sample.FirstName}" required="True"/>
           <apex:inputField value="{!sample.LastName}" required="True"/>
           <apex:inputField value="{!sample.Phone}" required="True"/>
           <apex:inputField value="{!sample.Email}" required="False"/>
           <apex:inputField value="{!sample.Title}" required="True"/>
           <apex:inputField value="{!sample.Additional_Comments__c}"/>
           </apex:pageBlockSection>
       </apex:pageBlockSection>  
            <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save"/>
                </apex:pageBlockButtons>
</apex:pageBlock>
 </apex:form>
</apex:page>


 

All Answers

Abdul KhatriAbdul Khatri
What was the point of creating a visualforce page when you can do the same Out of Box Page Layout?
Michael CarrierMichael Carrier
Great question, I don't have a great answer other than this is how it was set up before I was involved. While I have set up a web to lead form through the set up, I still cannot get the assignement rule to function as desired. On this form, the club is a look up field where the user enters in a 5 digit club number, this looks up to the custom object club and brings back the sales territory on the lead record itself. Users who enter leads are our front line personel and do not have access to salesforce.
Abdul KhatriAbdul Khatri
See if these changes help resolve your issue
 
public Lead sample {get; set;}
       
    public AssignLeadsUsingAssignmentRules(ApexPages.StandardController controller) 
    {
        sample = new Lead();        
    }
    
    public PageReference save() {
        Database.DMLOptions dmo = new Database.DMLOptions();             
        dmo.assignmentRuleHeader.assignmentRuleId= '01Q0m0000006EH9';                     
        sample.setOptions(dmo);         
        insert sample;
        return new PageReference('/' + sample.Id);
    }
Visualforce Page changes
<apex:page standardController="Lead" showHeader="false" sidebar="true" extensions="AssignLeadsUsingAssignmentRules">
<apex:form >
     <apex:pageBlock title="Lead Information">
       <apex:pageBlockSection >
           <apex:pageBlockSection >
           <apex:inputField value="{!sample.Lead_Referred_By__c}" required="True"/> 
           <apex:inputField value="{!sample.Club2__c}" required="True"/>
           <apex:inputField value="{!sample.company}" required="True"/>
           <apex:inputField value="{!sample.NumberOfEmployees}" required="True"/>
           <apex:inputField value="{!sample.FirstName}" required="True"/>
           <apex:inputField value="{!sample.LastName}" required="True"/>
           <apex:inputField value="{!sample.Phone}" required="True"/>
           <apex:inputField value="{!sample.Email}" required="False"/>
           <apex:inputField value="{!sample.Title}" required="True"/>
           <apex:inputField value="{!sample.Additional_Comments__c}"/>
           </apex:pageBlockSection>
       </apex:pageBlockSection>  
            <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save"/>
                </apex:pageBlockButtons>
</apex:pageBlock>
 </apex:form>
</apex:page>


 
This was selected as the best answer
Abdul KhatriAbdul Khatri
Is this helped?
Michael CarrierMichael Carrier
Adbul, first accept my apologies, I did not see that you posted the solution a couple days ago. This worked great and solved my issues. I appreciate your help!