Mass Create snippet problems (modified from the tutorial)

  • 1
  • Problem
  • Updated 4 years ago
  • (Edited)
Skuid page: Calendar

Use Case: Mass Create rows on newAppts model based on screen input into a protoAppts model

Deviations from the tutorial:
1. While is used instead of an Each
2. Get row by Id (proto model has more than 1 record)

Biggest problem
1. I don't know JavaScript
2. Reference Skuid Unique Id for get row by Id
3. All created records need to have the value of the first record stored in a custom field
     - How does one save an Id for a record that doesn't exist (whoa - that's deep)
4. Set the Loop Var values - it gives me errors
5. JavaScript data time syntax
6. Setting value in additionalConditions


Here's my code attempt:



var params = arguments[0];var step = params.step;
var $ = skuid.$;
// REFERENCE MODELS
var models = skuid.model.map();
var protoAppt = models.Appointment;
var newAppts = models.newAppts;
// SET DEFAULT VALUES protoModel values ==> conditions on newModel
var proto = protoAppt.getFirstRow();  // can we identify the row in another way? Skuid Unique IDs
// GET conditions
//        based on proto
var caseCondition = newAppts.getConditionByName('Case');
var userCondition = newAppts.getConditionByName('User');
var recurrencetotalCondition = newAppts.getConditionByName('RecurrenceTotal');
var recurrencestarttimeCondition = newAppts.getConditionByName('RecurrenceStartTime');
var recurrenceendtimeCondition = newAppts.getConditionByName('RecurrenceEndTime');
var recurrenceapptidCondition = newAppts.getConditionByName('RecurrenceApptID');
var recurrenceisrecurrenceCondition = newAppts.getConditionByName('RecurrenceIsRecurrence');
var attendanceCondition = newAppts.getConditionByName('Attendance');
var confirmationCondition = newAppts.getConditionByName('Confirmation');
// SET conditions
newAppts.setCondition(caseCondition,protoAppt.getFieldValue(proto,'Case__c'));
newAppts.setCondition(userCondition,protoAppt.getFieldValue(proto,'User__c'));
newAppts.setCondition(recurrencetotalCondition,protoAppt.getFieldValue(proto,'RecurrenceNumberTotal__c'));
newAppts.setCondition(recurrencestarttimeCondition,protoAppt.getFieldValue(proto,'StartDateTime__c'));
newAppts.setCondition(recurrenceendtimeCondition,protoAppt.getFieldValue(proto,'EndDateTime__c'));
newAppts.setCondition(recurrenceisrecurrenceCondition,protoAppt.getFieldValue(proto,'RecurrenceIsRecurrence__c'));
newAppts.setCondition(attendanceCondition,protoAppt.getFieldValue(proto,'Attendance__c'));
newAppts.setCondition(confirmationCondition,protoAppt.getFieldValue(proto,'Confirmation__c'));
//        How do I set an Id for a record that hasn't be created in SF yet?
newAppts.setCondition(recurrenceapptidCondition,protoAppt.getFieldValue(proto,'Id'));
// SET loop vars
var loopRecurrenceNumber = 1;
var loopRecurrenceNumberTotal = protoAppt.getFieldValue(proto,'RecurrenceNumberTotal__c'));
var loopStartDateTime = protoAppt.getFieldValue(proto,'StartDateTime__c'));
var loopEndDateTime = protoAppt.getFieldValue(proto,'EndDateTime__c'));
// ACTION CODE - loop and set non default values
while(loopRecurrenceNumber <= loopRecurrenceNumberTotal){
    var row = newAppt.createRow({
        additionalConditions: [
//             SET StartDateTime__c = loopStartDateTime, EndDateTime__c = loopEndDateTime, RecurrenceNumber__c = loopRecurrenceNumber
            {field: 'StartDateTime__c', value: this.Id, operator: '=', nameFieldValue: this.name}
            {field: 'EndDateTime__c', value: this.Id, operator: '=', nameFieldValue: this.name}
            {field: 'RecurrenceNumber__c', value: this.Id, operator: '=', nameFieldValue: this.name}
            ]
    });
    // loopStartDate + 7 days, loopEndDate + 7 days, loopRecurrenceNumber + 1
    loopStartDateTime.setDate(loopStartDateTime.getDate() + 7);
    loopEndDateTime.setDate(loopEndDateTime.getDate() + 7);
    loopRecurrenceNumber.setNumber(loopRecurrenceNumber.getNumber() + 1);
}];
// FINISH CODE
step.navigate('step2');

Photo of Griffin Brown

Griffin Brown

  • 2,766 Points 2k badge 2x thumb
  • hopeful

Posted 4 years ago

  • 1
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
OK here we go...
1) line 38 var row = newAppt.createRow() should be newAppts.createRow()
2) this.Id and this.Name are being used to populate your datetime fields which doesn't make any sense. Firstly you don't need the "operator" or "nameFieldValue" parts. Those are only needed for a lookup field. You want to get the values from the proto model, and assign them to your new rows.
Try this out:

