1

I've had this code working in my account for a year now. When I run a test in the Developer console, no errors are generated. But when I package it up for distribution I get an error: System.ListException: List index out of bounds: 0 Class.TTCreateCampaignMemberStatus.CampaignInsert: line 6, column 1

From what I've read it seems I need to account for the situation that the list returns zero values. Would love some advice on how to fix this. Here is the code:

@isTest
private class TTCreateCampaignMemberStatus
{
    static testMethod void CampaignInsert() {
        List<RecordType> rt = [select Id, name from RecordType where SobjectType = 'Campaign' and name = 'Events and Meetings'];
        Campaign testCampaign = new Campaign(Name='Test Test Campaign',RecordTypeId=rt[0].Id);
        insert testCampaign;
        Integer i =[Select count() FROM CampaignMemberStatus WHERE CampaignID=:testCampaign.id];
        System.assertEquals(5,i);
    }
}
  • Sounds like the RecordType "Events and Meetings" doesn't exist in the target org. A quick rt.size() check will tell you if you have matches. – Mike Chale Dec 30 '13 at 17:35
  • To add to Mike's comment, I would switch to using Developername instead of Name for checking the RecordTypes. It's more likely someone will override the Name of a Record Type, or worse yet, create a duplicate Record Type with the same label. – James Loghry Dec 30 '13 at 17:51
  • Also, does this test still use existing data? Seems to me like your System.assert at the bottom of the method will fail since you're (seemingly) not creating any Campaign Members – James Loghry Dec 30 '13 at 17:56
6

When you run this code in your original environment, it's finding the RecordType that you had manually created in that org. You'd get the same result both running the test and running the code in the console.

Packaging and deploying to a fresh organization without that RecordType fails if it hasn't been included in the package or manually created beforehand.

The immediate fix is to include the RecordType in your package, and then refine your test as Mike and James have suggested.

0

This is a Salesforce Bug.

Use this code in your test method in place of the query:

@isTest
private class TTCreateCampaignMemberStatus{
    static testMethod void CampaignInsert() {
        //List<RecordType> rt = [select Id, name from RecordType where SobjectType = 'Campaign' and name = 'Events and Meetings'];
        Schema.DescribeSObjectResult cfrSchema = Schema.SObjectType.Campaign; 
        Map<String,Schema.RecordTypeInfo> CampaignRecordTypeInfo = cfrSchema.getRecordTypeInfosByName(); 
        Id rtId = CampaignRecordTypeInfo.get('Events and Meetings').getRecordTypeId();

        //Campaign testCampaign = new Campaign(Name='Test Test Campaign',RecordTypeId=rt[0].Id);
        Campaign testCampaign = new Campaign(Name='Test Test Campaign',RecordTypeId=rtId);
        insert testCampaign;
        Integer i =[Select count() FROM CampaignMemberStatus WHERE CampaignID=:testCampaign.id];
        System.assertEquals(5,i);
    }
}

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.