title: SharePoint 2013 Javascript Insert Item with lookup field
body: <p>I'm trying to just Javascript to add a list item when in SharePoint 2013 using the following code;</p>
  
  <pre><code>function createListItem() {
  
      var clientContext = new SP.ClientContext.get_current();
  
      var oList = clientContext.get_web().get_lists().getByTitle('CrewNoticesAudit');
  
      var itemCreateInfo = new SP.ListItemCreationInformation();
      this.oListItem = oList.addItem(itemCreateInfo);
  
      oListItem.set_item('Title', 'My New Item!');
      oListItem.set_item('NoticeId', 1);  // THIS IS A LOOKUP VALUE
      oListItem.set_item('User', 'administrator');
      oListItem.set_item('TimeStamp', new Date());
      oListItem.set_item('AuditType', 'Open');
  
      oListItem.update();
  
      clientContext.load(oListItem);
  
      clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
  }
  
  function onQuerySucceeded() {
      alert('Item created: ' + oListItem.get_id());
  }
  
  function onQueryFailed(sender, args) {
      alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
  }
  </code></pre>
  
  <p>The problem that I'm having is that NoticeId is a lookup value looking at a different list. Do I need to do something different to set a lookup value on an item?</p>
  
  <p>UPDATE: sorry, cracked it. Second google more fruitful.</p>
  
  <p>If anyone else is struggling, you need to create a lookup field.</p>
  
  <pre><code>    var noticeIdLookup = new SP.FieldLookupValue();
      noticeId.set_lookupId(noticeId);
  </code></pre>