+ Start a Discussion
Kyle FreemanKyle Freeman 

Transfer to a lookup field from a list view

I need to make a list view button that allows me to transfer checked objects.  However, I would like to select the objects (check them off), click the button, and then have a pop-up dialog where I could either use the lookup to select where to transfer the objects, or a less preferred way of manually typing in the object's ID.
 
I have currently created Mass Delete & Mass Update (to a static value) buttons for the list view, but am wondering if something like this has been done or is possible.
 
Thanks in advance!
sfdcfoxsfdcfox

Anything is possible if you're willing to work for it. I'm not sure if it's been done (before today), but here's my solution:

Code:
{!RequireScript("/js/functions.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(x = 0; x < recordsToTransfer.length; x++)
  { var Opportunity = new sforce.SObject("Opportunity")
    Opportunity["Id"] = recordsToTransfer[x]
    Opportunity["OwnerId"] = userToTransferTo
    recordArray.push(Opportunity)
  }
try {
  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()

I configured this for oportunities, but you could easily replace it for any standard or custom object. Brings up a list of users, and transfers records to the selected user if you select a user. Could break at any time, etc, etc. This was a marriage of an S-Control that I have that uses a lookup window and a button that I have that transfers ownership of records (because I wanted one for custom objects).

Edit: Fixed another broken question mark, compliements of SRC.



Message Edited by sfdcfox on 01-07-2008 05:08 PM
Kyle FreemanKyle Freeman
Thanks for the quick response!
 
What are some things that would possibly cause this to break in the future?  I'd hate to change our processes only to have them not function properly and cause a crisis later.
sfdcfoxsfdcfox
Anything could happen, though the most likely possibility is that when the new (filtered) list views are introduced, something changes that breaks the functionality. Most likely, the problem will occur on the trigger that I'm using to execute the rest of the code. Right now, I use a "onFocus" command on the window, which works because it's a popup window. However, if the popup functionality goes away as they propose, my code won't know when to finish the update. The only way I can see this working is if onchange will trigger from a javascript-initiated change (I don't think it does, but need to mess with it), or you'd have to set up a "polling" loop (check every, say, 500ms to see if the value's updated, and if so, complete the transaction). Only time (and the pre-release) will tell...
sfdcfoxsfdcfox
Code:
{!RequireScript("/js/functions.js")}

var recordsToTransfer = {!GetRecordIds($ObjectType.Opportunity)}
var timeOutID = null

window.waitForTransfer = function()
{ if(document.getElementById('lookup_lkid').value != '')
  { clearTimeout(timeOutID)
    doAction()
  }
  else
  { timeOutID = setTimeout("waitForTransfer()",500)
  }
}

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(x = 0; x < recordsToTransfer.length; x++)
  { var Opportunity = new sforce.SObject("Opportunity")
    Opportunity["Id"] = recordsToTransfer[x]
    Opportunity["OwnerId"] = userToTransferTo
    recordArray.push(Opportunity)
  }
try {
  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')
if(timeOutID) clearTimeout(timeOutID) timeOutID = setTimeout("waitForTransfer()",500) } setupTransfer()


This version works around the lack of a second window by setting up a timeout loop. I had to do it this way because OnChange won't trigger based on the Lookup doing it's thing. Hopefully the rest of the code won't need to change, but always remember; salesforce.com reserves the right to change any undocumented features in order to better the service or adapt to new technologies.



Message Edited by sfdcfox on 01-09-2008 02:09 PM

Message Edited by sfdcfox on 01-09-2008 02:10 PM