I'm trying to employ the information from here: http://www.shillier.com/archive/2013/03/04/using-promises-with-the-javascript-client-object-model-in-sharepoint-2013.aspx
The issue is have I found the record? I check the variable, its 0, I call the asyncQuery and verify the record count of 1, when I return I'm expecting the globalRecCount to be updated because of the defered call, but it ain't workin :-/
var globalRecCount = 0;
function ActivateExamSched(pwd2){
//alert("ActivateExamSched");
var deferred = $.Deferred();
var clientContext = null;
var currentweb = null;
var n = 0;
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
var SchedRec = web.get_lists().getByTitle("Exam Schedule");
var camlQuery = new SP.CamlQuery();
var q = "<View><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + pwd2 + "</Value></Eq></Where></Query></View>"
camlQuery.set_viewXml(q);
this.listItems = SchedRec.getItems(camlQuery);
//COUNT is only available IN the executeQueryAsync success function
clientContext.load(listItems, 'Include(Exam_x0020_Generated)');
alert(globalRecCount); **//<<<<<<<<<<<<<<check here, globalRecCount is 0**
clientContext.executeQueryAsync(Function.createDelegate(this, function () { deferred.resolve(this.OnFindSchedLoadSuccess); }), Function.createDelegate(this, function (sender, args) { deferred.reject(this.OnFindSchedLoadFailed); }));
alert(globalRecCount); **//<<<<<<<<<<<<<< I expect globalRecCount to be 1, because I tried to defer & get the previous call to complete, but its still 0**
}
function OnFindSchedLoadSuccess(sender, args){
var RecCount = 0;
var clientContext = new SP.ClientContext.get_current();
var listItemEnumerator = listItems.getEnumerator();
RecCount = this.listItems.get_count();
alert("recordcount = " + RecCount);
globalRecCount = RecCount;
alert(globalRecCount);
if (RecCount > 0) {
while(listItemEnumerator.moveNext())
{
var listItem = listItemEnumerator.get_current();
listItem.set_item('Exam_x0020_Generated', "TRUE");
listItem.update();
}
// clientContext.executeQueryAsync(Function.createDelegate(this, function() { UpdateTargetInReport_onUpdateQuerySucceeded(sender, args); }), Function.createDelegate(this, function(sender,args) { UpdateTargetInReport_onUpdateQueryFailed(sender, args); } ));
}
return deferred.promise();
}
function OnFindSchedLoadFailed(sender, args)
{
SP.UI.Notify.addNotification('Request Generate Exam failed. ' + args.get_message());
alert("Failed OnFindSchedLoadFailed");
}