I'm pulling information from an Excel spreadsheet and trying to check the rows against data from a SharePoint list to find duplicates.
The executeQueryAsync()
in the itemIsDuplicate()
function is causing the promise to try to resolve before the promise is returned resulting in the error message "Unable to get property 'then' of undefined or null reference"
Here are the relevant sections of code:
for (var i = 0; i < rows.length; i++) {
var newItem = {
week: rows[i].Week._Default,
customer: rows[i].Customer._Default,
resource: rows[i].Resource,
title: rows[i].Title._Default,
category: rows[i].Category._Default,
notes: rows[i].Notes._Default,
plannedHours: rows[i].PlannedHours._Default,
percentComplete: rows[i].PercentComplete._Default,
extID: rows[i].ExtID._Default,
actualHours: rows[i].ActualHours._Default,
type: rows[i].Type._Default,
dmRequest: rows[i].DMRequest._Default
}
itemIsDuplicate(newItem).then(function (isDuplicate) {
if (!isDuplicate) {
newItems.push(newItem);
}
else {
duplicates.push(newItem);
}
})
}
$.when.apply($, promises).done(function () {
//process results...
}
function itemIsDuplicate(item, newItems) {
var deferred = new $.Deferred();
promises.push(deferred);
var isDuplicate = false;
var duplicateQuery = new SP.CamlQuery();
duplicateQuery.set_viewXml(
'<View><Query><Where>' +
'<And>'+
'<Eq>' +
'<FieldRef Name= \'Week\' />' +
'<Value Type= \'DateTime\' IncludeTimeValue=\'FALSE\'>' + new Date(item.week).toISOString() + '</Value>' +
'</Eq>' +
'<And>' +
'<Eq>' +
'<FieldRef Name= \'LIS_x0020_Analyst\' />' +
'<Value Type= \'Text\'>' + item.resource + '</Value>' +
'</Eq>' +
'<Eq>' +
'<FieldRef Name= \'Title\' />' +
'<Value Type= \'Text\'>' + item.title + '</Value>' +
'</Eq>' +
'</And>' +
'</And>' +
'</Where></Query></View>'
)
var duplicateItemsColl = worklist.getItems(duplicateQuery);
clientContext.load(duplicateItemsColl);
clientContext.executeQueryAsync(
function () {
if (duplicateItemsColl.get_count() > 0) {
isDuplicate = true;
}
deferred.resolve(isDuplicate);
return deferred.promise();
},
function (sender, args) {
console.log("Error message here...")
}
);
}
Does anybody have a good solution to avoid this problem?
I'm pretty new to JavaScript development in SharePoint, and the use of $.Deferred, so this might be simple but the answer has eluded me so far. Thanks.