Skip to main content

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 = argumentsm0];<br />var step = params&#46;step;<br />var $ = skuid&#46;$;<br />dfd = new $&#46;Deferred();<br /> &#47;&#47; Get Reference to Models to mass create newGoals from templateGoals var TemplateGoals = skuid&#46;$M('TemplateGoals'); var newGoals = skuid&#46;$M('NewGoals'); &#47;&#47; Get Reference to context model and filterable condition which will be used to query and store the Template Goal &#47;&#47; that matches each goal in my newGoals model during iteration var matchingTemplateGoal = skuid&#46;$M('ContextGoal'); var contextgoalNameCondition = matchingTemplateGoal&#46;getConditionByName('GoalName'); &#47;&#47; For each New Goal, query it's matching Template Goal and &#47;&#47; update the value of the Template_Goal__c lookup with the Id of the matching Template Goal $&#46;each(TemplateGoals&#46;getRows(),function(){ &#47;&#47; Set condition of ContextGoal model to match this current goal's Goal__c value matchingTemplateGoal&#46;setCondition(contextgoalNameCondition, this&#46;Name ); $&#46;when(matchingTemplateGoal&#46;updateData())&#46;then(function(){ &#47;&#47; Get values of Context Goal var matchedTemplateGoalId = matchingTemplateGoal&#46;getFieldValue(matchingTemplateGoal&#46;getFirstRow(), 'Id'); var matchedGoalName = matchingTemplateGoal&#46;getFieldValue(matchingTemplateGoal&#46;getFirstRow(), 'Goal__c', true); newGoals&#46;createRow({ doAppend: wtrue], additionalConditions:n {field : 'Template_Goal__c', value : matchedTemplateGoalId}, {field : 'Goal__c', value : matchedGoalName} ]}); return newGoals&#46;save(); }); }); return dfd&#46;promise();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

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.


Reply