var params = arguments[0];var step = params.step;var $ = skuid.$;
// REFERENCE MODELS
var models = skuid.model.map();
var protoAppt = models.Appointment;
var newAppts = models.newAppts;
// SET DEFAULT VALUES protoModel values ==> conditions on newModel
var proto = protoAppt.getFirstRow();  // can we identify the row in another way? Skuid Unique IDs
// GET conditions
//        based on proto
var caseCondition = newAppts.getConditionByName('Case');
var userCondition = newAppts.getConditionByName('User');
var recurrencetotalCondition = newAppts.getConditionByName('RecurrenceTotal');
var recurrencestarttimeCondition = newAppts.getConditionByName('RecurrenceStartTime');
var recurrenceendtimeCondition = newAppts.getConditionByName('RecurrenceEndTime');
var recurrenceapptidCondition = newAppts.getConditionByName('RecurrenceApptID');
var recurrenceisrecurrenceCondition = newAppts.getConditionByName('RecurrenceIsRecurrence');
var attendanceCondition = newAppts.getConditionByName('Attendance');
var confirmationCondition = newAppts.getConditionByName('Confirmation');
// SET conditions
newAppts.setCondition(caseCondition,protoAppt.getFieldValue(proto,'Case__c'));
newAppts.setCondition(userCondition,protoAppt.getFieldValue(proto,'User__c'));
newAppts.setCondition(recurrencetotalCondition,protoAppt.getFieldValue(proto,'RecurrenceNumberTotal__c'));
newAppts.setCondition(recurrencestarttimeCondition,protoAppt.getFieldValue(proto,'StartDateTime__c'));
newAppts.setCondition(recurrenceendtimeCondition,protoAppt.getFieldValue(proto,'EndDateTime__c'));
newAppts.setCondition(recurrenceisrecurrenceCondition,protoAppt.getFieldValue(proto,'RecurrenceIsRecurrence__c'));
newAppts.setCondition(attendanceCondition,protoAppt.getFieldValue(proto,'Attendance__c'));
newAppts.setCondition(confirmationCondition,protoAppt.getFieldValue(proto,'Confirmation__c'));
//        How do I set an Id for a record that hasn't be created in SF yet?
newAppts.setCondition(recurrenceapptidCondition,protoAppt.getFieldValue(proto,'Id'));
// SET loop vars
var loopRecurrenceNumber = 1;
var loopRecurrenceNumberTotal = protoAppt.getFieldValue(proto,'RecurrenceNumberTotal__c'));
var loopStartDateTime = protoAppt.getFieldValue(proto,'StartDateTime__c'));
var loopEndDateTime = protoAppt.getFieldValue(proto,'EndDateTime__c'));
// ACTION CODE - loop and set non default values
while(loopRecurrenceNumber <= loopRecurrenceNumberTotal){
    var row = newAppts.createRow({
        additionalConditions: [
//          SET StartDateTime__c = loopStartDateTime, EndDateTime__c = loopEndDateTime, RecurrenceNumber__c = loopRecurrenceNumber
// You definitely do not want to use this.Id and this.Name 
            {field: 'StartDateTime__c', value: loopStartDateTime}
            {field: 'EndDateTime__c', value: loopEndDateTime}
            {field: 'RecurrenceNumber__c', value: loopRecurrenceNumber}
            ]
    });
    // loopStartDate + 7 days, loopEndDate + 7 days, loopRecurrenceNumber + 1
    loopStartDateTime.setDate(loopStartDateTime.getDate() + 7);
    loopEndDateTime.setDate(loopEndDateTime.getDate() + 7);
    loopRecurrenceNumber.setNumber(loopRecurrenceNumber.getNumber() + 1);
};
// FINISH CODE
step.navigate('step2');
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
I also see that you have a whole bunch of conditions. Are you sure that you really need that many? Can you explain why you need the Id that doesn't exist yet? I think we can get through this one step at a time. Response to your problems listed above:

1. Not a big deal (a lot of us don't we just look like we do :))
2. I don't get this one
3. Not a big deal we can assign that to a variable during the save and update the rows
4. Post the errors that your getting
5. Check out the skuid tutorial on the skuid.time class here: http://help.skuidify.com/m/11720/l/129505-skuid-time
6. That's easy 

See no big deal!
Photo of Griffin Brown

Griffin Brown

  • 2,766 Points 2k badge 2x thumb
I probably don't need that many conditions, but I'm just following a recipe for now since I don't understand the ingredients I'm working with. :-)

2.  (see below)
3. This solves my need for an ID that hasn't be created - though I'm not sure how to execute this
4. Resolved
5. Of course - you even told me about this a couple days ago
6. Resolved


I probably don't understand how this works, but my proto model has more than 1 record (potentially hundreds). How can I ensure that the data is getting pulled from the correct row?
Photo of Moshe Karmel

Moshe Karmel, Champion

  • 8,646 Points 5k badge 2x thumb
OK you have to go to the advanced tab on the proto model and uncheck "load model data on page load" and check "create a row in model if there is none" this will give you a data entry model with one row. You really don't want a million conditions just a data entry model which hands off the variable (start and end) to the JS snippet which creates a whole bunch of rows.