Snippet to create child records (Tasks) and update parent record

I have a snippet that iterates through rows in a model (RequestedMedRecs) and creates a task for each row that meets certain conditions.  The snippet runs every time my model is saved.  The task creation part of the snippet is working well.

After each task is created, I want to update the row (Request_Submitted__c = true) so that the row no longer meets the criteria for task creation.  I think I need something along the lines of this, but can’t figure out where to put it within my snippet.  The update on the row needs to happen after all the tasks have been created.  

    medRecModel.updateRow(row, {           
        ‘Request_Submitted__c’ : ‘True’,
    });


Here’s my full snippet:

var params = arguments[0],
$ = skuid.$;


var MedRecModel = skuid.model.getModel(‘RequestedMedRecs’); 
var TaskModel = skuid.model.getModel(‘MedRecTasks’);

$.each(MedRecModel.getRows(),function(i,row){
    if (row.Status__c == ‘Requested’ && row.Request_Submitted__c === false) {

    var NewTask = TaskModel.createRow({ 
            additionalConditions: [
                { field: ‘Status’, value: ‘Completed’ },
                { field: ‘Subject’, value: ‘Record Requested’},
                { field: ‘ActivityDate’, value: ‘TODAY’ },
                { field: ‘Date__c’, value: ‘NOW’ },
                { field: ‘Type’, value: ‘Call’ },
                { field: ‘Patient_Case_Id__c’, value: row.Case__c },
                { field: ‘WhatId’, value: row.Case__c },
                { field: ‘Description’, value: ‘Record Requested’},

            ]
    });

    }});
    
TaskModel.save();

This should work:

var params = arguments[0], $ = skuid.$;<br>var MedRecModel = skuid.model.getModel('RequestedMedRecs');&nbsp;<br>var TaskModel = skuid.model.getModel('MedRecTasks');<br>$.each(MedRecModel.getRows(),function(i,row){<br>&nbsp; &nbsp; if (row.Status__c == 'Requested' &amp;&amp; row.Request_Submitted__c === false) {<br>&nbsp; &nbsp; &nbsp; &nbsp; var NewTask = TaskModel.createRow({&nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; additionalConditions: [<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { field: 'Status', value: 'Completed' },<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { field: 'Subject', value: 'Record Requested'},<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { field: 'ActivityDate', value: 'TODAY' },<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { field: 'Date__c', value: 'NOW' },<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { field: 'Type', value: 'Call' },<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { field: 'Patient_Case_Id__c', value: row.Case__c },<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { field: 'WhatId', value: row.Case__c },<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { field: 'Description', value: 'Record Requested'},<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]<br>&nbsp; &nbsp; &nbsp; &nbsp; });<br>&nbsp; &nbsp; &nbsp; &nbsp; MedRecModel.updateRow(row, { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Request_Submitted__c : true,<br>&nbsp; &nbsp; &nbsp; &nbsp; });<br>&nbsp; &nbsp; }<br>});<br>&nbsp; &nbsp;&nbsp;<br>skuid.model.save([TaskModel, MedRecModel]);



I think Moshe’s right, just replace this section:

MedRecModel.updateRow(row, {
   Request_Submitted__c : ‘True’,
});

with this:

MedRecModel.updateRow(row, {
   Request_Submitted__c : true,
});

Thank you both - this is working well now. 

Except…

I am getting some weird, jumpy behavior in the UI…it almost seems like this action is running on “row in model updated”, not on “row saved.”  (I’ll add new records to my table and they’ll save before I hit the save button.)  This seems to be working better when I remove the snippet.  I’m going to go through all of my model actions to see if I can find any other likely culprits, but if you have any thoughts or have seen this before…I’d be interested!

What were the conditions on your model: RequestedMedRecs? I am having the same issue and creating a similar snippet but I am having trouble limiting my parent model to only show records where a child record in my chile model looks up to the parent. It is either returning all parent records or none.

Hi Grace,

I’m not quite following you.  Are you trying to create a child record for every record that meets particular conditions?  Are you having trouble getting records to show in your table, or trouble selecting a subset of records from that table?  (The conditions on my RequestedMedRecs model has nothing to do with whether or not child records exist.)  

One thing to check - make sure that any fields that you use as conditions in your snippet are selected in your model!


So what I am doing is I have a model called Receipts (Receipts__c). The model starts out empty and the user can add any number of Receipt Rows and link them to a Matter via a lookup field called Receipts__c.Matter__c. I am trying to create a model of all Matters where the Receipt__c.Matter__r.Id = the Matter__c.Id so I can perform an update only on the Matter records related to the Receipts I just inserted but it does not return anything based on my condition so I was wondering how you made it so you could update only the RequestedMedRecs related to the Tasks you inserted in your example. Thanks for your help.

Ah, I see.  I’m going about this a little bit differently - I’m working from the perspective of the MedRec model, and I create the task and update the requested MedRec in one action.  (If the user has selected the MedRec to create a task, I create the task and update the MedRec - so, there’s no need for me to create a model to find MedRecs where I inserted tasks.)

I think what you will need to do is requery your Matters model whenever the Receipts model is saved.  Your Matters model should have the condition “Matters where ID is in Matter__c field from the Receipts model”.  Make sure you use “is in” instead of “=”.  This should return rows in your Matters model, and then you can update rows in that model.