I'm trying to get the value of a caml query to a variable in my getSelected function using a return statement. I'm getting undefined and I'm not sure why. I have tested the findCreatedByUser function and it does work correctly. getSelected is kicked off by a button. fieldValue is a global variable.
//Get selected items from customer location view
function getSelected() {
siteURL = "/sites/SPAPPS01/accessmanagement/";
listName2 = "Customer Locations";
fieldValue = findCreatedByUser(); // <====== Not returning ID from caml query in findCreatedByUser function
console.log(fieldValue);
var Astate, Aname;
var ctx = SP.ClientContext.get_current();
var clientContext = new SP.ClientContext();
var targetList = clientContext.get_web().get_lists().getByTitle(listName2);
var SelectedItems = SP.ListOperation.Selection.getSelectedItems(ctx);
var items = [];
var selItemLength = 0;
for (var i in SelectedItems) {
var id = SelectedItems[i].id;
var item = targetList.getItemById(id);
clientContext.load(item, "A_x0020_State", "A_x0020_Name");
items.push(item);
}
clientContext.executeQueryAsync(function() {
for (var i = 0; i < items.length; i++) {
Aname = item.get_item('A_x0020_Name');
Astate = item.get_item('A_x0020_State');
writeSelected(Astate, Aname);
}
}, failure);
} //End getSelected
//Find newly created item by current user
function findCreatedByUser() {
var camlValue;
var context = SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle("Projects");
var caml = new SP.CamlQuery();
caml.set_viewXml("<View><Query><ViewFields><FieldRef Name='ID' /></ViewFields><OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy><Where><Eq><FieldRef Name='Author' /><Value Type='Integer'><UserID /></Value></Eq></Where></Query><RowLimit>1</RowLimit></View>");
listItems = list.getItems(caml);
context.load(listItems, "Include(ID)");
context.executeQueryAsync(function() {
var count = listItems.get_count();
var listEnumerator = listItems.getEnumerator();
while (listEnumerator.moveNext()) {
listItem = listEnumerator.get_current();
camlValue = listItem.get_item("ID");
return camlValue;
}
}, failure);
} //End findCreatedByUser