There’s a long story here. I’ll try to make it as short as possible…
I’m creating a dynamic model, but when I save it, the server doesn’t update. When I re-query other models on the same object, the fields saved by the dynamic model are not updated.
Here’s the entire code. I’ve bolded the definition of the dynamic model in question, and the update and save portions of the code, which seem to be the most relevant.
(function(skuid){ <br>var $ = skuid.$;<br>$(document.body).one('pageload',function(){<br>//Get all models on Patient_Case__c and Interaction__c<br>var interactionModels = c];<br>$.each(skuid.model.map(),function(){<br>if (this.objectName == 'Interaction__c') {<br>interactionModels.push(this);<br>}<br>});<br>// If there are Interaction models, create a patient case model, an interaction model, and subscribe to the save event<br>if (interactionModels.length) {<br>//Create model on Patient_Case__c for collecting Pregnancy Intention and Date Most Recent Completed Interaction<br>var patientCase = new skuid.model.Model();<br>patientCase.objectName = 'Patient_Case__c';<br>patientCase.id = 'PatientCase_UpdateIntention';<br>patientCase.recordsLimit = 1;<br>patientCase.fields = C<br>{ id: 'Id'},<br> { id: 'Pregnancy_Intention__c' },<br> { id: 'Date_Most_Recent_Completed_Interaction__c' }<br>];<br>patientCase.conditions = .<br> { <br> type: 'fieldvalue',<br> field: 'Id',<br> operator: '=', <br> value: '', <br> state: 'filterableoff', <br> inactive: true,<br> name: 'CaseId',<br> enclosevalueinquotes: true<br> }<br>];<br>patientCase.initialize().register();<br><b>//Create model on Interaction for updating Pregnancy Intention</b><br><b>var interactionUpdate = new skuid.model.Model();</b><br><b>interactionUpdate.objectName = 'Interaction__c';</b><br><b>interactionUpdate.id = 'Interaction_UpdateIntention';</b><br><b>interactionUpdate.recordsLimit = 50;</b><br><b>interactionUpdate.fields = d</b><br><b>{ id: 'Id'},</b><br><b> { id: 'x_Pregnancy_Intention_ITC__c' },</b><br><b> { id: 'Date__c' },</b><br><b> { id: 'Status__c'},</b><br><b> { id: 'Patient_Case__c'}</b><br><b>];</b><br><b>interactionUpdate.conditions = .</b><br><b> { </b><br><b> type: 'modelmerge',</b><br><b> field: 'Patient_Case__c',</b><br><b> operator: '=', </b><br><b> model: 'PatientCase_UpdateIntention',</b><br><b> mergeField: 'Id',</b><br><b> inactive: false,</b><br><b> enclosevalueinquotes: true,</b><br><b> noValueBehavior: 'noquery' </b><br><b> },</b><br><b> {</b><br><b> type: 'fieldvalue',</b><br><b> field: 'Status__c',</b><br><b> operator: '=',</b><br><b> value: 'Scheduled',</b><br><b> state: 'on',</b><br><b> inactive: false,</b><br><b> enclosevalueinquotes: true</b><br><b> },</b><br><b> {</b><br><b> type: 'modelmerge',</b><br><b> field: 'Date__c',</b><br><b> operator: 'gte',</b><br><b> model: 'PatientCase_UpdateIntention',</b><br><b> mergeField: 'Date_Most_Recent_Completed_Interaction__c',</b><br><b> inactive: false,</b><br><b> enclosevalueinquotes: false</b><br><b> }</b><br><b>];</b><br><b>interactionUpdate.initialize().register();</b><br><br>//Subscribe to the model save event.<br>skuid.events.subscribe('models.saved', function(saveResult){<br>// Only run if the initiator was a ui element (to eliminate loop)<br>if (saveResult.initiatorId) {<br>// Find all saved Interaction__c models<br>var savedInteractionModels = c];<br>$.each(saveResult.models, function(){<br>if ($.inArray(this, interactionModels) > -1) {<br>savedInteractionModels.push(this);<br>}<br>});<br>// If any interaction models were saved...<br>if (savedInteractionModels.length) {<br>var dfd = new $.Deferred();<br>// Iterate through models until one is found that has a Patient_Case__c field<br>for (var i = 0, updateComplete = false; i < savedInteractionModels.length && !updateComplete; i++) {<br>var rowsToUpdate = {},<br>caseRow = savedInteractionModelsei].getFirstRow(),<br>caseid = (caseRow) ? caseRow.Patient_Case__c : null;<br>if (caseid) {<br>// Find Intention and Date from Patient Case<br>updateComplete = true;<br>patientCase.setCondition(patientCase.getConditionByName('CaseId'), caseid);<br>$.when(skuid.model.updateData(epatientCase, interactionUpdate])).then(function(){<br>var lastPregnancyIntention = patientCase.getFieldValue(patientCase.getFirstRow(), 'Pregnancy_Intention__c');<br>// Update pregnancy intention for all scheduled interactions after the most recent completed<br><b>$.each(interactionUpdate.getRows(), function(i, row){</b><br><b>if (row.x_Pregnancy_Intention_ITC__c !== lastPregnancyIntention) {</b><br><b>rowsToUpdatetrow.Id] = {x_Pregnancy_Intention_ITC__c : lastPregnancyIntention};</b><br><b>}</b><br><b>});</b><br><b>interactionUpdate.updateRows(rowsToUpdate);</b><br><b>// Save updates to server</b><br><b>$.when(interactionUpdate.save())</b><br><b>.done(function(){</b><br><b>dfd.resolve();</b><br><b>})</b><br><b>.fail(function(){</b><br><b>dfd.reject();</b><br><b>console.log('Interaction_UpdateIntention model save failed.');</b><br><b>});</b><br>});<br>} <br>}<br>return dfd.promise();<br>}<br>}<br>});<br>}<br>});<br>})(skuid);
The code runs smoothly. interactionUpdate.save() works, and gets to .done() to resolve the deferred. When I inspect the model in the console, the values are correct, and hasChanged = false. But the server hasn’t changed.
It seems like I must be doing something wrong in the model definition?