VF remoting exception: list index out of bounds

  • 1
  • Problem
  • Updated 3 years ago
  • Solved
I'm getting a VF remoting exception: "list index out of bounds: 1"

It's happening when I attempt a save on the FollowupInteractions model after running the code at the end of this post.

Here's the json string that VF exception is returning. I'm guessing that the "1" under "changes" is the problem?

{  "operations": [
    {
      "id": "FollowupInteractions",
      "type": "Interaction__c",
      "updates": [
        {
          "id": "Date__c",
          "targetObjects": null
        },
        {
          "id": "Interaction_Category__c",
          "targetObjects": null
        },
        {
          "id": "Interaction_Purpose__c",
          "targetObjects": null
        },
        {
          "id": "Interaction_Type__c",
          "targetObjects": null
        },
        {
          "id": "Status__c",
          "targetObjects": null
        },
        {
          "id": "Patient_Case__c",
          "targetObjects": null
        },
        {
          "id": "Patient_Case__r.Name",
          "targetObjects": null
        },
        {
          "id": "Follow_up_Attempts__c",
          "targetObjects": null
        }
      ],
      "changes": {
        "1": {
          "Patient_Case__c": "a0wo0000002ul8fAAA",
          "Interaction_Category__c": "Follow-up",
          "Date__c": "2015-12-17T23:30:00.000Z",
          "Follow_up_Attempts__c": "4",
          "Status__c": "Scheduled",
          "Interaction_Purpose__c": "4 week follow-up",
          "Interaction_Type__c": "Call"
        },
        "2": {
          "Patient_Case__c": "a0wo0000002ul8fAAA",
          "Interaction_Category__c": "Follow-up",
          "Date__c": "2016-06-22",
          "Follow_up_Attempts__c": "1",
          "Status__c": "Scheduled",
          "Interaction_Purpose__c": "Chart closing",
          "Interaction_Type__c": "Other"
        },
        "a0ro0000000HypUAAS": {
          "Status__c": "Cancelled"
        },
        "a0ro0000000HypTAAS": {
          "Status__c": "Cancelled"
        },
        "a0ro0000000HypSAAS": {
          "Status__c": "Cancelled"
        }
      }
    }
  ]
}

Here's the code:
var $ = skuid.$;            
//Get models and rows
var mI = skuid.$M('ThisInteraction'),
    mC = skuid.$M('PatientCase'),
    mF = skuid.$M('FollowupInteractions');
var rI = mI.getFirstRow(),
    rC = mC.getFirstRow();

