I wrote this about five years ago to leverage the internal lookup widget. I just tested it now, and it still performs as expected. This particular snippet of code was used to present a lookup from a list view to change the owner of many records at once, but can easily be adapted to your particular scenario.
{!RequireScript("/js/functions.js")}
{!RequireScript("/soap/ajax/16.0/connection.js")}
var recordsToTransfer = {!GetRecordIds($ObjectType.Opportunity)}
function createForm()
{ form = document.createElement('form')
form.name = 'lookupform'
form.id = 'lookupform'
document.body.appendChild(form)
}
function addElement(name,value)
{ form = document.getElementById('lookupform')
element = document.createElement('input')
element.name = name
element.id = name
element.type = 'hidden'
element.value = value
form.appendChild(element)
}
function doAction()
{ window.removeEvent(window,'focus',doAction,true)
userToTransferTo = document.getElementById('lookup_lkid').value
if(userToTransferTo == '')
return
recordArray = new Array()
for(recordToTransfer in recordsToTransfer)
{ var Opportunity = new sforce.SObject("Opportunity")
Opportunity["Id"] = recordsToTransfer[recordToTransfer]
Opportunity["OwnerId"] = userToTransferTo
recordArray.push(Opportunity)
}
try {
results = sforce.connection.update(recordArray)
} catch(e)
{ alert(e)
}
window.top.location.href = window.top.location.href
}
function setupTransfer()
{ if(recordsToTransfer.length < 1)
{ alert('Please select at least one row!')
return
}
if(!document.elementsAddedToPage)
{ createForm()
addElement('lookup_lkid','')
addElement('lookup_lkold','')
addElement('lookup_lktp','005')
addElement('lookup_lspf','0')
addElement('lookup_mod','0')
addElement('lookup','')
document.elementsAddedToPage = true
}
openLookup(
'/_ui/common/data/LookupPage?lknm=lookup'+
'&lkfm=lookupform&lkrf=&sn=1&lktp='+
document.getElementById('lookup_lktp').value,
670,
document.getElementById('lookup_mod').value,
'&lksrch='+
escapeUTF(document.getElementById('lookup').value),
'maxw')
window.addEvent(window,'focus',doAction,true)
}
setupTransfer()
You'll want to read the function setupTransfer
, which builds a hidden form with all the necessary fields you need to get the value back from the popup. openLookup
pops up the actual lookup window. lktp is used to identify the type of lookup you want to use. Most everything else should be left as is. Note that this doesn't include any specific ability to pre-filter the results. I'm only providing this code as a sample to get you started.