1

All,

I have seen this problem documented numerous times but have yet to find a fix that will address my situation. I am trying to either update an existing case or create a new on based on SOQL look up query results. It works if the case is existing but doesn't work and I get the below error in Workbench:

System.ListException: List index out of bounds: 0

I understand what the error means - no value returned. I want to create a new case based on this result. How do I fix this problem?

Here is my Apex class (look for extFormId for the problem):

@RestResource(urlMapping='/Cases/*')
global with sharing class CreateCases {

    @HttpPut  
    global static ID upsertCase(String supplierId, String extUserId, String extFormId, String extFormType, String extFormName, String extUrl, String origin) {

        System.debug('upsertCase Call');

        list<RecordType> recordTypeName = new List<RecordType>([SELECT Id FROM RecordType WHERE SObjectType = 'Case' and Name = 'Connect']);
        if(recordTypeName.size() >0) {
            system.debug('recordTypeName : ' +RecordTypeName[0].Id);
        }

        list<Account> accountName = new List<Account>([select Id from account where organizer_id__c = :supplierID]);
        if(accountName.size() >0) {
            system.debug('accountName : ' +accountName[0].Id);
        }

        List<Contact> contactName = new List<Contact>([select Id from contact where organizer_user_id__c = :extUserId]); 
        if(contactName.size() >0) {
            system.debug('contactName : ' +contactName[0].id);
        }

        System.debug('###1st extFormId : ' +extFormId);
        list<Case> existingCase = new List<Case>([select Id from case where extform_id__c = :extFormId]);
        If (existingCase.size() >0) {
            system.debug('###2nd ExistingCase : ' +existingCase[0].id);    
        }

        Case thisCase = new Case(
        RecordTypeId                   = recordTypeName[0].Id,         
        AccountId                      = accountName[0].Id,                         
        ContactId                      = contactName[0].Id,
        extForm_ID__C                  = extFormId,    // Need to use this field to determine existing or new case
        Type                           = extFormType,
        Subject                        = extFormName,
        extURL__C                      = extUrl,
        extUser_ID__c                  = extUserId,
        Status                         = 'New', 
        Priority                       = 'Medium',
        Origin                         = origin,
        //Id                           = null);
        Id                             = existingCase[0].id);
        // Match case by Id, if present.
        // Otherwise, create new case.
        upsert thisCase;
        // Return the case ID.
        return thisCase.Id;
    }
}
  • You have a whole bunch of unguarded [0] references in your debug statements that could throw this error. You need to be checking all of those query results for .size() > 0 and supplying sensible defaults. The ternary operator ?: is your friend. – David Reed Jan 23 '18 at 23:33
  • As mentioned in above comment, if you did not found any account or contact from your query, you should have some default account and contact, whose id you can refer to. – sunny Jan 23 '18 at 23:35
  • I changed the debug statements to be inside of an if/then statement – AZ Bubba Joe Jan 23 '18 at 23:40
3

You're checking for query results with snippets like this:

list<RecordType> recordTypeName = new List<RecordType>([SELECT Id FROM RecordType WHERE SObjectType = 'Case' and Name = 'Connect']);
if(recordTypeName.size() >0) {
    system.debug('recordTypeName : ' +RecordTypeName[0].Id);
}

The if there guards the RecordTypeName[0].Id list access, stops it from executing and prevents errors like the one you're seeing. But, you don't stop the flow of execution!

After those if statements you then have this:

Case thisCase = new Case(
    RecordTypeId                   = recordTypeName[0].Id,         
    AccountId                      = accountName[0].Id,                         
    ContactId                      = contactName[0].Id,

All those [0]'s are not protected, and being executed, and hence throwing your exception.

Most likely you want some return statements inside the if blocks to terminate execution early, though the first could definitely be said to be a configuration error. Having that blow up lets you know as the admin/developer you need to create the record type or things are never going to work right, but still, better the code is clean and handles it gracefully, potentially returning the error to the caller of the service.

0

To document, I fixed the code. Here is an following example to handle the null out of bounds error:

            String AccountNameId = null;     
            list<Account> accountName = new List<Account>([select Id from account where organizer_id__c = :supplierID]);
            if(accountName.size() >0) {
                system.debug('accountName : ' +accountName[0].Id);
                accountNameId = accountName[0].Id;
                    } else {
                        accountNameId = null;
                    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.