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
Vikas Kumar 135Vikas Kumar 135 

Auto Populate field based on Input from another field on Visualforce Page

Hi Friends,

On Opportunity, I have two fields. One is LOB (Line of Business), which is a Picklist field (With Values like Managed Services, Enterprise, Partner) another Field (Rate Card) is a lookup field.

I want to Auto-populate the Rate Card Field based on the value we select for the LOB, for example, if I select "Managed Services" as LOB then I want "Rate Card X" to appear in Rate Card field. 

if I select "Enterprise" as LOB then I want "Rate Card Y" to appear in Rate Card field. 


Also, I want to Opportunity Name to say "Auto Populate-Please do not fill". 

 
I want to achieve two things.

1) Opportunity Name to reflect- (Auto Created) for all the Opportunities.
2) Based on what we select in Line Of Business, I want Rate Card to get populated on UI.

User-added image


Please let me know if this we can achieve using a Visualforce page?

Regards,
Vikas

 
Pradeep SinghPradeep Singh
Hi, Yes we can do this using VF page and apex controller.

Refer below code:-
<apex:page controller="OppAutofill">
    <apex:form>
    <apex:pageBlock>
        <apex:pageBlockSection>
            <apex:inputField value="{!opp.name}"/>  
            <apex:pageBlockSectionItem > 
                <apex:outputLabel value="Forecast Category" for="forecast"/>
                <apex:actionRegion>  
                    <apex:inputField value="{!opp.ForecastCategoryName}" id="forecast" >
                        <apex:actionSupport action="{!changeAcc}" event="onchange" reRender="abc" />
                    </apex:inputField>
                </apex:actionRegion>
            </apex:pageBlockSectionItem>
            <apex:inputField value="{!opp.CloseDate}"/>
            <apex:inputField value="{!opp.StageName}"/>
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="AccountName" for="acc"/>                    
                <apex:outputPanel id="abc">
                    <apex:inputField value="{!opp.AccountId}" id="acc"/>
                </apex:outputPanel>
            </apex:pageBlockSectionItem>            
        </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
</apex:page>

------------------------------------------------------------------------------
public class OppAutofill{
    public opportunity opp{get;set;}
    
    public OppAutofill(){
        opp = new opportunity ();
        opp.name = 'test opp';
    }
    
    public void changeAcc(){
        if(opp.ForecastCategoryName == 'Pipeline')
        opp.AccountID = [select id,name from account][0].id;        
    }
}

This is just an example, please modify it according to your requirements.

If this solves your issue, please mark it as best answer.