0

I have a scenario where when I open a newform for one list, I want it to create a new record in another list. In list 1 the field I want it to copy is 'DocID' and this would go into Title of list 2. I would like to use javascript to do this but don't know enough about SharePoint (my guess) to figure it out.

Any assistance would be greatly appreciated!

function createNew() {
   var clientContext = new SP.ClientContext(siteUrl);
   var oList = clientContext.get_web().get_list().getByTitle('LIST2');
   var itemCreateInfo = new SP.ListItemCreationInformation();
   this.oListItem = oList.addItem(itemCreatInfo);
   oListItem.set_item('Title','Hello World!');
   oListItem.update();
   clientContext.load(oListItem);
}

Above is what I was trying to use to add a new record with "Hello World!" in the Title but when I try to change that to a variable, like the DocID in the existing list, it comes up blank.

I don't want to use workflows because I need the item in the 2nd list before the 1st item is saved.

0

It seems, DocID is not getting passed properly. If possible, please follow below approach and let me know Create a custom form(HTML & .js) for your 1st list.

On Save button click, you need to call 2 functions

  1. Save2ndlist Function to create an item in 2nd list
  2. Save1stlist Function to create an item in 1st list.

While calling the Save2ndlist function, you can pass the DocID to it.

Prefer REST API for above approach.

0

yes, thank you for this. Seems I had things a bit jumbled. I placed the variable at the top of the function. Seems to work now.

function createNew() {
  var saveID = getFieldValue("DocID");
  var clientContext = new SP.ClientContext(siteUrl);
  var oList = clientContext.get_web().get_list().getByTitle('LIST2');
  var itemCreateInfo = new SP.ListItemCreationInformation();
  this.oListItem = oList.addItem(itemCreatInfo);
  oListItem.set_item('Title',saveID);
  oListItem.update();
  clientContext.load(oListItem);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.