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>
ctx.load
, this error occurs. I bet its in linevar listItemEnumerator = collListItem.getEnumerator();