I'm 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)
The 'Output alert' (after 2nd while loop) seems random, sometimes only one alert displays and sometimes 2 alerts are displayed. I think the problem is in the 2nd section while loop.
F12 shows 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' -
is this related to this behaviour?
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)); //clientContext.executeQueryAsync(Function.createDelegate(this, function(){onQuerySucceededTeamRequests('cat');}), Function.createDelegate(this, this.onQueryFailedTeamRequests)); } function onQuerySucceededTeamRequests(sender, args) { //function onQuerySucceededTeamRequests(vcat) { var listItemInfo = ''; var iCount=0; // alert (vcat); 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); 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,'Include(Item,TeamRequest_x003a_Title,Title,Id)'); 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 listItemEnumerator1 = collListItem.getEnumerator(); while (listItemEnumerator1.moveNext()) { var oListItem = listItemEnumerator1.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 (trPassTitle + '\n 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>
- Edited by orange juice jones Friday, June 19, 2015 10:01 AM