2

I would like to create a lookup field in a visual force page. enter image description here

enter image description here Problem is that i don´t have a look-up field "Customer" on my custom object to add into VF page: i don t need to store the value of this look-up but i need it just for an update operation. For example i would like to link this look-up field to an apex controller property.

Do you know how to accomplish this?

Thanks in advantage for any advice.

4
  • Do you have any custom object having lookup field to "Customer"?
    – Ashwani
    Dec 26, 2014 at 11:58
  • No Regal, i don´t have. Do you have some idea?
    – Enry
    Dec 26, 2014 at 12:01
  • Can you write it in html and get the css for the lookup from salesforce and try to make it the same as a normal lookup. Then assign an id to the html input and get the value in the controller Dec 26, 2014 at 14:18
  • Of course i can build this from scratch but i m trying to find a faster way.Thanks Mihai.
    – Enry
    Dec 26, 2014 at 14:49

1 Answer 1

3

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .