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
hoagieryderhoagieryder 

Lookup Field in Visualforce Page

I am trying to insert the Opportunity Owner field in a visualforce page that I have created that is basically a replica of our standard Opportunities Details Page. Right now it is not showing up, what am I doing wrong? 

 

<apex:page standardcontroller="Opportunity" extensions="NewOpportunityCT">
	<apex:sectionHeader title="New Opportunity"/>
	<apex:form >
		<apex:pageBlock title="New Opportunity" mode="edit">
			<apex:pageBlockButtons title="New Opportunity">
				<apex:commandButton value="Save" action="{!save}"/>
				<apex:commandButton value="Cancel" action="{!cancel}"/> 
			</apex:pageBlockButtons>
			<apex:pageBlockSection title="Opportunity Information">
					 <apex:outputField value="{!opportunity.OwnerId}"/>
					 <apex:inputField value="{!opportunity.CloseDate}"/>	 
					 <apex:inputField value="{!opportunity.stagename}"/>
					 <apex:inputField value="{!opportunity.accountid}"/>
					 <apex:inputField value="{!opportunity.probability}"/>
					 <apex:inputField value="{!opportunity.type}"/>
					 <apex:inputField value="{!opportunity.amount}"/>
					 <apex:inputField value="{!opportunity.id}"/>
			</apex:pageBlockSection>
			<apex:pageBlockSection title="Additional Information" columns="1">
					 <apex:inputField value="{!opportunity.leadsource}"/>
					 <apex:inputField value="{!opportunity.nextstep}"/>
			</apex:pageBlockSection>
			<apex:pageBlockSection title="Description Information" columns="1">
					 <apex:inputField value="{!opportunity.description}"/>
					 <apex:inputField value="{!opportunity.Competitors__c}"/>
			</apex:pageBlockSection>
		</apex:pageBlock>
	</apex:form>
</apex:page>

 

 

sales4cesales4ce

if i understood your question correctly, did you pass the Opportunity Id in the Url of your page for to view the record details.

 

Example:

https://www.na1.salesforce.com/apex/yourVfpage?id=OpportunityId

 

Sales4ce

hoagieryderhoagieryder

No, this page is only being displayed when a user clicks the "New" button under opportunities. I overrode the new button because I do not want the Opportunity Name field to be displayed when creating a new opportunity. On the standard new opportunities page, the Opportunity Owner field appears as an output box with the name of the user that created the Opportunity. 

sales4cesales4ce

aah, i know what you are looking for.

Opportunity Owner field has the value of the User name who clicked on the "New" button.

User Object are accessible globally using $ symbol.

 

This example might help you:

 

<apex:page standardController="Opportunity" >
    <apex:form >
        <apex:pageBlock title="New Opportunity">
            <apex:pageBlockButtons location="both">
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>            
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="2">
              <apex:pageBlockSectionItem >
                  <apex:outputLabel > Opportunity Owner</apex:outputLabel>
                  {!$User.FirstName} {!$User.LastName}
              </apex:pageBlockSectionItem>
                
            </apex:pageBlockSection>
        
        </apex:pageBlock>
    </apex:form>
  
</apex:page>

 

 

Sales4ce

hoagieryderhoagieryder

That worked great, thank you so much! I previously got the name to display but was not able to display it in the standard format.

 

Since you were so helpful, I ran into one other problem and was wondering if you had a quick solution. In the new page when I select the Stage dropdown, the Probabilty % field is not generated like it is in the  standard opportunity page. Do I need to use apex:actionSupport to get the percent to generate depending on the dropdown value?

sales4cesales4ce

You should use a controller extension and then in the Visualforce page use the Action Support tag to rerender for event="onchange".

 

This should help you :

 

VF page:

<apex:page standardController="Opportunity" extensions="OpportunityExtension" >
    <apex:form >
        <apex:pageBlock title="New Opportunity" mode="edit" id="thePageBlock">
            <apex:pageBlockButtons location="both">
                <apex:commandButton value="Save" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>            
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="2">
              <apex:pageBlockSectionItem >
                  <apex:outputLabel > Opportunity Owner</apex:outputLabel>
                  {!$User.FirstName} {!$User.LastName}
              </apex:pageBlockSectionItem>
                 
                  <apex:inputField value="{!Oppty.StageName}">
                      <apex:actionSupport event="onchange" reRender="thePageBlock" />
                   </apex:inputField>
                  
                 <apex:pageBlockSectionItem >
                     <apex:outputLabel >Probability (%) </apex:outputLabel>
                      <apex:inputText value="{!Probability}" required="false"/>
                 </apex:pageBlockSectionItem>                                          
               
            </apex:pageBlockSection>
        
        </apex:pageBlock>
    </apex:form>
  
</apex:page>

 

 

Apex Controller:

public class OpportunityExtension {
    Public String probability;
    Public Opportunity oppty {get;set;}
    
    public String getProbability(){
        if(oppty.StageName=='Prospecting' || oppty.StageName=='Qualification')
            return '10';  
        else if(oppty.StageName=='Needs Analysis')
            return '20';   
        else if( oppty.StageName== 'Value Proposition')
            return '50';
         else if( oppty.StageName== 'Id. Decision Makers')
             return '60';
         else if( oppty.StageName== 'closed won')
             return '100';
         else
         return '0';
                    
    }
    
    public Void setProbability(String prob){
        this.probability=prob;
    }
    public OpportunityExtension(ApexPages.StandardController controller) {
        if(oppty==Null){
            oppty=New Opportunity();
        }

    }

}

 

FYI, this approach violates best practises as if at all you want to change the picklistvalues and their corresponding probabilities the code would not work. I would see if anyone might come up with a better approach.

 

I shall keep an eye on this for a better solution.

 

Sales4ce