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
KenAllredKenAllred 

Override standard page & auto assigning UserID to OwnerID

I'm in the process of overriding our "new" button functionality for when our users create a new lead. One of the things that I lost (I think) as I was doing this was having the User ID automatically assigned to the Owner ID. I'd like to also allow the user to change this (e.g. they're creating a lead, but need to assign it to someone else) as this situation happens at times.

 

I've done some Google searching to figure out if I can set the value for a field, but I haven't been able to find any examples (poor Google skills, I'm thinking).

 

Could anyone help me with this?

 

This is what I have so far for my page:

 

<apex:page standardController="Lead" tabStyle="Lead" showHeader="true" sidebar="true">

	<apex:form>

		<apex:sectionHeader title="Lead Edit" subtitle="{!if(Lead.Id==null,'New Lead',Lead.Name)}">

			<apex:pageBlock mode="maindetail" id="myLeadInstructions" title="Lead Instructions">
			
				I'll put my Lead Rules information here in standard HTML.

			</apex:pageBlock>

			<apex:pageBlock mode="edit" id="leadEditBlock" title="Lead Edit">

				<apex:pageBlockButtons>
					<apex:commandButton action="{!save}" value="Save"></apex:commandButton>
					<apex:commandButton action="{!cancel}" value="Cancel"></apex:commandButton>
				</apex:pageBlockButtons>

				<apex:pageMessages></apex:pageMessages>

				<apex:pageBlockSection id="leadInfoBlock" title="Lead Information">

					<apex:inputField value="{!Lead.OwnerID}"></apex:inputField>
					<apex:inputField value="{!Lead.Phone}"></apex:inputField>

					<apex:pageBlockSectionItem>

						<apex:outputLabel value="{!$ObjectType.Lead.Fields.FirstName.label}"></apex:outputLabel>

						<apex:outputPanel>

							<apex:inputField value="{!Lead.Salutation}"></apex:inputField>
							<apex:inputField value="{!Lead.FirstName}"></apex:inputField>

						</apex:outputPanel>

					</apex:pageBlockSectionItem>

					<apex:inputField value="{!Lead.MobilePhone}"></apex:inputField>
					<apex:inputField value="{!Lead.LastName}"></apex:inputField>
					<apex:inputField value="{!Lead.Company}"></apex:inputField>
					<apex:inputField value="{!Lead.Email}"></apex:inputField>
					<apex:inputField value="{!Lead.Title}"></apex:inputField>
					<apex:inputField value="{!Lead.Website}"></apex:inputField>
					<apex:inputField value="{!Lead.Leadsource}"></apex:inputField>
					<apex:inputField value="{!Lead.Status}"></apex:inputField>
					<apex:inputField value="{!Lead.Industry}"></apex:inputField>
					<apex:inputField value="{!Lead.NumberOfEmployees}"></apex:inputField>

				</apex:pageBlockSection>				

			</apex:pageBlock>

		</apex:sectionHeader>

	</apex:form>
	
</apex:page>

 Thanks for your help!

Best Answer chosen by Admin (Salesforce Developers) 
KenAllredKenAllred

Thanks for you help. I actually ended up writing an extension controller for my Lead controller with the following:

 

public with sharing class cntrSetLeadOwner {

	private final Lead myLead{get; set;} 

    public cntrSetLeadOwner(ApexPages.StandardController stdController) {
        this.myLead = (Lead)stdController.getRecord();
        myLead.OwnerId = UserInfo.getUserId();        
    }

}

 

And this did the trick for me. Thanks for the help though!

All Answers

asish1989asish1989

When a lead is created, user will be allowed to change the Owner of the lead.

 is it your requirement?

 

While creating lead you can assign to other user, a lookup filed is there Lead Owner .

Also after saving  there is a link change  already present (By salesforce), click on that to change the owner of lead

KenAllredKenAllred
The code above allows me to change the lead owner, which is good. The part that is missing is populating the Owner field with the user that clicked the "new lead" button. Most of the time the user that creates the lead will be the lead owner, which is why I would like the Owner field editable, but automatically populated with the user's ID that is creating the new lead.
KenAllredKenAllred
This is true for the standard page, but not true for the new VF page I have created above and I need to override the standard page.
asish1989asish1989

Can you please try this and let me know if it works for you or not 

<apex:page standardController="Lead" tabStyle="Lead" showHeader="true" sidebar="true" extensions="LeadOwnerChangeExtension">

    <apex:form >

        <apex:sectionHeader title="Lead Edit" subtitle="{!if(Lead.Id==null,'New Lead',Lead.Name)}">

            <apex:pageBlock mode="maindetail" id="myLeadInstructions" title="Lead Instructions">
            
                I'll put my Lead Rules information here in standard HTML.

            </apex:pageBlock>

            <apex:pageBlock mode="edit" id="leadEditBlock" title="Lead Edit">

                <apex:pageBlockButtons >
                    <apex:commandButton action="{!save}" value="Save"></apex:commandButton>
                    <apex:commandButton action="{!cancel}" value="Cancel"></apex:commandButton>
                </apex:pageBlockButtons>

                <apex:pageMessages ></apex:pageMessages>

                <apex:pageBlockSection id="leadInfoBlock" title="Lead Information">

                    <apex:inputField value="{!lead.OwnerID}"></apex:inputField>
                    <apex:inputField value="{!lead.Phone}"></apex:inputField>

                    <apex:pageBlockSectionItem >

                        <apex:outputLabel value="{!$ObjectType.Lead.Fields.FirstName.label}"></apex:outputLabel>

                        <apex:outputPanel >

                            <apex:inputField value="{!lead.Salutation}"></apex:inputField>
                            <apex:inputField value="{!lead.FirstName}"></apex:inputField>

                        </apex:outputPanel>

                    </apex:pageBlockSectionItem>

                    <apex:inputField value="{!lead.MobilePhone}"></apex:inputField>
                    <apex:inputField value="{!lead.LastName}"></apex:inputField>
                    <apex:inputField value="{!lead.Company}"></apex:inputField>
                    <apex:inputField value="{!lead.Email}"></apex:inputField>
                    <apex:inputField value="{!lead.Title}"></apex:inputField>
                    <apex:inputField value="{!lead.Website}"></apex:inputField>
                    <apex:inputField value="{!lead.Leadsource}"></apex:inputField>
                    <apex:inputField value="{!lead.Status}"></apex:inputField>
                    <apex:inputField value="{!lead.Industry}"></apex:inputField>
                    <apex:inputField value="{!lead.NumberOfEmployees}"></apex:inputField>

                </apex:pageBlockSection>                

            </apex:pageBlock>

        </apex:sectionHeader>

    </apex:form>
    
</apex:page>

 

public class LeadOwnerChangeExtension{
    
    public Lead lead{get;set;}
    public User user{get;set;}
   
    public LeadOwnerChangeExtension(ApexPages.StandardController controller){
    
        lead = new Lead();
        
        //String str = String.valuof(getUserId());
        user = [SELECT Id,Name FROM User WHERE id=:UserInfo.getUserId()];
        lead.OwnerID = user.id;
    
    }
    
    public PageRefeRence save(){
        upsert lead;
        PageRefeRence samepage = new PageRefeRence('/' +lead.id);
        return samepage;
    }
}

 

KenAllredKenAllred

Thanks for you help. I actually ended up writing an extension controller for my Lead controller with the following:

 

public with sharing class cntrSetLeadOwner {

	private final Lead myLead{get; set;} 

    public cntrSetLeadOwner(ApexPages.StandardController stdController) {
        this.myLead = (Lead)stdController.getRecord();
        myLead.OwnerId = UserInfo.getUserId();        
    }

}

 

And this did the trick for me. Thanks for the help though!

This was selected as the best answer