////////////////////////////////////////
//          Helper Functions          //
////////////////////////////////////////
var getClosingDate = function(){
    var edd = rC.Estimated_Due_Date_by_U_S__c || rC.Estimated_Due_Date_LMP__c;
    if (!edd) {
        edd = skuid.time.parseSFDate(rC.LMP__c) || new Date();
        edd.setDate(edd.getDate() + 40*7);
    } else {
        edd = skuid.time.parseSFDate(edd);
    }
    var closingDate = new Date();
    closingDate.setFullYear(edd.getFullYear(), edd.getMonth(), edd.getDate() + 4*7);
    return skuid.time.getSFDate(closingDate);
};
var makeFollowup = function(model, date, attempt, purpose, type){
    //Create row
    model.createRow({
        additionalConditions:[
            {field: 'Date__c', value: date},
            {field: 'Follow_up_Attempts__c', value: attempt},
            {field: 'Status__c', value: 'Scheduled'},
            {field: 'Interaction_Category__c', value: 'Follow-up'},
            {field: 'Interaction_Purpose__c', value: purpose},
            {field: 'Interaction_Type__c', value: type}
        ]
    });
};
var cancelScheduled = function(model){         
    //Cancel all future interactions
    var rowsToUpdate = {},
        now = new Date();
    
    //check followup interactions model for future scheduled follow-up interactions
    $.each(model.getRows(), function(){
        if (skuid.time.parseSFDateTime(this.Date__c) > now && this.Status__c == 'Scheduled') {
            rowsToUpdate[this.Id] = { Status__c: 'Cancelled' };
        }
    });
    model.updateRows( rowsToUpdate ); 
};
////////////////////////////////////////
//              Action                //
////////////////////////////////////////
if (rI.Interaction_Purpose__c === 'After due-date follow-up') {
    var dt = skuid.time.parseSFDateTime(rI.Date__c);
    dt.setDate(dt.getDate() + 1);
    var newDate = skuid.time.getSFDateTime(dt);
    makeFollowup(mF, newDate, '1', 'Chart closing', 'Other');
    
} else {
    //check follow-up status
    switch (rI.Status__c) {
        case 'Unsuccessful':
        case 'Left message':        //We did not make a connection. 
            var attempt = rI.Follow_up_Attempts__c;
            if (!attempt) {
                attempt = 1;
                mI.updateRow(rI, {Follow_up_Attempts__c: '1'});
            }
            var dt = skuid.time.parseSFDateTime(rI.Date__c);
            dt.setDate(dt.getDate() + 1);
            var newDate = skuid.time.getSFDateTime(dt);
            
            //If we haven't tried three times
            if (attempt < 3) {              //create new row for next attempt
                
                // Get vairables for new row
                attempt++;
                makeFollowup(mF, newDate, attempt.toString(), rI.Interaction_Purpose__c, rI.Interaction_Type__c);
                
            } else {
                cancelScheduled(mF);
                if (!rC.Pregnancy_Confirmed__c){
                    //Create final followup attempt with certified letter
                    makeFollowup(mF, newDate, '4', rI.Interaction_Purpose__c, 'Certified Letter');
                }
                //Create 'Awesome Christina' follow-up attempt 4 for patients who still intend to abort
                if (rC.Pregnancy_Intention__c == 'Abort') {
                    mF.createRow({
                        additionalConditions:[
                            {field: 'Date__c', value: newDate},
                            {field: 'Follow_up_Attempts__c', value: '4'},
                            {field: 'Status__c', value: 'Scheduled'},
                            {field: 'Interaction_Category__c', value: 'Follow-up'},
                            {field: 'Interaction_Purpose__c', value: rI.Interaction_Purpose__c},
                            {field: 'Interaction_Type__c', value: rI.Interaction_Type__c},
                            {field: 'Primary_Staff__c', value: '005o0000002PSJR'} //Christina
                        ]
                    });
                }
                var closingDate = getClosingDate();
                makeFollowup(mF, closingDate, '1', 'Chart closing', 'Other');
            }
            break;
        case 'Hung-up':             //If patient hung up, do not call again.
            mC.updateRow(rC, {Patient_Requests_No_Further_Contact__c: true});
            cancelScheduled(mF);
            
            var closingDate = getClosingDate();
            makeFollowup(mF, closingDate, '1', 'Chart closing', 'Other');
            break;
        case 'Successful':          //We made a connection
            if (rC.Patient_Requests_No_Further_Contact__c) {
                cancelScheduled(mF);
                var closingDate = getClosingDate();
                makeFollowup(mF, closingDate, '1', 'Chart closing', 'Other');
            } else if (rC.Pregnancy_Outcome__c && rC.Pregnancy_Outcome__c !== 'Still Pregnant') { // There was a closing event
                cancelScheduled(mF);
                
                var dt = new Date();
                dt.setDate(dt.getDate() + 1);
                var newDate = skuid.time.getSFDateTime(dt);
                makeFollowup(mF, newDate, '1', 'Chart closing', 'Other');
            }
            break;
    }
}

$.blockUI({
    message: 'Processing Complete.',
    timeout: 2000
});
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb

Posted 3 years ago

  • 1
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
Looks like you're trying to send a date into a datetime field. Notice the value you're sending for "changes" -> "2" -> "Date__c"
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
That was it! Thanks.