javascript save issue with deferred / promise approach

Conlan O'Rourke
Conlan O'Rourke ✭✭✭✭
edited August 21, 2018 in Questions
I'm struggling to implement the deferred / promise approach as outlined here, here, and here

My use case is to:

1) Mass create rows with a different lookup value in each row
2) Update / Refresh a second model
3) Use a value from my second model as the lookup value in each row
4) SAVE!

I can get it to do steps 1-3 perfectly, but can't get it to save my new rows.

I've tried a bunch of different things...(e.g. chaining .then(), putting save action as next action in sequence, etc)...nothing so far...

Any advice would be much appreciated.
var params = arguments[0];
var step = params.step;
var $ = skuid.$;
dfd = new $.Deferred();
// Get Reference to Models to mass create newGoals from templateGoals var TemplateGoals = skuid.$M('TemplateGoals'); var newGoals = skuid.$M('NewGoals'); // Get Reference to context model and filterable condition 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'); var contextgoalNameCondition = matchingTemplateGoal.getConditionByName('GoalName'); // 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(TemplateGoals.getRows(),function(){ // Set condition of ContextGoal model to match this current goal's Goal__c value matchingTemplateGoal.setCondition(contextgoalNameCondition, this.Name ); $.when(matchingTemplateGoal.updateData()).then(function(){ // Get values of Context Goal var matchedTemplateGoalId = matchingTemplateGoal.getFieldValue(matchingTemplateGoal.getFirstRow(), 'Id'); var matchedGoalName = matchingTemplateGoal.getFieldValue(matchingTemplateGoal.getFirstRow(), 'Goal__c', true); newGoals.createRow({ doAppend: [true], additionalConditions:[ {field : 'Template_Goal__c', value : matchedTemplateGoalId}, {field : 'Goal__c', value : matchedGoalName} ]}); return newGoals.save(); }); }); return dfd.promise();               

Comments

  • Mark L
    Mark L ✭✭✭✭

    It's very difficult to dissect your code here because it's all showing up on one commented line, however if I'm seeing this correctly then you appear to be running an each loop and inside the each loop calling $.when -- this loops synchronously and calls the when multiple times asynchronously. This doesn't seem correct. You also never do a dfd.resolve(); to indicate that the deferred is resolved, instead you return the deferred promise from newGoals.Save() whis is also in the loop but in the asynchronous when.

    I'm not sure what you're doing with updateData here inside a loop on each row, and its definitely not advisable to call a when (asynchronous) within a loop (synchronous) here. Also in general it would make sense to return the deferred promise you create originally, rather than the deferred promise of the newGoals.Save() function, and this snippet ultimately seems to actually complete (deferred promise) when the first newGoals.save() occurs in the loop. It looks very strange and out of order.

    Are the second model's lookup value in each row a result of a formula using MODEL_LOOKUP? If that's the case (and the updateData function actually works..) then you should only need to run updateData once, rather than in the for loop.


    I'm not sure the syntax to post a block of code in this forum.. and I'm still not quite sure what you're trying to do. Why are you looping to keep setting different conditions on a model and then trying to update the model data? Are you requering the model with each new condition? I'm not familiar with trying to utilize updateData in this way. If you do want to do some sort of a loop here that relies on the updateData() to be synchronous in the loop, then instead of running an $.each here, you'd likely want to just keep running a when inside a function that on .then, reruns the function, iterating rows to use, until all rows have been iterated through. That would make the asynchronous $when operation happen in sequence row by row, instead of right now where the loop happens first and the $.whens all fire off at once and the snippet returns when the first one completes a newGoals.save(). To get the save to happen, as a final step of the function (once you run out of rows to iterate using $when) you can run the newGoals.save() also within a $when, and when that is done you can do dfd.resolve();

    This is probably really weird to explain in text, and again I'm not entirely sure how you're using setCondition / updateData and what you're trying to do with that, but hopefully this explanation of how to run a sequence of synchronous / asynchronous operations is somewhat helpful.. If I knew how to embed code here I could maybe give you an example.. though it would also be very helpful to have a better understanding of what you're trying to do with the setCondition and the updateData.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!