Skip to main content

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> &nbsp; &nbsp;{ id: 'Pregnancy_Intention__c' },<br> &nbsp; &nbsp;{ id: 'Date_Most_Recent_Completed_Interaction__c' }<br>];<br>patientCase.conditions = .<br> &nbsp; &nbsp;{&nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;type: 'fieldvalue',<br> &nbsp; &nbsp; &nbsp; &nbsp;field: 'Id',<br> &nbsp; &nbsp; &nbsp; &nbsp;operator: '=',&nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;value: '',&nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;state: 'filterableoff',&nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;inactive: true,<br> &nbsp; &nbsp; &nbsp; &nbsp;name: 'CaseId',<br> &nbsp; &nbsp; &nbsp; &nbsp;enclosevalueinquotes: true<br> &nbsp; &nbsp;}<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> &nbsp; &nbsp;{ id: 'x_Pregnancy_Intention_ITC__c' },</b><br><b> &nbsp; &nbsp;{ id: 'Date__c' },</b><br><b> &nbsp; &nbsp;{ id: 'Status__c'},</b><br><b> &nbsp; &nbsp;{ id: 'Patient_Case__c'}</b><br><b>];</b><br><b>interactionUpdate.conditions = .</b><br><b> &nbsp; &nbsp;{&nbsp;</b><br><b> &nbsp; &nbsp; &nbsp; &nbsp;type: 'modelmerge',</b><br><b> &nbsp; &nbsp; &nbsp; &nbsp;field: 'Patient_Case__c',</b><br><b> &nbsp; &nbsp; &nbsp; &nbsp;operator: '=',&nbsp;</b><br><b> &nbsp; &nbsp; &nbsp; &nbsp;model: 'PatientCase_UpdateIntention',</b><br><b> &nbsp; &nbsp; &nbsp; &nbsp;mergeField: 'Id',</b><br><b> &nbsp; &nbsp; &nbsp; &nbsp;inactive: false,</b><br><b> &nbsp; &nbsp; &nbsp; &nbsp;enclosevalueinquotes: true,</b><br><b> &nbsp; &nbsp; &nbsp; &nbsp;noValueBehavior: 'noquery'&nbsp;</b><br><b> &nbsp; &nbsp;},</b><br><b> &nbsp; &nbsp;{</b><br><b> &nbsp; &nbsp; type: 'fieldvalue',</b><br><b> &nbsp; &nbsp; field: 'Status__c',</b><br><b> &nbsp; &nbsp; operator: '=',</b><br><b> &nbsp; &nbsp; value: 'Scheduled',</b><br><b> &nbsp; &nbsp; state: 'on',</b><br><b> &nbsp; &nbsp; inactive: false,</b><br><b> &nbsp; &nbsp; enclosevalueinquotes: true</b><br><b> &nbsp; &nbsp;},</b><br><b> &nbsp; &nbsp;{</b><br><b> &nbsp; &nbsp; type: 'modelmerge',</b><br><b> &nbsp; &nbsp; field: 'Date__c',</b><br><b> &nbsp; &nbsp; operator: 'gte',</b><br><b> &nbsp; &nbsp; model: 'PatientCase_UpdateIntention',</b><br><b> &nbsp; &nbsp; mergeField: 'Date_Most_Recent_Completed_Interaction__c',</b><br><b> &nbsp; &nbsp; inactive: false,</b><br><b> &nbsp; &nbsp; enclosevalueinquotes: false</b><br><b> &nbsp; &nbsp;}</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) &gt; -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 &lt; savedInteractionModels.length &amp;&amp; !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?

^bump^


Hey Friends…

I really need an answer for this before Friday, if at all possible. Really appreciate any help. Thanks!


^^


Hey Matt - I took a quick pass at the code.  Can you try the following to help isolate:

