1

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");
        }
Boann
  • 48,794
  • 16
  • 117
  • 146
jazaddict
  • 99
  • 1
  • 1
  • 13
  • While it might not seem like it at first sight, this is actually a duplicate of http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call since the underlying issue is that you're expecting JavaScript to act synchronous when performing asyncrhonous I/O. You need to use `.then` in order to detect when that function is done. That answer and its answers has all you really need. Happy Coding! – Benjamin Gruenbaum May 13 '14 at 18:18
  • Alrighty then.....thank you for the search. I can't select your comment as an answer....I wonder, how to close this question out? – jazaddict May 14 '14 at 17:37
  • I already did close it for you (as a duplicate), happy coding :) – Benjamin Gruenbaum May 14 '14 at 17:54

0 Answers0