issue with JS snippet updating rows within iteration

  • 1
  • Question
  • Updated 5 months ago
  • Answered
I have tried what feels like endless variations of this snippet, but for some reason I can't get this snippet to update my rows correctly. I have commented out each line so as to hopefully describe exactly what I'm doing:

Thanks in advance for any/all help, and please pardon any obvious oversights...still learning.

var params = arguments[0];
var step = params.step; var $ = skuid.$; var models = skuid.model.map(); // Model of Goals to be updated var newGoals = models.Goals_Placeholder; // Context model which will be used to query and store the Template Goal // that matches each goal in my newGoals model during iteration var matchingTemplateGoal = models.ContextGoal; // For each New Goal, query it's matching Template Goal and // update the value of the Template_Goal__c lookup with the Id of the matching Template Goal $.each(newGoals.data,function(){ // Get reference to filterable condition on Context Goal model var contextgoalNameCondition = matchingTemplateGoal.getConditionByName('GoalName'); // Set condition of ContextGoal model to match this current goal's Goal__c value matchingTemplateGoal.setCondition(contextgoalNameCondition, this.Goal__c ); // Refresh Context Goal model, now that the condition has been set $.when(matchingTemplateGoal.updateData()).then(function(){ // Potential alternative for refreshing Context Goal model //matchingTemplateGoal.updateData(); // Get value of Context Goal Id var matchedGoalId = matchingTemplateGoal.getFieldValue(matchingTemplateGoal.getFirstRow(), 'Id'); // Update this current goal's Template Goal lookup with the Id of Context Goal var row = newGoals.updateRow(this, { Template_Goal__c: matchedGoalId}); }); });
Photo of Conlan O'Rourke

Conlan O'Rourke

  • 3,280 Points 3k badge 2x thumb

Posted 6 months ago

  • 1
Photo of Cody Taylor

Cody Taylor, Documentation

  • 474 Points 250 badge 2x thumb
Hey Conlan,

I'm not sure I can totally diagnose your snippet issues, but here are some ideas: 

  • Using skuid.model.getModel()—shortcut skuid.$M()—and the getRows() API is typically the best way to work with model data you'll be updating. The map() API and a model's data property may sometimes work, but can lead to issues. They're more useful for looking at data than updating it.
  • Is setting the reference to the ContextGoal's condition inside the iteration necessary? Perhaps try it outside of that.
  • By setting your updateRow() to a variable, it may not be getting invoked as intended. Try removing the variable assignment.
I'm not a JS expert, so you may need to tinker more. But give something like this a shot:
var $ = skuid.$;
var newGoals = skuid.$M('Goals_Placeholder')
var matchingTemplateGoal = skuid.$M('ContextGoal')
var contextGoalCondition = matchingTemplateGoal.getConditionByName('GoalName');
$.each(newGoals.getRows(), function() {
  matchingTemplateGoal.setCondition(contextGoalCondition, this.Goal__c)
  $.when(matchingTemplateGoal.updateData()).then(function(){
    var matchedGoalId = matchingTemplateGoal.getFieldValue(matchingTemplateGoal.getFirstRow(), 'Id');    
    newGoals.updateRow(this, {Template_Goal__c: matchedGoalId});
  })
})
(Edited)
Photo of Conlan O'Rourke

Conlan O'Rourke

  • 3,280 Points 3k badge 2x thumb
Hey Cody,

Sorry to be so slow and unresponsive, this project got put on the backburner. Thanks for your help on this. I've tried out your suggestions, and am still not getting the snippet to function as expected.

It appears the issue is with the row updates. The condition sets fine within the iteration, and I was able to print out each matching Goal Id to the console. 

However, for some reason it's not updating my newGoals records.

Any further helping hand you could provide is much appreciated.

var params = arguments[0];
var step = params.step;
var $ = skuid.$;

// Model of Goals to be updated
var newGoals = skuid.$M('Goals_Placeholder');

// Context model which will be used to query and store the Template Goal 
// that matches each goal in my newGoals model during iteration
var matchingTemplateGoal = skuid.$M('ContextGoal');

// Get reference to filterable condition on Context Goal model
var contextgoalNameCondition = matchingTemplateGoal.getConditionByName('GoalName');

var rowsToUpdate = {};

// For each New Goal, query it's matching Template Goal and 
// update the value of the Template_Goal__c lookup with the Id of the matching Template Goal
$.each(newGoals.getRows(),function(){

    // Set condition of ContextGoal model to match this current goal's Goal__c value
    matchingTemplateGoal.setCondition(contextgoalNameCondition, this.Goal__c );
   
    // Refresh Context Goal model, now that the condition has been set
    $.when(matchingTemplateGoal.updateData()).then(function(){
   
    // Get value of Context Goal Id
    var matchedGoalId = matchingTemplateGoal.getFieldValue(matchingTemplateGoal.getFirstRow(), 'Id');    
   
    //update this row with Matched Template Id
    rowsToUpdate[this.Id] = { Template_Goal__c: matchedGoalId };

});
});

// update & save newGoals with Matched Template Id
newGoals.updateRows( rowsToUpdate );
newGoals.save();