Create a new item in list B prefilling common fields from list A

Forums General discussion Create a new item in list B prefilling common fields from list A

Viewing 4 reply threads
  • Author
    Posts
    • #13160
      Penny Kingston
      Participant

      Hi Alex,
      I have a list A which gets populated, then using a manual trigger I would like to create an item in list B and prefill a few common fields between the two lists. I would like to do this behind the scenes so that the user doesn’t know about the new item. Is that possible? I thought using spjs.utility.addItem might work if the new list and fields are defined. Would the field internal names have to be the same in both lists for the common fields?
      Thanks!

    • #13164
      Alexander Bautz
      Keymaster

      You can basically use the code from this page: https://spjsblog.com/2015/10/13/redirect-from-newform-to-editform-in-dffs/

      Change the “listName” parameter to match list B and populate the “data” object like this:

      data.ColNameInListB1 = getFieldValue("ColNameInListA1");
      data.ColNameInListB2 = getFieldValue("ColNameInListA2");

      You must change the code to remove this line:

      location.href = url;

      and you must change the PreSaveAction function to return true.

      Here is an example of the finished code:

      function addItemInListB(){
      	var ok = spjs.dffs.check_Mandatory(["Title"]), data = {}, newItem;
      	if(ok){
      		data.Title = getFieldValue("Title");
      		newItem = spjs.utility.addItem({"listName":"List B Display name or GUID","data":data});
      		if(newItem.success){
      			// Item added
      		}else{
      			alert(newItem.errorText);
      		}
      	}
      }
      
      function PreSaveAction(){
      	addItemInListB();
      	return true;
      }

      Hope this gets you started.

      Alexander

    • #14329
      Celeste Garcia
      Participant

      I have implemented this code successfully, except I get an error when trying to set Content Type and date type fields in the new item…

      Field table says FIN for Content Type is “ContentTypeChoice”, so my line of code is:

      data.ContentTypeChoice = "TestCT";

      Error: “One or more field types are not installed properly. Go to the list settings page to delete these fields.”

      For the date type field, code is:

      data.ScheduleDate = getFieldValue("ScheduleDate");

      Error: “Invalid date/time value. A date/time field contains invalid data. Please check the value and try again.”

      Any help is appreciated, thanks!

    • #14348
      Alexander Bautz
      Keymaster

      Hi,
      Sorry, I had a mistake in the “Field table” – the correct internal name of the Content type field is “ContentType”. I’ll fix this in the next release.

      When setting a date and time field, you must supply the value in ISO 8601 format – December 5 (at noon) would be entered like this:

      2015-12-05 12:00:00

      To convert your “getFieldValue” to this format, you can do it like this:

      var raw = getFieldValue("DateColumnInternalName").split(/\/| /);
      var dateString = raw[2]+"-"+raw[0]+"-"+raw[1]+" 12:00:00"

      This snippet expects this format: 12/5/2016 – if you have another date format, the code snippet must be changed to put the year, month and date in the correct place.

      Alexander

    • #14375
      Celeste Garcia
      Participant

      This got it working, thank you!

Viewing 4 reply threads
  • You must be logged in to reply to this topic.