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
Surya.236Surya.236 

Onchange function not working for lookup

Hi i have a custom visual force page , in this i was unable to use onchange event
My VF Page has Account Lookup and salesDistrict lookup ,when user chnage the account  value i have to populate the accout's sales value in sales lookup field. Folowing is my code

<apex:pageBlockSectionItem helpText=" The End User’s physical location" >
Account Name:
<apex:inputField id="oppAccountId" value="{!thisOpp.AccountId}" required="true">
<apex:actionSupport action="{!populatesalesdistrict}" event="onchange" reRender="salesDistrict" status="pageStatus"/> </apex:inputField>
</apex:pageBlockSectionItem>

<apex:pageBlockSectionItem helpText="Auto populated from Account; Enter if blank">
Sales District :
<apex:inputField value="{!thisOpp.Sales_District_lkup__c}" id="salesDistrict" required="true"/>
</apex:pageBlockSectionItem>
 controller: Mrthod:
  Public void populatesalesdistrict(){
       Account thisAcc = [SELECT Id, Sales_District__c,SoldTo__c from Account where ID =: thisOpp.AccountId];
       thisOpp.Sales_District_lkup__c = thisAcc.Sales_District__c;
}
What is alternate if onchange will not work for lookup fields.?
Best Answer chosen by Surya.236
Dushyant SonwarDushyant Sonwar
Apologies , I was busy  at some work, so didn't had the chance to look at ths.

I implemeted the same code you posted using actionRegion and it is working Fine.
 
<apex:actionRegion>
<apex:inputField id="oppAccountId" value="{!thisOpp.AccountId}" required="true">
<apex:actionSupport action="{!populatesalesdistrict}" event="onchange" reRender="salesDistrict,pg" status="pageStatus"/> </apex:inputField>
</apex:actionRegion>


Hope this helps.
 

All Answers

Dushyant SonwarDushyant Sonwar
Hey Surya,

I think some page validation is causing some issue ,

Do the following changes in your code.
1)  <apex:pagemessages id="pg" />

2) <apex:actionSupport action="{!populatesalesdistrict}" event="onchange" reRender="salesDistrict,pg" status="pageStatus"/>
<apex:pageBlockSectionItem helpText=" The End User’s physical location" >
<apex:pagemessages id="pg" />
Account Name:
<apex:inputField id="oppAccountId" value="{!thisOpp.AccountId}" required="true">

<apex:actionSupport action="{!populatesalesdistrict}" event="onchange" reRender="salesDistrict,pg" status="pageStatus"/> </apex:inputField>
</apex:pageBlockSectionItem>

<apex:pageBlockSectionItem helpText="Auto populated from Account; Enter if blank">
Sales District :
<apex:inputField value="{!thisOpp.Sales_District_lkup__c}" id="salesDistrict" required="true"/>
</apex:pageBlockSectionItem>
Then see what error you are getting when changing the lookup.

Hope this helps.
 
Surya.236Surya.236
Hi Sonwar,
I made the changes as you said but i didnt get any error and debug logs are also not showing anything
Dushyant SonwarDushyant Sonwar
Apologies , I was busy  at some work, so didn't had the chance to look at ths.

I implemeted the same code you posted using actionRegion and it is working Fine.
 
<apex:actionRegion>
<apex:inputField id="oppAccountId" value="{!thisOpp.AccountId}" required="true">
<apex:actionSupport action="{!populatesalesdistrict}" event="onchange" reRender="salesDistrict,pg" status="pageStatus"/> </apex:inputField>
</apex:actionRegion>


Hope this helps.
 
This was selected as the best answer
Surya.236Surya.236
Hi Sonwar,
Thank you for your response, there was a  jquery which was stoping it's onchange property, it is working now.
Regards,
Surya