HELP: Loop stuck on first record in javascript

  • 1
  • Question
  • Updated 5 months ago
  • In Progress
  • (Edited)
Here is our full code
_____________________


var params = arguments[0],
$ = skuid.$;
var field = arguments[0];
var model = field.model;
var row = field.row;
var HtmlString = '';
var ActName = row.Name;
var Piece = row.Piece__r;
    if(Piece){
        console.log(Piece.Name);
        //var PieceWriterModel = skuid.model.getModel("MMA_Music_Piece_Writer");
        //var MusicPieceCondition = PieceWriterModel.getConditionByName("MMA_Music_PieceId");
        //var PieceId = Piece.Id;
        var Piece_Name = Piece.Name;
        HtmlString = '<div><ul><strong><font size="3" style="font-family:Georgia">'+ Piece.Name + '</strong></ul></div>';
        //var cellElem = field.element;
        //console.log(HtmlString);
        //cellElem.html(HtmlString);
        }
    else{
        HtmlString = ActName + '';
        window.alert(ActName + ' - There is no peice in thie Act.')
        //cellElm = field.element;
        }
        
var NoOfWriters = 0;
var MusicWriter ='none';
    if(Piece){
        var PieceWriterModel = skuid.model.getModel("MMA_Music_Piece_Writer");
        var MusicPieceCondition = PieceWriterModel.getConditionByName("MMA_Music_PieceId");
        var PieceId = Piece.Id;
        PieceWriterModel.setCondition(MusicPieceCondition,PieceId);
        console.log('After Set condition' + PieceId + ' PieceId ');
        PieceWriterModel.updateData(function(){
            NoOfWriters = PieceWriterModel.data.length;
            if(NoOfWriters>0){
                for(i=0;i<NoOfWriters;i++){
                    if(PieceWriterModel.data[i].MMA_Music_Composer__c){
                    MusicWriter = PieceWriterModel.data[i].MMA_Music_Composer__r.Name;
                    HtmlString = HtmlString + '<div><ul><em><font color="grey" size="3" style="font-family:Georgia">'+ '&nbsp;' + '&nbsp;' + '&nbsp;' + '&nbsp;'+ MusicWriter +'';
                    if(PieceWriterModel.data[i].MMA_Music_Writer_Role__r){
                        var MusicWriterRole = PieceWriterModel.data[i].MMA_Music_Writer_Role__r.Name;
                        var Writer_Role_String = ', '+ MusicWriterRole +'</em></ul></div>';
                        HtmlString = HtmlString + Writer_Role_String;
                        
                        }
                    else{
                        }}
                        else{
                            window.alert(Piece_Name + ' - This Piece does not have any associated Music Writers.');
                        }
                }
                var cellElem = field.element;
                cellElem.html(HtmlString);
            }
        });
       
    
        
    }
    else{
        cell.Elem.html(HtmlString);
    }

__________________________

THE PART BELOW IS NOT LOOPING PROPERLY AND IS GETTING STUCK ON THE FIRST RECORD. ANY IDEAS FOR HOW TO GET IT TO LOOP PROPERLY?


var NoOfWriters = 0;
var MusicWriter ='none';
    if(Piece){
        var PieceWriterModel = skuid.model.getModel("MMA_Music_Piece_Writer");
        var MusicPieceCondition = PieceWriterModel.getConditionByName("MMA_Music_PieceId");
        var PieceId = Piece.Id;
        PieceWriterModel.setCondition(MusicPieceCondition,PieceId);
        console.log('After Set condition' + PieceId + ' PieceId ');
        PieceWriterModel.updateData(function(){
            NoOfWriters = PieceWriterModel.data.length;
            if(NoOfWriters>0){
                for(i=0;i<NoOfWriters;i++){
                    if(PieceWriterModel.data[i].MMA_Music_Composer__c){
                    MusicWriter = PieceWriterModel.data[i].MMA_Music_Composer__r.Name;
                    HtmlString = HtmlString + '<div><ul><em><font color="grey" size="3" style="font-family:Georgia">'+ '&nbsp;' + '&nbsp;' + '&nbsp;' + '&nbsp;'+ MusicWriter +'';
                    if(PieceWriterModel.data[i].MMA_Music_Writer_Role__r){
                        var MusicWriterRole = PieceWriterModel.data[i].MMA_Music_Writer_Role__r.Name;
                        var Writer_Role_String = ', '+ MusicWriterRole +'</em></ul></div>';
                        HtmlString = HtmlString + Writer_Role_String;
                        
                        }
                    else{
                        }}
                        else{
                            window.alert(Piece_Name + ' - This Piece does not have any associated Music Writers.');
                        }
                }
                var cellElem = field.element;
                cellElem.html(HtmlString);
            }
        });
       
    
        
    }
    else{
        cell.Elem.html(HtmlString);
    }
Photo of Anna Orias

Anna Orias

  • 1,468 Points 1k badge 2x thumb

Posted 6 months ago

  • 1
Photo of Mike Dwyer

Mike Dwyer

  • 3,390 Points 3k badge 2x thumb
Can you explain what you mean by 'stuck'? You mention a loop but I don't see a for statement.

What do you see on the console? Are there any error messages? What are your console.log statements writing? Is the alert firing?

My initial suspicion is an empty field value that the model is actually including as undefined, but you are testing for an empty string with ''. The console should give an error message to that effect, however.
Photo of Anna Orias

Anna Orias

  • 1,468 Points 1k badge 2x thumb
There is a for loop 

         for(i=0;i<NoOfWriters;i++){
                    if(PieceWriterModel.data[i].MMA_Music_Composer__c){
                    MusicWriter = PieceWriterModel.data[i].MMA_Music_Composer__r.Name;
                    HtmlString = HtmlString + '<div><ul><em><font color="grey" size="3" style="font-family:Georgia">'+ '&nbsp;' + '&nbsp;' + '&nbsp;' + '&nbsp;'+ MusicWriter +'';


When the Snipet is rendering values, model is filtered for only last pieceid. But we want Snipet to filter the model based on the row value (piece id). 


Once the model is filtered for the each row value, update function on the model should return the values based on the row value.
Currently the Snipet is returning values corresponding to only the last pieceid for all the rows
Photo of Mike Dwyer

Mike Dwyer

  • 3,390 Points 3k badge 2x thumb
I was reading your original on my phone and missed the for( loops. Oops!
I still don't understand 'stuck.' That suggests the snippet dies on or immediately after the first iteration, when i=0, or perhaps at i=1.
Are there any console messages when the snippet runs?
I am also looking at two if statements:
  •  if(PieceWriterModel.data[i].MMA_Music_Composer__c)
  • if(PieceWriterModel.data[i].MMA_Music_Writer_Role__r)
I am thinking these cannot both be correct, and I am thinking the relational indicator, __r, is not valid without a field reference. Should it be __c? They may both be right, but I'm not able to check easily at the moment.

The comment about "last pieceid  for all the rows" has me wondering if the snippet is being called through a custom renderer on a table column/field. Do you then see on every row in the table the expected snippet results from the last row displayed? What happens if you change the number of rows displayed? Do you also have a Search box or a Filter with which you can limit the number of rows to 1, or to a different data set; what happens then? Could you also add a console.log(HtmlString); immediately after the for loop terminates?