1) Open up browser and set a breakpoint on $.when(interactionUpdate.save()) and inside the done and inside the fail
2) Make the situation occur on your page
3) When the breakpoint is hit for the when, go to the network tab and clear the list
4) “run” so that the code continues and it should hit the done per your comments above
5) check the network list and see if a network call was made to your SFDC server

If it was, verify the HTTP response was 200 (it should be but just to be sure).  Assuming yes, then this is likely something either in your model definition or a problem on the server side with skuid.  I’m thinking its more likely the former than the latter but this will help by checking to see if the call is even made to the server.

Let me know how it goes…


Hey Matt -


I believe I’ve found the source of your issue and along the way found another issue that you might be running in to.


First, let’s start with the root cause of why the server isn’t updating. In your code, you are not calling skuid.model.load. The result of this is that skuid doesn’t have the metadata necessary to appropriately handle the updateRows call. If you run the test I suggested above, I’m fairly certain that you will see that no network call is made after calling “save”. By adding the following line of code after you register the interactionUpdate model, you should see your data update on the server:


skuid.model.load(ipatientCase, interactionUpdate])


In troubleshooting your issue, I ran in to a problem with model conditions, see enclosevalueinquote condition parameter remoting exception (Banzai 7.12) for more details.


Per the above, if you are running Skuid 7.x, you will want to change your condition attribute to encloseValueInQuotes


Since I didn’t have your custom objects, in order to troubleshoot this, I created a similar page using Accounts and Contacts. I’ve included that page below so you can take a look. Please forgive the refactoring of your original code, as I wrote the test page, it was easier for me to break up things as I figured out what you were attempting to do. Hopefully what I came up with is close 🙂


