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
ShikibuShikibu 

ContactId inputField fails to invoke "onchange" action if user uses magnifying glass

I have a VF page for creating a new support Case. The page presents a list of valid Assets, once the user has chosen a Contact. Here's the VF

 

 

<apex:inputField value="{!theCase.ContactId}" id="the_contact"> <apex:actionSupport event="onchange" action="{!contactChangedAction}" status="updating_assets_status" rerender="assets, the_asset, asset_message" immediate="false"/> </apex:inputField>

 

 

 

If the user types or pastes in a valid Contact name, the apex contactChangedAction() method is invoked. But if the user presses the magnifying glass, contactChangedAction() is not invoked. How can I make sure that contactChangedAction() is invoked, by whatever means the user changes the contact?

Best Answer chosen by Admin (Salesforce Developers) 
ShikibuShikibu

Here's what I ended up doing. The actionSupport for onblur performs my ajax update in the case where the user pastes or types a contact name into the contactId lookup field.

 

The javascript at the bottom of the page is invoked when the window (the whole page) either loses or gains focus. On loss of focus, it remembers what the contact name was. On gaining focus, it forces the onblur actionsupport to run if the contact name has been changed. This handles the case where the user uses the magnifying glass.

Best of all, it doesn't seem likely to break when salesforce changes their implementation internals.

 

 

<apex:form id="theForm">
<apex:sectionHeader title="Case Edit" subtitle="New Case"/>
<apex:pageBlock title="Case Edit" id="whole_page">

<apex:pageBlockSection>
<apex:inputField value="{!theCase.ContactId}"
id="the_contact">
<apex:actionSupport event="onblur"
action="{!contactChangedAction}"
status="updating_assets_status"
rerender="whole_page"
immediate="false"/>
</apex:inputField>

<apex:inputField value="{!theCase.AssetId}"
styleClass="Asset"
id="the_asset"/>

<apex:pageBlockSectionItem>
<label/>
<apex:outputPanel id="asset_message">
<apex:outputPanel rendered="{!LEN(assetMessage)!=0}">
<div class="statusMsg">
<apex:outputText value="{!assetMessage}"/>
</div>
</apex:outputPanel>
</apex:outputPanel>
</apex:pageBlockSectionItem>

<apex:pageBlockSectionItem>
<label/>
<apex:actionStatus startText="(updating...)" id="updating_assets_status" />
</apex:pageBlockSectionItem>

</apex:pageBlockSection>
<script>
var theContact = document.getElementById("{!$Component.contact_and_asset}"+":the_contact");
</script>

</apex:pageBlock>
</apex:form>

<script>
var previousOnblur = window.onblur;
var previousContact;
window.onblur = function() {
if (previousOnblur) {
previousOnblur();
}

console.log('losing focus');
previousContact = theContact.value;
}

var previousOnfocus = window.onfocus;
window.onfocus = function() {
if (previousOnfocus) {
previousOnfocus();
}

console.log('gaining focus');
if (theContact.value != previousContact) {
console.log('contact changed');
theContact.onblur();
}
}
</script>
</apex:page>

 



 

 

Message Edited by Shikibu on 10-22-2009 11:25 AM
Message Edited by Shikibu on 10-22-2009 11:33 AM

All Answers

ThomasTTThomasTT
I had the same problem, but at the end of the day, I gave up using onchange and I used onblur instead. Whenever the cursor left he field, I updated other field (that was what I wanted to do onchange).
So, I still wish someone could find a better solution, but that was one of workarounds.
If you really want to use onchange, you could implement the magnifying glass with SFDC JavaScript openlookup and set your function to window.close event, or use showModalpopup and call your function after that ( showModalpopup will wait for closing the window).
This is a very classic automation so that I hope there is easier way.
ThomasTT
ShikibuShikibu
ah, thanks Thomas. That fixed it adequately for my needs.
ShikibuShikibu

Here's what I ended up doing. The actionSupport for onblur performs my ajax update in the case where the user pastes or types a contact name into the contactId lookup field.

 

The javascript at the bottom of the page is invoked when the window (the whole page) either loses or gains focus. On loss of focus, it remembers what the contact name was. On gaining focus, it forces the onblur actionsupport to run if the contact name has been changed. This handles the case where the user uses the magnifying glass.

Best of all, it doesn't seem likely to break when salesforce changes their implementation internals.

 

 

<apex:form id="theForm">
<apex:sectionHeader title="Case Edit" subtitle="New Case"/>
<apex:pageBlock title="Case Edit" id="whole_page">

<apex:pageBlockSection>
<apex:inputField value="{!theCase.ContactId}"
id="the_contact">
<apex:actionSupport event="onblur"
action="{!contactChangedAction}"
status="updating_assets_status"
rerender="whole_page"
immediate="false"/>
</apex:inputField>

<apex:inputField value="{!theCase.AssetId}"
styleClass="Asset"
id="the_asset"/>

<apex:pageBlockSectionItem>
<label/>
<apex:outputPanel id="asset_message">
<apex:outputPanel rendered="{!LEN(assetMessage)!=0}">
<div class="statusMsg">
<apex:outputText value="{!assetMessage}"/>
</div>
</apex:outputPanel>
</apex:outputPanel>
</apex:pageBlockSectionItem>

<apex:pageBlockSectionItem>
<label/>
<apex:actionStatus startText="(updating...)" id="updating_assets_status" />
</apex:pageBlockSectionItem>

</apex:pageBlockSection>
<script>
var theContact = document.getElementById("{!$Component.contact_and_asset}"+":the_contact");
</script>

</apex:pageBlock>
</apex:form>

<script>
var previousOnblur = window.onblur;
var previousContact;
window.onblur = function() {
if (previousOnblur) {
previousOnblur();
}

console.log('losing focus');
previousContact = theContact.value;
}

var previousOnfocus = window.onfocus;
window.onfocus = function() {
if (previousOnfocus) {
previousOnfocus();
}

console.log('gaining focus');
if (theContact.value != previousContact) {
console.log('contact changed');
theContact.onblur();
}
}
</script>
</apex:page>

 



 

 

Message Edited by Shikibu on 10-22-2009 11:25 AM
Message Edited by Shikibu on 10-22-2009 11:33 AM
This was selected as the best answer
ThomasTTThomasTT
Nicely done! I bookmarked this post. Thank you! - ThomasTT
prakashdprakashd

I wonder if there are any typos in the script. I'm getting an error that theContact.onblur() is not a function.

 

It seems the script is failing to invoke onblur on the lookup text component. 

Please advise.

Thank you

Leo BarnoskiLeo Barnoski
need help in integration for my Prominent Commercial Glass Installers (https://www.houstonseaofglass.com) website.