Thursday, December 22, 2016

Visualforce - Beyond Basics Series 36 - Action for Lookup Dialog

What is Lookup dialog ?

All parent relationship field which is of type lookup can be changed using the standard inputField tag on them.

How can we take action after the Lookup ?

When user selects some value for the parent relationship, we may need to take some action, perhaps a validation (or) confirmation action to the user.


Where it will be useful ?

We could use this for validation purposes or rendering different section of the page based on the value selected, etc.


Where is the Code ?

The code will illustrate how to take action after the lookup value changed & update different section of the page based on the new selected value.
public class testcls36_22122016 {
Opportunity opp;
public testcls36_22122016(ApexPages.StandardController sc) {
opp = (Opportunity) sc.getRecord();
}
public void changeAccount() {
opp.Account.Name = [Select
Id, Name
From
Account
Where
Id =: opp.AccountId
][0].Name;
}
}
view raw gistfile1.txt hosted with ❤ by GitHub
<apex:page standardController="Opportunity" extensions="testcls36_22122016">
<apex:form>
<!-- Action for a change in lookup field -->
<apex:sectionHeader title="Change Account to reflect in Account Details"/>
<apex:outputLabel value="Opportunity Name"/>
<apex:inputField value="{!Opportunity.AccountId}">
<apex:actionSupport event="onchange" action="{!changeAccount}" reRender="AccountName"/>
</apex:inputField>
</apex:form>
<!-- Account Details -->
<apex:sectionHeader title="Account Details"/>
<apex:outputText value="Account Name is {!Opportunity.Account.Name}" id="AccountName"/>
</apex:page>
view raw gistfile1.txt hosted with ❤ by GitHub


Any Screenshots ?


When loaded



After lookup



After value changed



Walk through in a video ?


1 comment:

  1. I am getting this issue

    Attempt to de-reference a null object
    Error is in expression '{!changeAccount}' in page rapidorderentry: Class.ProductController.changeAccount: line 11, column 1

    ReplyDelete

Thanks for reading my blog !