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

  • 1
  • Question
  • Updated 3 years ago
  • Answered
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();
Photo of Elissa Bradley

Elissa Bradley

  • 1,672 Points 1k badge 2x thumb

Posted 4 years ago

  • 1
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
This should work:

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'},
            ]
        });
        MedRecModel.updateRow(row, {            
            Request_Submitted__c : true,
        });
    }
});
    
skuid.model.save([TaskModel, MedRecModel]);
Photo of Elissa Bradley

Elissa Bradley

  • 1,672 Points 1k badge 2x thumb
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!
Photo of Grace Klingensmith

Grace Klingensmith

  • 210 Points 100 badge 2x thumb
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.
Photo of Elissa Bradley

Elissa Bradley

  • 1,672 Points 1k badge 2x thumb
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!
Photo of Grace Klingensmith

Grace Klingensmith

  • 210 Points 100 badge 2x thumb
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.
Photo of Elissa Bradley

Elissa Bradley

  • 1,672 Points 1k badge 2x thumb
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.