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
MTBRiderMTBRider 

Two Questions: Using onblur instead of onchange & default field behavior in VF pages

I have two questions:
 
1.  onChange does not work on a Lookup field.  The recommendation is to use onblur, but I am unsure on how to use it.  When the user clicks on the magnifying glass, onblur fires but no changes have been made yet.  If my action method is to perform a calculation based on the changed value in the lookup field, how do I do this if onblur is going to call the action method before any changes have been made?
 
2.  I am creating a custom Opportunity screen with Visualforce.  I am using the standard Opportunity controller with my own extension.  I was under the impression that standard functionality of the Opportunity screen would still work.  Specifically, in a standard Opportunity screen, probability percentage is driven by the sales stage that the user selects.  Using the standard controller with an extension, this does not seem to work anymore.  Selecting the Sales Stage does not automatically select a corrisponding probability percentage.  Shouldn't it?
 
Thanks
jwetzlerjwetzler
1.  I'm not sure who recommended onblur to you.  neither onchange or onblur are going to work.  lookup fields consist of an input and a lookup button, and your onchange/onblur attributes are going to be applied to the input box.  I'm not sure how to go around it aside from making the user click a button or link that will fire your action.  lookup fields are unfortunately complex inputs.

2.  the probability percentage is a dependent field, and dependent fields are not yet supported in Visualforce.
ctewellctewell

To get onchange type functionality to work I did this to lookup the Account Name when the Opportunity changed.

This needs a little improvement but I just got it working.  I used the onfocus event of the controlling field to call an actionFunction.

 

<script type="text/javascript">
function oppFocused(oppId)
{
   if(oppId.indexOf("Opportunity_lkwgt") > -1){
        return;
   }
   var Opplkid = document.getElementById(oppId + '_lkid').value;
   if(Opplkid != '000000000000000' && Opplkid != null){
           lookupAccountName(Opplkid);
   }
}
</script>
<apex:actionFunction action="{!lookupAccountName}" name="lookupAccountName"
    immediate="true"
    rerender="AccountName" id="AFlookupAccountName" >
    <apex:param name="firstParam" assignTo="{!TSImpCont.TSImplementation.Opportunity__c}" value="" id="paramOppId" />
</apex:actionFunction>

.

.

.

<apex:inputField value="{!TSImpCont.TSImplementation.Opportunity__c}" id="Opportunity"
    onfocus="oppFocused(this.id)" />
<apex:pageBlockSectionItem >
<apex:outputLabel value="Account Name" for="AccountName"/>
<apex:outputText value="{!AccountName}" id="AccountName"/>
</apex:pageBlockSectionItem>

 

Controller code:

    // This method sets the AccountName value based on the Opportunity Id
    public PageReference lookupAccountName(){
        if(TSImpCont.TSImplementation.Opportunity__c == null){
            return null;
        }
        AccountName = [select Account.Name
                        from Opportunity
                        where Id = :TSImpCont.TSImplementation.Opportunity__c][0].Account.Name;
        return null;
    }

 

Need to add code to only execute the soql if the opportunity id has changed.

If SalesForce changes the underlying lkid the code will no longer work.

 

I also suggest using onchanged on the opportunity to clear the account name.  If the user types in the opportunity the account name will be cleared out.  You could then perhaps set it when the record is saved.