JSOM: Update Item

While trying to update an item in a SharePoint 2013 list, something I’ve done many times before, I received the following error:

Error: “SCRIPT5007: Unable to get property ‘apply’ of undefined or null reference. File: ScriptResource.axd, Line 5, Column: 298”

Here’s the code that generated that error:

var siteUrl = _spPageContextInfo.webAbsoluteUrl
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Document Approval');
var oListItem = oList.getItemById(promise.responseJSON.d.results[0].ID);

oListItem.set_item('Role Status', roleStatus);
oListItem.update();

clientContext.executeQueryAsync(
 Function.createDelegate(this, this.onAddSucceeded),
 Function.createDelegate(this, this.onAddFailed)
);

function onAddSucceeded(sender, args){
  console.log("Item successfully updated!");
}

function onAddFailed(sender, args){
  console.log("Request Failed");
}

The problem was in the way I was referencing the functions (onAddSucceeded and onAddFailed). Using the ‘this’ keyword means that both onAddSucceeded and onAddFailed are part of the object which they aren’t. So the following modification worked, thanks to SharePoint stackexchange.

var siteUrl = _spPageContextInfo.webAbsoluteUrl
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Document Approval');
var oListItem = oList.getItemById(promise.responseJSON.d.results[0].ID);

oListItem.set_item('Role Status', roleStatus);
oListItem.update();

clientContext.executeQueryAsync(
 onAddSucceeded,onAddFailed
);

function onAddSucceeded(sender, args){
  console.log("Item successfully updated!");
}

function onAddFailed(sender, args){
  console.log("Request Failed");
}

Hope that makes sense. Love to hear from you.

SharePoint

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: