0

I want to get the return value true or false of res2 and use it in the last return. So it can works like the first validation on PreSaveAction the one validating demoField value.

function ViewItem(listTitle, Success, Error) {
 var context = new SP.ClientContext.get_current();
 var web = context.get_web();
 var list = web.get_lists().getByTitle(listTitle);
 var query = SP.CamlQuery.createAllItemsQuery();
 var allItems = list.getItems(query);
 context.load(allItems, 'Include(Title, EventDate, tiempo2)');
 context.executeQueryAsync(function(){
   var currentTitle = SPUtility.GetSPFieldByInternalName('EventDate').GetValue();
   var res = "No";
   for (var i = 0; i < allItems.get_count(); i++) {
    var item = allItems.get_item(i);
    console.log(item.get_item('tiempo2') + ' - ' + currentTitle );
    if (currentTitle == item.get_item('tiempo2')){
     res = "Si"; 
     console.log('There is an event with the same Start Date on DemoTrainingRoom2' 
      + ' ' + item.get_item('tiempo2') + ' - ' + currentTitle);
              
          }
   }
   return Success(res);
  },
  Error
 );
}

function PreSaveAction() {
 var time = SPUtility.GetSPFieldByInternalName('EventDate').GetValue();
 alert(time + " Current Start Time ");
 
 if(SPUtility.GetSPField('demoField').GetValue() == "no") {
  alert('No need for validation');
  return true;
 } 
 else if(SPUtility.GetSPField('demoField').GetValue() == "yes") {
  
  var res2 = ViewItem('demoTrainingRoom2', function(res){
    console.log('Result: ' + res);
    if(res == "Si") 
    {
     alert(res);
     return false; //I want this or
    }
    else
    {
     alert(res); 
     return true; // This
    }  
   },
   function (sender, args) {
    alert("failed. Message:" + args.get_message());
   }
  )
  ;
  return res2; // Over here
 }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
suizzak
  • 19
  • 4
  • You can't. All values obtained by async should be used in async callback, or functions called by the callback. You can't "return" a value from async. Figure out what you need `res2` for, and execute that inside the `ViewItem` callback. – Amadan Oct 20 '15 at 01:21
  • is there any workaround for this? – suizzak Oct 20 '15 at 01:22
  • bring the action to the data, not the data to the action like you do in php... – dandavis Oct 20 '15 at 01:41

0 Answers0