+ Start a Discussion
b-Forceb-Force 

Calling Javascript after user select Record from Standard Lookup Search window

My requirement is as follow

 

I have a Visual force page with some input fields

It has a lookup field

When user click on Search icon ------Standard Search popup opened -------- User select some record

and return back to original page

when it returns to original page I want to retrive some data from selected master record and populate on the page 

Please help me how to catch this event in javascript or apex....

 

code is as follow

 

 

<apex:pageBlockSection id="GeneralInfo" columns="1" showHeader="true" title="General Information for Claims"> 
<apex:inputField Value="{!Claim__c.Name}"/>
<apex:inputField Value="{!Claim__c.Description__c}"/>
<apex:inputField Value="{!Claim__c.RequestedAmount__c}"/>
<apex:inputField Value="{!Claim__c.Fund_Request__c}"  onchange="changed()" onselect="selected()" onmouseout="mouseout()" onblur="alert('onblurred');"/>
</apex:pageBlockSection>

 

 

If we capture this event, then We can populate fields using ajax :)

 

Thanks in advance,

Bala

 

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

I resolve my bussiness problem using Javascript

 

<apex:page standardController="Claim__c" extensions="LookUp" tabStyle="Claim__c">
<script type="text/javascript">var __sfdcSessionId = '{!GETSESSIONID()}';</script>
<script src="../../soap/ajax/19.0/connection.js" type="text/javascript"></script>
<script type="text/javascript">
var IsContinue=true;
    window.onload = function() {
    var int=self.setInterval("StartpopulateFundInfo()",2000);
    }
function StartpopulateFundInfo()
  {
  var id=document.getElementById("j_id0:j_id2:j_id4:GeneralInfo:j_id8_lkid").value;
//  document.getElementById("clock").value=id;
  if(id!='000000000000000' && IsContinue )
  {
  IsContinue=false;
  return populateFundInfo();
  }
  }
function  populateFundInfo()
{
      //function contains all code to execute after page is rendered  
      var state = { //state that you need when the callback is called  
          output : document.getElementById("output"),
          startTime : new Date().getTime()};
  var callback = {
          //call layoutResult if the request is successful  
          onSuccess: layoutResults,
          //call queryFailed if the api request fails  
          onFailure: queryFailed,
          source: state};
     var sFQuery='Select Id, Name,Fund_Type__c,Owner__c,TicketNumber__c,Department__c From Fund_Request__c where Id=\''+document.getElementById("j_id0:j_id2:j_id4:GeneralInfo:j_id8_lkid").value+'\'';
     sforce.connection.query(sFQuery,callback);
}
function queryFailed(error, source) 
  {
    source.output.innerHTML = "An error has occurred: " + error;
  }
function layoutResults(queryResult, source) {
  if (queryResult.size > 0) {
      var records = queryResult.getArray('records');
      var sfund = records[0];
document.getElementById("j_id0:j_id2:j_id4:FundInfo:j_id9").value=sfund.Name;
document.getElementById("j_id0:j_id2:j_id4:FundInfo:j_id10").value=sfund.Fund_Type__c;
document.getElementById("j_id0:j_id2:j_id4:FundInfo:j_id11").value=sfund.TicketNumber__c;
document.getElementById("j_id0:j_id2:j_id4:FundInfo:j_id12").value=sfund.Department__c;  
  }
  }
</script>
Hello {!$User.FirstName}  {!$User.LastName}<br/><br/>
<apex:form >
<body>
<div id="output"> Error Place Holder-----</div><br/><br/><br/>
<apex:pageBlock >
<apex:pageBlockSection id="GeneralInfo" columns="1" showHeader="true" title="General Information for Claims"> 
<apex:inputField Value="{!Claim__c.Name}"/>
<apex:inputField Value="{!Claim__c.Description__c}"/>
<apex:inputField Value="{!Claim__c.RequestedAmount__c}"/>
<apex:inputField Value="{!Claim__c.Fund_Request__c}" onfocus="populateFundInfo()"/>
</apex:pageBlockSection>
<apex:pageBlockSection id="FundInfo" columns="2" showHeader="true" title="Fund Information">
<apex:inputField Value="{!Claim__c.Sample1__c}"/>
<apex:inputField Value="{!Claim__c.Sample2__c}"/>
<apex:inputField Value="{!Claim__c.Sample3__c}"/>
<apex:inputField Value="{!Claim__c.Sample4__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:commandButton action="{!save}" value="Save New Claim"/>
<apex:commandButton action="Cancel" value="Cancel"/>
</body>
</apex:form>
</apex:page>

 

 

 I follow following steps to

1) On Window load initiate a function which fires periodically and checks whether Any record from look is seleceted (It check for every 2 seconds)

var IsContinue=true;
    window.onload = function() {
    var int=self.setInterval("StartpopulateFundInfo()",2000);
    }

2)then get Id of selected record, Retrive record using SOQL in Ajax

and populate on the page

 

 

It works fine

 

Thanks,

Bala

 

 

All Answers

TrueCloudTrueCloud

Bala You will have to use JavaScript to resolve this issue to capture the information from the record that is selected. I am confused on what data exactly you need to retrieve and how do you plan to use it.

 

I understand you have a custom VF Page that has a lookup field.

