• AZ Bubba Joe
  • NEWBIE
  • 10 Points
  • Member since 2015
  • Salesforce Consultant

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
all - I need help with finishing my test method for this apex trigger. I am currently only getting 33% test coverage. What am I missing? Any help or guidance would be appreciated. 

First my trigger:
 
trigger CaseAutoResponseLoopKiller on Case (before insert) {
//create a set of the subjects
        Set<String> subjects = new Set<String>();
        for (Case c : Trigger.new){
        
            User owner = [Select Id From User Where Id =: c.OwnerId];
            
            if (!String.isBlank(c.Subject) && owner == null) {
                subjects.add(c.Subject);            
            }
        }
        
        if (!subjects.IsEmpty()) {
            
        
            //query for cases created within 5 minutes with these subjects
            Datetime fiveMinsAgo = system.now().addMinutes(-5);
            AggregateResult[] subjectCount = [SELECT Count(id) SubCount, Subject, SuppliedEmail FROM Case where Subject in :subjects AND CreatedDate > :fiveMinsAgo GROUP BY Subject, SuppliedEmail ];
            
            //add them to a map of subject and supplied email if there have been more than 3 created with 5 minutes
            Map<String,String> loopList = new Map<String,String>();
            for (AggregateResult ar : subjectCount) {
                if (integer.valueof(ar.get('SubCount')) > 3) {
                    loopList.put(string.valueof(ar.get('Subject')), string.valueof(ar.get('SuppliedEmail')));
                }
            }
            
            //loop through the trigger and add an error if the supplied email matches the mapped value
            for (Case c : Trigger.new){
                if (c.SuppliedEmail == loopList.get(c.Subject)) {
                    c.addError('Nope');
                }
            }
        
        }
}

now for the Test Class code:
 
@IsTest
private class CaseAutoResponseLoopKillerTest {
static testMethod void CaseAutoResponseLoopKiller() {
        Integer initCaseSize = 4;
        List<Case> cl = new List<Case>();
        for (Integer i = 0; i < initCaseSize; i++) {
            Case c = new Case();
            c.Subject = 'Infinite Loop Test';
            c.SuppliedEmail = 'test@loop.com';
            cl.add(c);
        }
        
        insert cl;
        
        system.assertEquals(initCaseSize, [SELECT Id FROM Case].size());
    
        try {
            Case c = new Case();
            c.Subject = 'Infinite Loop Test';
            c.SuppliedEmail = 'test@loop.com';
            insert c;
    
        } 
        catch(Exception e) {
            system.assert(e.getMessage().contains('Nope'));
        }  
		}
    }

Code Coverage Screen Shot
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 receive the following 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 ConnectCases {

     @HttpPut  
    global static ID upsertCase(String supplierId, String connectUserId, String connectFormId, String connectFormType, String connectFormName, String connectUrl, String origin) {
        
        System.debug('upsertCase Call');
        
             list<RecordType> RecordTypeName = new List<RecordType>([SELECT Id FROM RecordType WHERE SObjectType = 'Case' and Name = 'ThirdPartyApp']);
            system.debug('recordTypeName : ' +RecordTypeName[0].Id);
        
            list<Account> accountName = new List<Account>([select Id from account where extid__c = :supplierID]);
            system.debug('accountName : ' +accountName[0].Id);
        
            List<Contact> contactName = new List<Contact>([select Id from contact where extuser_id__c = :extUserId]); 
            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.isempty() ) {
                    existingCase[0].Id = null;  
                      }    else {
                          existingCase[0].Id = null;
                      }*/
            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;
    }
}

 
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 receive the following 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 ConnectCases {

     @HttpPut  
    global static ID upsertCase(String supplierId, String connectUserId, String connectFormId, String connectFormType, String connectFormName, String connectUrl, String origin) {
        
        System.debug('upsertCase Call');
        
             list<RecordType> RecordTypeName = new List<RecordType>([SELECT Id FROM RecordType WHERE SObjectType = 'Case' and Name = 'ThirdPartyApp']);
            system.debug('recordTypeName : ' +RecordTypeName[0].Id);
        
            list<Account> accountName = new List<Account>([select Id from account where extid__c = :supplierID]);
            system.debug('accountName : ' +accountName[0].Id);
        
            List<Contact> contactName = new List<Contact>([select Id from contact where extuser_id__c = :extUserId]); 
            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.isempty() ) {
                    existingCase[0].Id = null;  
                      }    else {
                          existingCase[0].Id = null;
                      }*/
            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;
    }
}

 
If I try to create Vivian Torres, as an Account with Record Type, Individual, I get an error message:

"FinServ.AccountTrigger: execution of BeforeInsert caused by: FinServ.MoiExceptionWrapper.ValidationException: The Individual record type isn't available. Contact your administrator for help. (FinServ)"