A tricky way to create a lookup field in Visualforce page
来源:互联网 发布:斑马梦龙网络计划2016 编辑:程序博客网 时间:2024/06/06 01:50
Recently I am asked how to create a lookup field in a Visualforce page without binding any objects. In this article, I will explain a trick to implement it.
First, let's see how Salesforce implements lookup field in a standard page. Select a contact, then click "Edit" button to go to the edit page. As we know, contact has an Account lookup field, so in the edit page we only care about how Salesforce implements account lookup field. Open source code by selecting "View Source" menu after clicking right mouse button, then move to account lookup field source code. You would see the source code like below:
<input type="hidden" name="con4_lkid" id="con4_lkid" value="001R000000cnfQd" />
<input type="hidden" name="con4_lkold" id="con4_lkold" value="a" />
<input type="hidden" name="con4_lktp" id="con4_lktp" value="001" />
<input type="hidden" name="con4_lspf" id="con4_lspf" value="0" />
<input type="hidden" name="con4_lspfsub" id="con4_lspfsub" value="0" />
<input type="hidden" name="con4_mod" id="con4_mod" value="0" />
<span class="lookupInput">
<input autocomplete="off" id="con4" maxlength="255" name="con4"onchange="getElementByIdCS('con4_lkid').value='';getElementByIdCS('con4_mod').value='1';" size="20"tabindex="4" type="text" value="a" />
<ahref="javascript:%20openLookup%28%27%2F_ui%2Fcommon%2Fdata%2FLookupPage%3Flkfm%3DeditPage%26lknm%3Dcon4%26lktp%3D%27%20%2B%20getElementByIdCS%28%27con4_lktp%27%29.value%2C670%2C%271%27%2C%27%26lksrch%3D%27%20%2B%20escapeUTF%28getElementByIdCS%28%27con4%27%29.value.substring%280%2C%2080%29%29%29" id="con4_lkwgt" onclick="setLastMousePosition(event)" tabindex="4"title="Customer Name Lookup (New Window)">
<img src="/s.gif" alt="Customer Name Lookup (New Window)" class="lookupIcon" onblur="this.className = 'lookupIcon';" onfocus="this.className = 'lookupIconOn';" onmouseout="this.className = 'lookupIcon';this.className = 'lookupIcon';" onmouseover="this.className = 'lookupIconOn';this.className = 'lookupIconOn';" title="Customer Name Lookup (New Window)" />
</a>
Now let's look at the input elements firstly, the notes below is my assumption, it may not be 100% correct.
javascript: openLookup('/_ui/common/data/LookupPage?lkfm=editPage&lknm=con4&lktp=' getElementByIdCS('con4_lktp').value,670,'1','&lksrch='escapeUTF(getElementByIdCS('con4').value.substring(0, 80)))
We can see that a few parameters would be passed into lookup page: con4, con4_lktp.
Ok, I think we pretty much understand the source code. Next, we will change the source code a little bit (indicated as red color), and we can use it in a Visualforce page.
<input type="hidden" name="testlookup_lkid" id="testlookup_lkid" value="000000000000000" />
<input type="hidden" name="testlookup_lkold" id="testlookup_lkold" value="" />
<input type="hidden" name="testlookup_lktp" id="testlookup_lktp" value="001" />
<input type="hidden" name="testlookup_lspf" id="testlookup_lspf" value="0" />
<input type="hidden" name="testlookup_lspfsub" id="testlookup_lspfsub" value="0" />
<input type="hidden" name="testlookup_mod" id="testlookup_mod" value="0" />
<span class="lookupInput">
<input autocomplete="off" id="testlookup" maxlength="255" name="testlookup" onchange="getElementByIdCS('testlookup_lkid').value='';getElementByIdCS('testlookup_mod').value='1';" size="20" tabindex="4" type="text" value="a" />
<a href="javascript:%20openLookup%28%27%2F_ui%2Fcommon%2Fdata%2FLookupPage%3Flkfm%3DeditPage%26lknm%3Dtestlookup%26lktp%3D%27%20%2B%20getElementByIdCS%28%27testlookup_lktp%27%29.value%2C670%2C%271%27%2C%27%26lksrch%3D%27%20%2B%20escapeUTF%28getElementByIdCS%28%27testlookup%27%29.value.substring%280%2C%2080%29%29%29" id="con4_lkwgt" onclick="setLastMousePosition(event)" tabindex="4" title="Customer Name Lookup (New Window)">
<img src="/s.gif" alt="Customer Name Lookup (New Window)" class="lookupIcon" onblur="this.className = 'lookupIcon';" onfocus="this.className = 'lookupIconOn';" onmouseout="this.className = 'lookupIcon';this.className = 'lookupIcon';" onmouseover="this.className = 'lookupIconOn';this.className = 'lookupIconOn';" title="Customer Name Lookup (New Window)" />
</a>
So in your Visualforce page, we should see an account lookup field. What if we want to create other object lookup field, like opportunity. How can we do it? Maybe you have the answer, yes, just change lktp value as below:
From:
<input type="hidden" name="testlookup_lktp" id="testlookup_lktp" value="001" />
To:
<input type="hidden" name="testlookup_lktp" id="testlookup_lktp" value="006" />
First, let's see how Salesforce implements lookup field in a standard page. Select a contact, then click "Edit" button to go to the edit page. As we know, contact has an Account lookup field, so in the edit page we only care about how Salesforce implements account lookup field. Open source code by selecting "View Source" menu after clicking right mouse button, then move to account lookup field source code. You would see the source code like below:
<input type="hidden" name="con4_lkid" id="con4_lkid" value="001R000000cnfQd" />
<input type="hidden" name="con4_lkold" id="con4_lkold" value="a" />
<input type="hidden" name="con4_lktp" id="con4_lktp" value="001" />
<input type="hidden" name="con4_lspf" id="con4_lspf" value="0" />
<input type="hidden" name="con4_lspfsub" id="con4_lspfsub" value="0" />
<input type="hidden" name="con4_mod" id="con4_mod" value="0" />
<span class="lookupInput">
<input autocomplete="off" id="con4" maxlength="255" name="con4"onchange="getElementByIdCS('con4_lkid').value='';getElementByIdCS('con4_mod').value='1';" size="20"tabindex="4" type="text" value="a" />
<ahref="javascript:%20openLookup%28%27%2F_ui%2Fcommon%2Fdata%2FLookupPage%3Flkfm%3DeditPage%26lknm%3Dcon4%26lktp%3D%27%20%2B%20getElementByIdCS%28%27con4_lktp%27%29.value%2C670%2C%271%27%2C%27%26lksrch%3D%27%20%2B%20escapeUTF%28getElementByIdCS%28%27con4%27%29.value.substring%280%2C%2080%29%29%29" id="con4_lkwgt" onclick="setLastMousePosition(event)" tabindex="4"title="Customer Name Lookup (New Window)">
<img src="/s.gif" alt="Customer Name Lookup (New Window)" class="lookupIcon" onblur="this.className = 'lookupIcon';" onfocus="this.className = 'lookupIconOn';" onmouseout="this.className = 'lookupIcon';this.className = 'lookupIcon';" onmouseover="this.className = 'lookupIconOn';this.className = 'lookupIconOn';" title="Customer Name Lookup (New Window)" />
</a>
Now let's look at the input elements firstly, the notes below is my assumption, it may not be 100% correct.
- con4_lkid represents current lookup account id
- con4_lkold represents current lookup account name
- con4_lktp represents object prefix (first three characters)
javascript: openLookup('/_ui/common/data/LookupPage?lkfm=editPage&lknm=con4&lktp=' getElementByIdCS('con4_lktp').value,670,'1','&lksrch='escapeUTF(getElementByIdCS('con4').value.substring(0, 80)))
We can see that a few parameters would be passed into lookup page: con4, con4_lktp.
Ok, I think we pretty much understand the source code. Next, we will change the source code a little bit (indicated as red color), and we can use it in a Visualforce page.
<input type="hidden" name="testlookup_lkid" id="testlookup_lkid" value="000000000000000" />
<input type="hidden" name="testlookup_lkold" id="testlookup_lkold" value="" />
<input type="hidden" name="testlookup_lktp" id="testlookup_lktp" value="001" />
<input type="hidden" name="testlookup_lspf" id="testlookup_lspf" value="0" />
<input type="hidden" name="testlookup_lspfsub" id="testlookup_lspfsub" value="0" />
<input type="hidden" name="testlookup_mod" id="testlookup_mod" value="0" />
<span class="lookupInput">
<input autocomplete="off" id="testlookup" maxlength="255" name="testlookup" onchange="getElementByIdCS('testlookup_lkid').value='';getElementByIdCS('testlookup_mod').value='1';" size="20" tabindex="4" type="text" value="a" />
<a href="javascript:%20openLookup%28%27%2F_ui%2Fcommon%2Fdata%2FLookupPage%3Flkfm%3DeditPage%26lknm%3Dtestlookup%26lktp%3D%27%20%2B%20getElementByIdCS%28%27testlookup_lktp%27%29.value%2C670%2C%271%27%2C%27%26lksrch%3D%27%20%2B%20escapeUTF%28getElementByIdCS%28%27testlookup%27%29.value.substring%280%2C%2080%29%29%29" id="con4_lkwgt" onclick="setLastMousePosition(event)" tabindex="4" title="Customer Name Lookup (New Window)">
<img src="/s.gif" alt="Customer Name Lookup (New Window)" class="lookupIcon" onblur="this.className = 'lookupIcon';" onfocus="this.className = 'lookupIconOn';" onmouseout="this.className = 'lookupIcon';this.className = 'lookupIcon';" onmouseover="this.className = 'lookupIconOn';this.className = 'lookupIconOn';" title="Customer Name Lookup (New Window)" />
</a>
So in your Visualforce page, we should see an account lookup field. What if we want to create other object lookup field, like opportunity. How can we do it? Maybe you have the answer, yes, just change lktp value as below:
From:
<input type="hidden" name="testlookup_lktp" id="testlookup_lktp" value="001" />
To:
<input type="hidden" name="testlookup_lktp" id="testlookup_lktp" value="006" />
This is a tricky way to implement a lookup field without binding any object/field in visualforce page. Not sure if you have any other way. Of course we can implement your own lookup function, which I will explain the details later.
0 0
- A tricky way to create a lookup field in Visualforce page
- How to create a minimal master page
- How to Create A Facebook Page
- How to study in a field
- Simpler way to Create a Custom User Control
- you must specify a way to create the tab indicator.
- A way to implement a general purpose LinkedList in C
- How to create a user in PostgreSQL
- How to: Create a Binding in Code
- How to create a function in SQL
- How to create a link in Linux
- A way to learn
- Show how to print a table in a html page
- Create a Custom Master Page
- A better way to parse XML documents in .NET (zz)
- a way to Integrate massive webpages in Python
- normal way to install a program in Ubuntu
- A simple way to read an XML file in Java
- BZOJ 1170 [Balkan2007]Cipher Hash
- Volley的使用笔记
- Installing Wireshark on Ubuntu 12.04 LTS
- android versionCode versionName的获取
- Nutch1.9安装
- A tricky way to create a lookup field in Visualforce page
- nginx编译和配置
- C程序中对时间的处理——time库函数详解以及系统时间结构体类型
- 联系方式!
- iOS 后台
- 如果是JS文件限制了你的页面加载速度,不用再担心了
- navcat for mysql 注册码
- html,添加iframe后,其他div看不见了
- C语言运算符优先级 详细列表