I have a lookup field declared as below:

<apex:inputfield value="{!Product2.Opportunity__c}" id="prodoppr"/> 

wish to know how to add or handle event for this lookup field? When there is a change in the value I want to trigger method to perform certain validation and upate other fields....how can i achieve this? please any inputs?


You can use apex:actionSupport to listen for changes and respond accordingly. For example:

<apex:inputfield value="{!Product2.Opportunity__c}" id="prodoppr">
    <apex:actionsupport event="onchange" action="{!validateandupdate}" reRender="form" />

If you want to do client-side validation, you can also use apex:inputField's onchange attribute to support listening for changes to the lookup field.

Edit: The action attribute is used to determine the function to call. So, in this example, the function is named validateAndUpdate, which would look like this:

public void validateAndUpdate() {
    if(...) { // Some validations
    } else {
        update someRecords;

Appropriate error handling should also be part of such a function to avoid partial transactions by way of Database.setSavePoint and Database.rollback.

  • hi - could u provide some input on how to handle this in the controller class pls? Oct 20 '15 at 23:43
  • @PrabhuSethupathy I provided a very simple example for you.
    – sfdcfox
    Oct 21 '15 at 1:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.