When a user selects the record for the Lookup field you want to get some other information, Correct?

 

I think it would be helpful to:

 

a) Explain more clearly with the actual business case that you are trying to resolve?

b) If possible put your entire code for others to understand the issue in its entirety.

 

One tip I would give is that there are two hidden elements that you can access on a custom VF Page for a lookup field using document.GetElementbyID function.

The first element is a hidden element that is set when a record is selected by the User. this element is setting the record ID. The second element sets the record name which is displayed on the lookup page.

 

There are also other Elements such as lk_old(XXXXXX), lk_new etc which stores historical values.

 

These element IDs are constant and hence can be used to do some major customization.

 

Here is an example that I had developed when I was working with one of my clients. On a Contact Page there is a Lookup field for Primary Office, Hence if the Primary Office (Custom Object) was not selected than it will pop-up the a custom VF Page which will then be populated by the user to create a custom office.

 

I passed the Contact ID, and the Company Name selected on the contact record to that custom VF Page by appending those parameters to the URL and retrieving them by split method. I will be happy to share my code if that is needed.

b-Forceb-Force

Thanks you so much for your reply  :smileyhappy:

Bala

 

b-Forceb-Force

I resolve my bussiness problem using Javascript

 

<apex:page standardController="Claim__c" extensions="LookUp" tabStyle="Claim__c">
<script type="text/javascript">var __sfdcSessionId = '{!GETSESSIONID()}';</script>
<script src="../../soap/ajax/19.0/connection.js" type="text/javascript"></script>
<script type="text/javascript">
var IsContinue=true;
    window.onload = function() {
    var int=self.setInterval("StartpopulateFundInfo()",2000);
    }
function StartpopulateFundInfo()
  {
  var id=document.getElementById("j_id0:j_id2:j_id4:GeneralInfo:j_id8_lkid").value;
//  document.getElementById("clock").value=id;
  if(id!='000000000000000' && IsContinue )
  {
  IsContinue=false;
  return populateFundInfo();
  }
  }
function  populateFundInfo()
{
      //function contains all code to execute after page is rendered  
      var state = { //state that you need when the callback is called  
          output : document.getElementById("output"),
          startTime : new Date().getTime()};
  var callback = {
          //call layoutResult if the request is successful  
          onSuccess: layoutResults,
          //call queryFailed if the api request fails  
          onFailure: queryFailed,
          source: state};
     var sFQuery='Select Id, Name,Fund_Type__c,Owner__c,TicketNumber__c,Department__c From Fund_Request__c where Id=\''+document.getElementById("j_id0:j_id2:j_id4:GeneralInfo:j_id8_lkid").value+'\'';
     sforce.connection.query(sFQuery,callback);
}
function queryFailed(error, source) 
  {
    source.output.innerHTML = "An error has occurred: " + error;
  }
function layoutResults(queryResult, source) {
  if (queryResult.size > 0) {
      var records = queryResult.getArray('records');
      var sfund = records[0];
document.getElementById("j_id0:j_id2:j_id4:FundInfo:j_id9").value=sfund.Name;
document.getElementById("j_id0:j_id2:j_id4:FundInfo:j_id10").value=sfund.Fund_Type__c;
document.getElementById("j_id0:j_id2:j_id4:FundInfo:j_id11").value=sfund.TicketNumber__c;
document.getElementById("j_id0:j_id2:j_id4:FundInfo:j_id12").value=sfund.Department__c;  
  }
  }
</script>
Hello {!$User.FirstName}  {!$User.LastName}<br/><br/>
<apex:form >
<body>
<div id="output"> Error Place Holder-----</div><br/><br/><br/>
<apex:pageBlock >
<apex:pageBlockSection id="GeneralInfo" columns="1" showHeader="true" title="General Information for Claims"> 
<apex:inputField Value="{!Claim__c.Name}"/>
<apex:inputField Value="{!Claim__c.Description__c}"/>
<apex:inputField Value="{!Claim__c.RequestedAmount__c}"/>
<apex:inputField Value="{!Claim__c.Fund_Request__c}" onfocus="populateFundInfo()"/>
</apex:pageBlockSection>
<apex:pageBlockSection id="FundInfo" columns="2" showHeader="true" title="Fund Information">
<apex:inputField Value="{!Claim__c.Sample1__c}"/>
<apex:inputField Value="{!Claim__c.Sample2__c}"/>
<apex:inputField Value="{!Claim__c.Sample3__c}"/>
<apex:inputField Value="{!Claim__c.Sample4__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:commandButton action="{!save}" value="Save New Claim"/>
<apex:commandButton action="Cancel" value="Cancel"/>
</body>
</apex:form>
</apex:page>

 

 

 I follow following steps to

1) On Window load initiate a function which fires periodically and checks whether Any record from look is seleceted (It check for every 2 seconds)

var IsContinue=true;
    window.onload = function() {
    var int=self.setInterval("StartpopulateFundInfo()",2000);
    }

2)then get Id of selected record, Retrive record using SOQL in Ajax

and populate on the page

 

 

It works fine

 

Thanks,

Bala

 

 

This was selected as the best answer
TrueCloudTrueCloud

I am glad that worked out for you.

Michele ToscanoMichele Toscano
@TrueCloud - Could you share your code?  I would be interested to see how you did that.  Many thanks.