Code JSOM not executing as expected

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>


June 19th, 2015 9:52am

Hi,

I think you are facing this issue due to javascript's asynchronous behaviour. You can try your code with "Deferred" & "Promise" to make it work synchronously. Here you can get reference code:

http://stackoverflow.com/questions/10664466/syntax-for-jquery-deferred-making-synchronous-function-return-promise

http://stackoverflow.com/questions/20122227/jquery-deferred-and-promise-for-sequential-execution-of-synchronous-and-asynchro

Alternatively you can store your 1st list data in an array first and later loop that array for 2nd list data extraction and save your 2nd list data in other array for later

Free Windows Admin Tool Kit Click here and download it now
June 19th, 2015 10:49am

Hi

The error is reported in developer tools when all items are not alerted.
'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'

Could it have something to do with   camlQuery.set_viewXml ?

Thanks

June 19th, 2015 12:40pm

Hi,

I recommend to use the code demo below to achieve your goal:

<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 oList1 = clientContext.get_web().get_lists().getByTitle('Mandays');        
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View></View>');
    var camlQuery1 = new SP.CamlQuery();
    camlQuery1.set_viewXml('<View></View>');
    this.collListItem = oList.getItems(camlQuery);
    this.collListItem1 = oList1.getItems(camlQuery1);
 	clientContext.load(collListItem); 
 	clientContext.load(collListItem1);       
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededTeamRequests), Function.createDelegate(this, this.onQueryFailedTeamRequests));        
   
}

function onQuerySucceededTeamRequests(sender, args) {	
     compare();
}

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

function compare()
{
	    var listItemEnumerator = collListItem.getEnumerator();    
                    while (listItemEnumerator.moveNext()) {
	    var oListItem = listItemEnumerator.get_current();        
		Mandays(oListItem.get_item('Title'));	
	} 
}

function Mandays(trTitle){

	var option1=0; option2=0; option3=0;	 
    var listItemEnumerator1 = collListItem1.getEnumerator();    
    while (listItemEnumerator1.moveNext()) {
        var oListItem1 = listItemEnumerator1.get_current();
		if (trTitle == (oListItem1.get_item('TeamRequest_x003a_Title').get_lookupValue())) 
		{
		if (oListItem1.get_item('Item') == 'Option1') {
			option1 += parseFloat(oListItem1.get_item('EstCost')); }	
		if (oListItem1.get_item('Item') == 'Option2') {
			option2 += parseFloat(oListItem1.get_item('EstCost')); }
		if (oListItem1.get_item('Item') == 'Option3') {
			option3 += parseFloat(oListItem1.get_item('EstCost')); }
		}			
		} // end while		
		alert (trTitle + '\n Output Option1 ' + option1 + '\nOption2 ' + option2 + '\nOption3 ' + option3);
}

</script>

Thanks,

Victoria

Free Windows Admin Tool Kit Click here and download it now
June 22nd, 2015 2:30am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics