Skip to main content

Solution for ActionSupport on Lookups for a Visualforce Page

Hello there, There are two reasons why you are on this page, one is that you like to go through Force.com blogs and gain as much knowledge as you can. Its a good thing, only you won't gain any knowledge reading the blogs but by digging. Two, you are highly frustrated and have Googled extensively for keywords 'ActionSupport', 'Lookup' and 'Visualforce' for many hours and before that were struggling with ActionSupport, onblur, onChange and other events.

If you are here for second reason, we both sail in the same ship and even though I may sound like a telemarketer who is keen on wasting your time, I however, would like to inform you that I am sympathetic towards you. After having struggling to get this thing work on a Lookup for a long time, I decided to embark on the journey that took me back in time even before Salesforce.com existed. I went to the very depths of DOM and source code. Right to the birth place... ok not really, but I am just back from Star Wars Movie Marathon and still a bit woozie.

So anyways yes, the solution.

The code:
This is how your code will be looking right now:


<apex:pageBlockSection >
<apex:inputField value="{!someLookup__c}" >
<apex:actionSupport event="onChange" action="{!SomeAction}" rerender="thePageBlock" status="theStatus"/>         
</apex:inputField>
</apex:pageBlockSection>
<apex:pageBlock id="thePageBlock">
      //Something cool here.
</apex:pageBlock>  

So we need to do a bit of tweaking for this code and use the Big brother of ActionSupport, change the code to this

Corrected Code:
<apex:pageBlockSection >
        <apex:inputField value="{!someLookup__c}" onChange="someJs()"/>
<apex:actionFunction name="someJs" action="{!SomeAction}" rerender="thePageBlock" status="theStatus"/>    
         </apex:pageBlockSection>
<apex:pageBlock id="thePageBlock">
      Something cool here.
</apex:pageBlock>  

Obviously this is a workaround for the problem. There are some questions in the community as is this a bug in Salesforce? I don't think so.
Basically this has something to do with very primitive that is, native HTML. See lookup may be easy to build and render, but deep down it converts itself into a HTML component. In HTML it is split into two different components, one input field and another hidden field. As this is a complex rendering in itself, adding another event to it using actionSupport would be much tedious task.

If they do, obviously, its awesome.

Whats more?

Instead of actionFuction we can also call a native javascript code for the same. Well I changed my mood from aaarggh to Wuuu huuu.

Hope it makes you the happy as well. 

Toodles for now,
Siddhesh


Comments

Post a Comment

Popular posts from this blog

Video: Top ten tips on how not to ruin Salesforce implementation

Another presentation for the London Salesforce Developer Group using mobile app. 
When it comes to Salesforce development, we all know what it takes to deliver stuff. Having a certified professional helps you gain a knowledgeable partner but what about the things such as common sense?  We all know how to deliver things on client requirements but what about things that are not mentioned by the client. We will be discussing in details things that will enhance the user experience, focus on tips to read your customers mind and have a bit of perspective on the user experience.

Cache me if you can: What you should know before daring to set URL parameter on visualforce

If someone gave me a pence for every time there was an SOQL query in an APEX Class without using Limit or a condition during a code review, I could afford a Lamborgini this month. Sigh. If only. We make it a habit of going digging for data, at the very moment we need it. The crux of this problem happens when you have chain classes which are independent of each other. Each class needs the reference from a single record and we have to query for that record every single time.

While we don't see it, every SOQL query has a cost to it, and it does not go in my Lamborghini fund, however, it should. In a recent project, we had to construct an Account 360 page that could fetch information from different integration points. The page was also called using a live telephony integration, which could pass the phone number for the account. This required an ability to keep in context the Account that was on call.

Passing the Account id in URL parameter was a valid option, however, any manipulatio…

Four secrets to improve the usability of your visualforce/Lightning pages on Salesforce

In the immortal words of a certain machine returning back in time, I am back. For now. After procrastinating for over a year and drafting a lot of rants- I am back with a very important coding lesson for you. For now.

When we are designing a Visualforce page or an app or a lightning component (added that only to trick google) or any web page for that matter, we often tend to wrap up things quickly. Ask anyone on the team, how to design a good User Experience? and the person will immediately throw buzz words like Bootstrap, angular and other useless libraries (did someone squint?)

Ok, Bootstrap, Angular-Js, and all the libraries are really cool and are useful in designing a fabulous UI. They are helpful in creating responsive pages that will work on all the devices but this post is not about those. Nope. For the purpose of this post, all the external libraries and buzzwords you use to decorate your resume are useless. (Adding Angular JS in your resume for a Salesforce consultant posit…