<skuidpage unsavedchangeswarning="yes" personalizationmode="server" showsidebar="true" showheader="true" tabtooverride="Account"> <models> <model id="Contact" limit="20" query="true" createrowifnonefound="false" adapter="" type="" sobject="Contact" doclone=""> <fields> <field id="FirstName"/> <field id="Name"/> <field id="LastName"/> <field id="Description"/> <field id="AccountId"/> <field id="Account&#46;Name"/> </fields> <conditions> <condition type="param" value="id" field="Id" operator="=" enclosevalueinquotes="true" novaluebehavior=""/> </conditions> <actions/> </model> </models> <components> <pagetitle uniqueid="sk-3u-sd-81" model="Contact"> <maintitle> <template>{{Name}}</template> </maintitle> <subtitle> <template>{{Model&#46;label}}</template> </subtitle> <actions> <action type="multi" label="Save" icon="sk-icon-save"> <actions> <action type="save" rollbackonanyerror="true"> <models> <model>Contact</model> </models> </action> </actions> </action> <action type="multi" label="Cancel" icon="sk-icon-cancel"> <actions> <action type="cancel"> <models> <model>Contact</model> </models> </action> </actions> </action> </actions> </pagetitle> <basicfieldeditor showheader="true" showsavecancel="false" showerrorsinline="true" model="Contact" buttonposition="" uniqueid="sk-3COBx-1173" mode="read"> <columns> <column width="100%"> <sections> <section title="Section A" collapsible="no"> <fields> <field id="Name" valuehalign="" type=""/> <field id="FirstName"/> <field id="LastName"/> <field id="AccountId"/> <field id="Description"/> </fields> </section> </sections> </column> </columns> <renderconditions logictype="and"/> </basicfieldeditor> </components> <resources> <labels/> <css/> <javascript> <jsitem location="inline" name="newInlineJS" cachelocation="false" url=""> (function(skuid){ var $ = skuid&#46;$ , counter = 0; &#47;&#47; creates an object map of the property specified by objectKeyPropName &#47;&#47; from each object in the array specified by items var createMapForArray = function(items, objectKeyPropName) { return items&#46;reduce(function(obj, k) { var key = koobjectKeyPropName]; objjkey] = k; return obj; }, {}); }; &#47;&#47; creates an object map of model id to model &#47;&#47; for each model that is for SObject Contact var getcontactModelMap = function (models) { var contactModels = models&#46;filter( function(model) { return model&#46;objectName === 'Contact'; }); return createMapForArray(contactModels, 'id'); }; var getRowsToUpdate = function(contactModel, fieldUpdates) { var rowsToUpdate = {}; &#47;&#47; Update description for all contacts to the website of the account plus the /s# $&#46;each(contactModel&#46;getRows(), function(i, row) { rowsToUpdate,row&#46;Id] = fieldUpdates; }); return rowsToUpdate; }; &#47;&#47; create a dynamic account model var createAccountModel = function() { var accountModel = new skuid&#46;model&#46;Model(); accountModel&#46;objectName = 'Account'; accountModel&#46;id = 'DynamicAccount'; accountModel&#46;recordsLimit = 1; accountModel&#46;fields = d { id: 'Id' } , { id: 'Website' } , { id: 'CreatedDate' } ]; accountModel&#46;conditions = a { type: 'fieldvalue' , field: 'Id' , operator: '=' , value: '' , state: 'filterableoff' , inactive: true , name: 'AccountId' , encloseValueInQuotes: true } ]; accountModel&#46;initialize()&#46;register(); return accountModel; }; var createContactModel = function() { &#47;&#47;Create model on Interaction for updating Pregnancy Intention var contactModel = new skuid&#46;model&#46;Model(); contactModel&#46;objectName = 'Contact'; contactModel&#46;id = 'DynamicContact'; contactModel&#46;recordsLimit = 50; contactModel&#46;fields = d { id: 'Id'} , { id: 'Description' } , { id: 'CreatedDate' } , { id: 'LeadSource'} , { id: 'AccountId'} , { id: 'Name' } ]; contactModel&#46;conditions = a { type: 'modelmerge' , field: 'AccountId' , operator: '=' , model: 'DynamicAccount' , mergeField: 'Id' , inactive: false , encloseValueInQuotes: true , noValueBehavior: 'noquery' } &#47;&#47; NOTE - The following conditions are commented out to make it easier to find contacts &#47;&#47; but Matt's original code did have a condition for a picklist field &#47;&#47; , { &#47;&#47; type: 'fieldvalue' &#47;&#47; , field: 'LeadSource' &#47;&#47; , operator: '=' &#47;&#47; , value: 'Web' &#47;&#47; , state: 'on' &#47;&#47; , inactive: false &#47;&#47; , encloseValueInQuotes: true &#47;&#47; } , { type: 'modelmerge' , field: 'CreatedDate' , operator: 'gte' , model: 'DynamicAccount' , mergeField: 'CreatedDate' , inactive: false , encloseValueInQuotes: false } ]; contactModel&#46;initialize()&#46;register(); return contactModel; }; &#47;&#47; find the account id for the first contact &#47;&#47; that has an account id var findAccountId = function(models) { for (var i = 0; i &amp;lt; models&#46;length; i++) { var contactRow = modelsri]&#46;getFirstRow(); if (contactRow &amp;amp;&amp;amp; contactRow&#46;AccountId) { return contactRow&#46;AccountId; } } return null; }; &#47;&#47; get the list of models that are being saved &#47;&#47; that were identified in our list of Contact models var getContactModels = function(savedModels, contactModelMap) { &#47;&#47; Find all saved Contact models var savedContactModels = s]; $&#46;each(savedModels, function(idx, model) { if (contactModelMap&#46;hasOwnProperty(model&#46;id)) { savedContactModels&#46;push(this); } }); return savedContactModels; }; $(document&#46;body)&#46;one('pageload',function(){ &#47;&#47; get map of Contact models var contactModelMap = getcontactModelMap(skuid&#46;model&#46;list()); &#47;&#47; build array of Contact Models var contactModels = $&#46;map(contactModelMap, function(model, modelId) { return model; }); &#47;&#47; if there are contact models, create an account &amp;amp; contact models and subscribe to Skuid's save event if (contactModelMap &amp;amp;&amp;amp; !$&#46;isEmptyObject(contactModelMap)) { var accountModel = createAccountModel(); var contactModel = createContactModel(); &#47;&#47; make sure we load 'em up skuid&#46;model&#46;load(6accountModel, contactModel]); &#47;&#47;Subscribe to the model save event&#46; skuid&#46;events&#46;subscribe('models&#46;saved', function(saveResult){ &#47;&#47; Only run if the initiator was a ui element (to eliminate loop) if (saveResult&#46;initiatorId) { &#47;&#47; check the models that were saved and see if any where from our Contact model list var savedContactModels = getContactModels(saveResult&#46;models, contactModelMap); &#47;&#47; If any contact models were saved&#46;&#46;&#46; if (savedContactModels&#46;length) { var dfd = new $&#46;Deferred(); &#47;&#47; find the account id to use var accountId = findAccountId(savedContactModels); &#47;&#47; if we have an account id if (accountId) { &#47;&#47; set the account model condition accountModel&#46;setCondition(accountModel&#46;getConditionByName('AccountId'), accountId); &#47;&#47; update the account model and contact model $&#46;when(skuid&#46;model&#46;updateData(laccountModel, contactModel]))&#46;then(function() { &#47;&#47; get the first row of the account and then &#47;&#47; build the new website name var acctRow = accountModel&#46;getFirstRow() , website = (acctRow &amp;amp;&amp;amp; acctRow&#46;Website) || 'http:&#47;&#47;www&#46;test&#46;com' , newWebsite = (website + '/s' + (counter++)); &#47;&#47; get the rows to update var rowsToUpdate = getRowsToUpdate(contactModel, { Description: newWebsite }); &#47;&#47; update the rows in the contact model contactModel&#46;updateRows(rowsToUpdate); &#47;&#47; Save updates to server $&#46;when(contactModel&#46;save()) &#46;done(function() { console&#46;log('Successfully saved contact updates&#46;'); &#47;&#47; reload data for all contact models skuid&#46;model&#46;updateData(contactModels)&#46;then(function() { console&#46;log('updated all contact models'); dfd&#46;resolve(); }); }) &#46;fail(function() { dfd&#46;reject(); console&#46;log('Interaction_UpdateIntention model save failed&#46;'); }); }); } } return dfd&#46;promise(); } }); } }); })(skuid);</jsitem> </javascript> </resources> <styles> <styleitem type="background" bgtype="none"/> </styles> </skuidpage>

Agreed with Barry’s analysis - if you haven’t called load() on your Models, then they won’t have the Metadata required in order to know that your fields are editable/createable, and so when you call updateRow(s) or other similar methods, no changes will be written to the Model, because Skuid thinks that the fields are not editable/createable, so it won’t register these changes, thus when a save() call is made, no changes will be sent to the server because there are no changes in the Model! You can test this yourself by setting a JS breakpoint in your code right before calling save() and you’d see that there would not be changes for the fields in the Model.

Regarding the encloseValueInQuotes property on your dynamically-created Conditions, as I answered Barry in the other community post, you have always needed to camel-case this property’s name, if you use “enclosevalueinquotes” you’ll get problems, your Model’s query won’t run because you’ll get a server-side error. Always use encloseValueInQuotes(camel-cased) when setting this property via JavaScript.



Barry…

You are awesome.

Thanks.


IT WORKS! 🙂


Thanks for the extra background and info Zach!

Matt - To follow-on with Zach’s comments, what he mentions is how I figured out that load was required.  In short, using your original code I put a breakpoint on updateRows and then checked hasChanged and changes properties of the model and they were false and empty respectively even though the row data itself has been updated.  This led me down the path of the missing “load” call.  Sorry for not providing that extra background/detail in my previous post.


Awesome!!


Reply