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
    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");
    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

1 Answer 1


ExecuteQueryAsync is asynchronous, which means the calling code isn't going to hang around and wait for it to complete. It's just going to keep going and return undefined.

This is a big problem with the JSOM and a big reason why folks have moved on to the REST interface, where it's easier to use patterns like promises to alleviate this problem.

But, if you're using 2010 or otherwise married to the JSOM you need to make sure that caml variable is not accessed util it's had a chance to get populated. An easy way to do that would be to have your button click trigger findCreatedByUser and implement the code from getSelected inside your callback function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.