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
PS81PS81 

Lookup field event handler

Hi

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?
Best Answer chosen by PS81
KaranrajKaranraj
You can use actionsupport to handle the event and call the controller method 
 
<apex:inputfiled value={!someValeu} >
 <apex:actionsupport event="onchange" action={!yourmethod} defender="sectionid" />
<apex:inputfield>

check this for more detail about action support https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionSupport.htm



 

All Answers

Balaji BondarBalaji Bondar
You can use lookup filter to have validation while selecting the appropriate Opportunity.
For custom logic implementation on selection of Opporutnity, write a trigger as below on Product2 object:
trigger <name> on Product2 (before insert , before update) {
//custom logic
}
PS81PS81
Hi Balaji

I'm using a custom VF page and with reference to it writting a trigger will not have the validations happening online. Handling events and triggers are not the same and i wish to write a event handler that would work online somehting like client-server interaction so that i can write code to have other fields updated as required,
KaranrajKaranraj
You can use actionsupport to handle the event and call the controller method 
 
<apex:inputfiled value={!someValeu} >
 <apex:actionsupport event="onchange" action={!yourmethod} defender="sectionid" />
<apex:inputfield>

check this for more detail about action support https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionSupport.htm



 
This was selected as the best answer
PS81PS81
Thk you....

i have the below in my VF page:

            <apex:inputfield value="{!Opportunity.StageName}" >
                <apex:actionSupport id="oprstage" event="onchange" action="{!setProbability}" rerender="form" immediate="TRUE" />
            </apex:inputfield>

in my controller:

public with sharing class OpportunitySave {
    public string probability {get;set;}
    public Opportunity opr {get;set;}
    public OpportunitySave(ApexPages.StandardController controller) {

    }
    public PageReference setProbability(){

    }
}

Not sure how I can refer the value from StageName and update other field 'Probability' (text field) in opportunity...any inputs on this pleasE?