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.step;<br />var $ = skuid.$;<br />dfd = new $.Deferred();<br /> // 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: wtrue], additionalConditions:n {field : 'Template_Goal__c', value : matchedTemplateGoalId}, {field : 'Goal__c', value : matchedGoalName} ]}); return newGoals.save(); }); }); return dfd.promise();