Domanda

I am iterating List A and fetching each items title. Then I match the title against a column in List B. If a match is found I add relevant columns in list B and output. I have put 2 items in list A as test data. Though I can see the code in List B being called twice (I get the alert 'Part2 -' xx) I only get the 'Output alert' once and only the last item in List A is being worked on. I think the problem is in the 2nd section while loop. Any ideas please?

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(TeamRequests, "SP.js");

function TeamRequests() {
    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('TeamRequests');        
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View></View>');
    this.collListItem = oList.getItems(camlQuery);
    clientContext.load(collListItem);        
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededTeamRequests), Function.createDelegate(this, this.onQueryFailedTeamRequests)); 
}

function onQuerySucceededTeamRequests(sender, args) {
    var listItemInfo = ''; var iCount=0;

    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();

       listItemInfo += '\nID: ' + oListItem.get_id() +  '\nTitle: ' + oListItem.get_item('Title');

// Code to get Mandays
//alert (oListItem.get_item('Title'));
    Mandays(oListItem.get_item('Title'));

        }
  // alert (listItemInfo);
}

function onQueryFailedTeamRequests(sender, args) {
    alert('TeamRequests Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}    
function Mandays(trTitle)
{
    alert (trTitle);
    this.trTitle = trTitle;
    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('Mandays');        
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View></View>');
    this.collListItem = oList.getItems(camlQuery);
    clientContext.load(collListItem);        
    //clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededMandays), Function.createDelegate(this, this.onQueryFailedMandays));        

     clientContext.executeQueryAsync(Function.createDelegate(this, function(){onQuerySucceededMandays(trTitle);}), Function.createDelegate(this, this.onQueryFailedMandays));        



}

//function onQuerySucceededMandays(sender, args) {


function onQuerySucceededMandays(trPassTitle) {

alert ('part2 -' +  trPassTitle);
    var option1=0; option2=0; option3=0;

    var listItemInfo = ''; var iCount=0;

    var listItemEnumerator = collListItem.getEnumerator();


    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();

       listItemInfo += '\nID: ' + oListItem.get_id() +  '\nTitle: ' + oListItem.get_item('Title')+  '\nTRTitle: ' + oListItem.get_item('TeamRequest_x003a_Title').get_lookupValue();

        //test for Team
        alert ('trPassTitle -'+ trPassTitle + '\n Man -'+ oListItem.get_item('TeamRequest_x003a_Title').get_lookupValue());
        if (trPassTitle == (oListItem.get_item('TeamRequest_x003a_Title').get_lookupValue())) 
        {
            if (oListItem.get_item('Item') == 'Option1') {
                option1 += parseFloat(oListItem.get_item('EstCost')); }
            if (oListItem.get_item('Item') == 'Option2') {
                option2 += parseFloat(oListItem.get_item('EstCost')); }
            if (oListItem.get_item('Item') == 'Option3') {
                option3 += parseFloat(oListItem.get_item('EstCost')); }
        }

    } // end while
    alert ('Output Option1 ' + option1 + '\nOption2 ' + option2 + '\nOption3 ' + option3);

    //  alert (listItemInfo);
    //return;
}

function onQueryFailedMandays(sender, args) {

    alert('Mandays Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}



</script>
È stato utile?

Soluzione

I refactored (not tested!) your code - you shouldnt have any scoping issues now.

/* global SP */
/* global ExecuteOrDelayUntilScriptLoaded */
(function() {
    //here we are in our own scope - no messing with other globals (SharePoint has a lot)
    ExecuteOrDelayUntilScriptLoaded(function() {
        //if you use a function only once - you dont necessarily need a function 
        var clientContext = new SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('TeamRequests');
        var camlQuery = new SP.CamlQuery.createAllItemsQuery();
        var collListItem = oList.getItems(camlQuery);
        clientContext.load(collListItem);
        clientContext.executeQueryAsync(
            function() {
                //success
                var listItemInfo = '';
                var listItemEnumerator = collListItem.getEnumerator();

                while (listItemEnumerator.moveNext()) {
                    var oListItem = listItemEnumerator.get_current();
                    listItemInfo += '\nID: ' + oListItem.get_id() + '\nTitle: ' + oListItem.get_item('Title');
                    // Code to get Mandays
                    Mandays(oListItem.get_item('Title'));
                }
                //alert (listItemInfo);
            },
            function(sender, args) {
                console.log(sender);
                console.log(args);
                alert('TeamRequests Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            }
        );
    }, "SP.js");

    function Mandays(trTitle) {
        var clientContext = new SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('Mandays');
        var camlQuery = new SP.CamlQuery.createAllItemsQuery();
        var collListItem = oList.getItems(camlQuery);
        clientContext.load(collListItem);
        clientContext.executeQueryAsync(
            function() {
                //success
                alert('part2 -' + trTitle);
                var option1 = 0,
                    option2 = 0,
                    option3 = 0,
                    listItemInfo = '';

                var listItemEnumerator = collListItem.getEnumerator();
                while (listItemEnumerator.moveNext()) {
                    var oListItem = listItemEnumerator.get_current();

                    listItemInfo += '\nID: ' + oListItem.get_id() + '\nTitle: ' + oListItem.get_item('Title') + '\nTRTitle: ' + oListItem.get_item('TeamRequest_x003a_Title').get_lookupValue();

                    //test for Team
                    alert('trPassTitle -' + trTitle + '\n Man -' + oListItem.get_item('TeamRequest_x003a_Title').get_lookupValue());
                    if (trTitle == (oListItem.get_item('TeamRequest_x003a_Title').get_lookupValue())) {
                        if (oListItem.get_item('Item') == 'Option1') {
                            option1 += parseFloat(oListItem.get_item('EstCost'));
                        }
                        if (oListItem.get_item('Item') == 'Option2') {
                            option2 += parseFloat(oListItem.get_item('EstCost'));
                        }
                        if (oListItem.get_item('Item') == 'Option3') {
                            option3 += parseFloat(oListItem.get_item('EstCost'));
                        }
                    }
                }
                alert('Output Option1 ' + option1 + '\nOption2 ' + option2 + '\nOption3 ' + option3);
            },
            function(sender, args) {
                console.log(sender);
                console.log(args);
                alert('TeamRequests Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            }
        );
    }
}());

Altri suggerimenti

you should not call a function in loop which is having executeQueryAsyc() method in it because of the asynchronous nature of the method. The while loop will create multiple async calls and code execution will get messed up to decide which querySucceed() method to execute. Thats why the error The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
I have made some changes to your code. Check if it works for you.

    ExecuteOrDelayUntilScriptLoaded(TeamRequests, "SP.js");
    function TeamRequests() {
        var clientContext = new SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('TeamRequests');        
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View></View>');
        this.collListItem = oList.getItems(camlQuery);
        clientContext.load(collListItem);        
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededTeamRequests), Function.createDelegate(this, this.onQueryFailedTeamRequests));        
    }
    function onQuerySucceededTeamRequests(sender, args) {
        var listItemInfo = ''; var iCount=0;
        var listItemEnumerator = collListItem.getEnumerator();
        var arrTitle = [];
        while (listItemEnumerator.moveNext()) {
            var oListItem = listItemEnumerator.get_current();
            listItemInfo += '\nID: ' + oListItem.get_id() +  '\nTitle: ' + oListItem.get_item('Title');
            //Mandays(oListItem.get_item('Title'));
            if(oListItem.get_item('Title')!= null){
                arrTitle.push(oListItem.get_item('Title'));
            }
        }
        Mandays(arrTitle);
    }
    function onQueryFailedTeamRequests(sender, args) {
        alert('TeamRequests Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    function Mandays(arrTitle)
    {
        var clientContext = new SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('Mandays');        
        var camlQuery = new SP.CamlQuery();
        var strQueryStart = "<View><Query><Where><In><FieldRef Name='TeamRequest_x003a_Title' /><Values>";
        var strQueryMiddle = "";
        for(var i = 0; i < arrTitle.length; i++){
            strQueryMiddle += "<Value Type='Lookup'>"+arrTitle[i]+"</Value>";  /////for getting only those "TeamRequest_x003a_Title" which are equals to Title from "TeamRequests"
        }
        var strQueryEnd = "</Values></In></Where></Query></View>";
        camlQuery.set_viewXml(strQueryStart + strQueryMiddle + strQueryEnd);
        this.collListItem = oList.getItems(camlQuery);
        clientContext.load(collListItem);        
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededMandays), Function.createDelegate(this, this.onQueryFailedMandays));
    }
    //function onQuerySucceededMandays(sender, args) {
    function onQuerySucceededMandays(trPassTitle) {
        var option1=0; option2=0; option3=0;
        var listItemInfo = ''; var iCount=0;
        var listItemEnumerator = collListItem.getEnumerator();
        while (listItemEnumerator.moveNext()) {
            var oListItem = listItemEnumerator.get_current();
            listItemInfo += '\nID: ' + oListItem.get_id() +  '\nTitle: ' + oListItem.get_item('Title')+  '\nTRTitle: ' + oListItem.get_item('TeamRequest_x003a_Title').get_lookupValue();
            /////we don't need this condition here as we already have such records which full fill this condition
            //if (trPassTitle == (oListItem.get_item('TeamRequest_x003a_Title').get_lookupValue())) 
            //{
            if (oListItem.get_item('Item') == 'Option1') {
                option1 += parseFloat(oListItem.get_item('EstCost')); }
            if (oListItem.get_item('Item') == 'Option2') {
                option2 += parseFloat(oListItem.get_item('EstCost')); }
            if (oListItem.get_item('Item') == 'Option3') {
                option3 += parseFloat(oListItem.get_item('EstCost')); }
            //}
        } // end while
        alert ('Output Option1 ' + option1 + '\nOption2 ' + option2 + '\nOption3 ' + option3);
    }
    function onQueryFailedMandays(sender, args) {
        alert('Mandays Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top