Page 1 of 1

PreFill Form Field

Posted: 26 May 2013
by bburke
I have two lists, "Pipeline" and "Notes". I am following the blog post http://spform.com/forms-designer- ... ist-part-2

I want to prefill the "Pipeline" field in the New Item Notes form with the "Title" field from the "Pipeline" form.

In the Parent (Pipeline) Edit form I enter the following in the js-editor:

Code: Select all

// getting query string hash
var queryString = SP.ScriptHelpers.getDocumentQueryPairs();
// adding get-parameter 'order' into new item link
newItem.attr('onclick', 
  'NewItem2(event, "' + newItem.attr('href') + '&Title=' + queryString['ID'] + '"); return false;')
In the (Notes) New Item Form, I enter the following in the js-editor:

Code: Select all

// getting query string hash
var queryString = SP.ScriptHelpers.getDocumentQueryPairs();
// fill order number
fd.field('Pipeline').control().value(queryString['Title']);
I see the Title ID in the url when I open the Notes NewItem Form.

But the Title ID in the url is populating the "Title" field in the Notes New Item Form.

Am I doing something wrong?

Re: PreFill Form Field

Posted: 27 May 2013
by Dmitry Kozlov
Thanks for your great support helping me solve my issue.

Re: PreFill Form Field

Posted: 27 May 2013
by bburke
First, you have to rename your get-parameter. SharePoint prefills Title field with parameter named ‘Title’.

How many items are in Pipeline list?

This script works only if items count in the dropdown is less than 20:

Code: Select all

fd.field('Pipeline').control().value(queryString['Title']);
Use this one, that works for any count of related items:

Code: Select all

var queryString = SP.ScriptHelpers.getDocumentQueryPairs(); 
var id = queryString['Title'];

if (fd.field('Parent').control()._el().find('input').length > 0) {
  var control = fd.field('Parent').control()._el().find('input');
  var options = control.attr('choices').split('|');
  $.each(options, function(i) {
    if (i % 2 == 1 && parseInt(this) == id) {
      $('#' + control.attr('optHid')).val(id);
      control.val(options[i-1]);
      return false;
    }
  })
} else {
  fd.field('Parent').control().value(id);
}
My lookup field called ‘Parent’, so, you have to rename it to ‘Pipeline’.

Re: PreFill Form Field

Posted: 30 May 2013
by bburke
This works in Internet Explorer but is not working in Firefox 21.0. Can you assist?

Re: PreFill Form Field

Posted: 31 May 2013
by Dmitry Kozlov
I have just checked this code in the latest version of FF and it seems it works correctly. Please, verify that you have correct internalName in the highlighted line:

Code: Select all

if (fd.field('Parent').control()._el().find('input').length > 0) {
  var control = fd.field('Parent').control()._el().find('input');
  var options = control.attr('choices').split('|');
  $.each(options, function(i) {
    if (i % 2 == 1 && parseInt(this) == id) {
      $('#' + control.attr('optHid')).val(id);
      control.val(options[i-1]);
      return false;
    }
  })
} else {
  fd.field('Parent').control().value(id);
}