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
DannyK89DannyK89 

Look Up Field Question

I am working with a custom object that has a look up field. I was wondering if it is possible to plug in a partail name into the look up field and then search using the look up field. I know I can type the name into the look up and that works but I would like to auto fill the lookup field with a partail name. Im not sure if this is possible and would like some guidence. Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
Shebin-KVP Business SolnsShebin-KVP Business Solns

Hi Danny 

 

 Please try the following it is working perfectly for  in my Organization.

<apex:page standardController="Account" sidebar="false" showHeader="false">
 <script type="text/javascript" charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>  
 <script type="text/javascript" charset="utf-8" src="http://code.jquery.com/mobile/1.0b3/jquery.mobile-1.0b3.min.js"></script>
 <html>
  <apex:form id="AccountForm">
      <apex:inputField id="ContactLookup" value="{!Account.contact__c}"/>
  </apex:form>
</html>
<script type="text/javascript">
  try
  {
    var form = document.getElementsByTagName("FORM");
    var formid= '#' + form[0].id.replace(/:/g, "\\:");
    $(formid+"\\:ContactLookup").val('{!$User.FirstName}');
  }
  catch(err)
  {
    alert(err);
  }
</script>
</apex:page>

 

  Plase note that the contact__c is  a custom lookup field and while page loading the value is set as my user first name.

 

 Hope this solves your problem.

All Answers

cvuyyurucvuyyuru

 

 


We cannot do on Standard page layouts.

 

We need to design the Visual force page for this.

 

We have jQuery (autocomplete) for this.

 

 

Here is the link for it 

 

Try this. Else let me know I will share a piece of code which I have worked before.

 

 

Thanks

 

Shebin-KVP Business SolnsShebin-KVP Business Solns

Hi Danny,

 

   With standard salesforce lookup it is pretty imposible.But Since for every problem there is a solution,you can write a Javascript to get the apex form, you can then find the the input field where you will enter the default value by another javascript statement.This process  should happen while the page loads.

  

Here is the sample code.

<apex:page standardController="Contact">
//Here is your entire Html code

<apex:form id="anyId">
<apex:outputField value="{!Contact.Account}" id="anyOtherId">  //Your Lookup Field
</apex:form>

//Here you should write the script
<script type="text/javascript">
var formid= document.getElementsByTagName("FORM")[0].id;
document.getElementById(formid+":anyOtherId").value="Your Default Value";
</script>


</apex:page>

 

The above javascript will execute while the page loads and your Lookup Field will be plugged with the default value.

provided in the javascript.The limitation is  the lookup should be inside the first <apex:form tag ,or else else you need to change the index of the array in the first  javascript statement Try with the same logic it is working for me.Feel free for further clarification.

DannyK89DannyK89

Shebin,


I tried your method and it did not seem to work for me. 

 

Also I don't want the field to auto complete. I want to be able to put the users first name into the look up field from a page load and then use the look up field to search salesforce for a contact record that has a similar first name. 

Shebin-KVP Business SolnsShebin-KVP Business Solns

Hi Danny 

 

 Please try the following it is working perfectly for  in my Organization.

<apex:page standardController="Account" sidebar="false" showHeader="false">
 <script type="text/javascript" charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>  
 <script type="text/javascript" charset="utf-8" src="http://code.jquery.com/mobile/1.0b3/jquery.mobile-1.0b3.min.js"></script>
 <html>
  <apex:form id="AccountForm">
      <apex:inputField id="ContactLookup" value="{!Account.contact__c}"/>
  </apex:form>
</html>
<script type="text/javascript">
  try
  {
    var form = document.getElementsByTagName("FORM");
    var formid= '#' + form[0].id.replace(/:/g, "\\:");
    $(formid+"\\:ContactLookup").val('{!$User.FirstName}');
  }
  catch(err)
  {
    alert(err);
  }
</script>
</apex:page>

 

  Plase note that the contact__c is  a custom lookup field and while page loading the value is set as my user first name.

 

 Hope this solves your problem.

This was selected as the best answer
DannyK89DannyK89

It worked, there is one thing that I noticed however. It only worked in the showHeader attribute was set to false. If it is set to true it does not work. 

 

Thats kind of strange but this was very helpful Thank you.