Skip to main content
4 of 4
pulled code from comments, formatting and grammar

Call executeQueryAsync method second time gives first time result

In the code I tried to get the data from 2 different lists. Input of the second query will be done after executing first list.

$(document).ready(function () {
    $("#SearchButton").click(function () {
        ExecuteOrDelayUntilScriptLoaded(function getData() {
            var ctx = SP.ClientContext.get_current();
            var SearchItem = document.getElementById('item').value;

            var oList = ctx.get_web().get_lists().getByTitle('All Dictionary items');

            var camlQuery = new SP.CamlQuery();
            camlQuery.set_viewXml('<view><Query><Where><Eq><FieldRef Name=\'Title\'/><Value Type=\'Text\'>' + SearchItem + '</Value></Eq></Where>' +
                '</Query><RowLimit>10</RowLimit>' +
                '</view>');

            this.collListItem = oList.getItems(camlQuery);

            ctx.load(this.collListItem, 'Include(Title, Id,SL,Description,Type1)');
            ctx.executeQueryAsync(

            function (sender, args) {
                var listItemEnumerator = collListItem.getEnumerator();
                while (listItemEnumerator.moveNext()) {
                    var oListItem = listItemEnumerator.get_current();
                    listItemInfoID = oListItem.get_item('SL').toString();
                    listItemInfo = oListItem.get_item('Type1').toString() + '  ' + oListItem.get_item('SL').toString(); //for searching next list

                    itemDescription = oListItem.get_item('Description').toString();
                }
                var desp = itemDescription.toString();
                var despCorrection = desp.replace("</font>", " ");
                despCorrection = despCorrection.substring(35);
                document.getElementById('Descriptions').value = despCorrection;
                document.getElementById('SearchedItem').value = listItemInfo.toString();
                alert('here' + listItemInfoID);

            },

            function (sender, args) {
                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            });


        }, "sp.js");

        alert('start new');
        var ContextForRelatedItem = SP.ClientContext.get_current();
        var oListForRelationItem = ContextForRelatedItem.get_web().get_lists().getByTitle('RelationWithIds');
        var camlQueryForRelatedItem = new SP.CamlQuery();
        camlQueryForRelatedItem.set_viewXml('<view><Query><Where><Eq><FieldRef Name=\'Word_SL_No\'/><Value Type=\'Number\'>' + listItemInfoID + '</Value></Eq></Where>' +
            '</Query><RowLimit>10</RowLimit>' +
            '</view>');
        alert('after query');
        this.collListItemForRelationItem = oListForRelationItem.getItems(camlQueryForRelatedItem);
        ContextForRelatedItem.load(this.collListItemForRelationItem, 'Include(Title, Id,Linked_To,Relation_No)');
        alert('after load');
        ContextForRelatedItem.executeQueryAsync(function (sender, args) {
            alert('in execute');
            var listItemEnumerator2 = collListItem.getEnumerator();
            document.getElementById('relation1item').value = '';
            document.getElementById('relation2item').value = '';
            while (listItemEnumerator2.moveNext()) {
                var oListItemForRelation = listItemEnumerator2.get_current();
                alert('in while');
                listItemInfoForRelationItem = oListItemForRelation.get_item('Relation_No').toString(); //This alert is not getting triggered.
                alert('in here jyfkufkuyfbjh' + listItemInfoForRelationItem.toString() + 'here');
                relatedItems = oListItemForRelation.get_item('Linked_To');
            }
            alert('values are' + '\n' + relatedItems.toString());
            alert('fine');
        },function (sender, args) { 
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); 
        }); 
    }); 
});

Alerts are getting triggered before oListItemForRelation.get_item('Relation_No').toString(). If I put oListItemForRelation.get_item('Description').toString() then the alert gets triggered and the value of previous list returned.

Please help. Please advise me if the approach is wrong. I have to pass a value from one column of a list to another list.