PreFill Form Field

rss

Posted by bburke - May 26 ’13 at 09:45

I have two lists, "Pipeline" and "Notes". I am following the blog post http://formsdesigner.blogspot.com/2013/04/sharepoint-2013-form-with-related-items_11.html

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:

// 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:

// 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?

  • Dmitry
    Dmitry
    Admin
    Member for: 7 years 10 days
    #1 by Dmitry May 27 ’13 at 12:20

    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: fd.field('Pipeline').control().value(queryString['Title']);

    Use this one, that works for any count of related items:

    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’.

  • bburke
    Member
    Member for: 6 years 27 days
    #2 by bburke May 27 ’13 at 01:28

    Thanks for your great support helping me solve my issue.

  • bburke
    Member
    Member for: 6 years 27 days
    #3 by bburke May 30 ’13 at 06:07

    This works in Internet Explorer but is not working in Firefox 21.0. Can you assist?

  • Dmitry
    Dmitry
    Admin
    Member for: 7 years 10 days
    #4 by Dmitry May 31 ’13 at 03:39

    [#3]: 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:

    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);
    }
    
Displaying 1 to 4 of 4 messages