+ Start a Discussion
larrmill59larrmill59 

Trouble getting a test class to work

I'm working on a trigger that simply populates a date/time value into a field on the account when a record of a specific record type gets created on a related custom object. The trigger is fine but the test class fails due to the line where I try to specify the record type in the test. The error I get is below.

 


System.StringException: Invalid id: DO NominationClass.AccUpdateTest.SponsorshipInsert: line 11, column 20 External entry point

 

My trigger code:

trigger AccUpdate on AE_Sponsorship__c (after insert, after update) {
//Query for the Account record types
     List<RecordType> rtypes = [Select Name, Id From RecordType 
                  where sObjectType='AE_Sponsorship__c' limit 1];
     
     //Create a map between the Record Type Name and Id for easy retrieval
     Map<String,String> accountRecordTypes = new Map<String,String>{};
     for(RecordType rt: rtypes)
        accountRecordTypes.put(rt.Name,rt.Id);

    for (AE_Sponsorship__c a: trigger.new){
            if(a.RecordTypeId==accountRecordTypes.get('DO Nomination'))
            {
            Account myAcc = [select Id from Account where ID = : a.Account__c];
        myAcc.Test_Update_from_AE_sponsorship__c = a.lastmodifiedDate;
        update myAcc;
        }
    }
}

My test class:

@isTest
private class AccUpdateTest {
    private static testmethod void SponsorshipInsert(){
    AE_Sponsorship__c sponsor1 = new AE_Sponsorship__c(
    Overall_Monthly_Volume__c=100, 
    Met_with__c='jim', 
    Expected_Flagstar_Volume__c=100, 
    Approved_for_CFL__c=TRUE, 
    Account__c = '0015000000Jo7v5',
    Date_of_Visit__c=Date.newInstance(2010, 07, 15),
    RecordTypeID = 'DO Nomination'
);
insert sponsor1;
}
}

 Can anyone help me get this final piece working please? I need to specify the record type without hard coding the record type ID, which is why the trigger has a map for record type.

 

Thank you,

Larry

 

Best Answer chosen by Admin (Salesforce Developers) 
JA-DevJA-Dev

You're going to either run the same query again and store the values into a map or I'd put this portion of your code into a separate class (say class A):

 

public Map<String, Id> recTypeMap()
{
    List<RecordType> rtypes = [Select Name, Id From RecordType 
                               where sObjectType='AE_Sponsorship__c' limit 1];
     
    //Create a map between the Record Type Name and Id for easy retrieval
    Map<String,Id> accountRecordTypes = new Map<String,Id>{};
    for(RecordType rt: rtypes)
        accountRecordTypes.put(rt.Name,rt.Id);
    return accountRecordTypes;
}

And then I'd just reference recTypeMap() from class A in your test class:

 

@isTest
private class AccUpdateTest {
    private static testmethod void SponsorshipInsert(){
    AE_Sponsorship__c sponsor1 = new AE_Sponsorship__c(
    Overall_Monthly_Volume__c=100, 
    Met_with__c='jim', 
    Expected_Flagstar_Volume__c=100, 
    Approved_for_CFL__c=TRUE, 
    Account__c = '0015000000Jo7v5',
    Date_of_Visit__c=Date.newInstance(2010, 07, 15),
    A myClassA = new A();
    RecordTypeID = myClassA.recTypeMap().get('DO Nomination');
insert sponsor1;
}
}

 

 

 

 

All Answers

forcedotcomforcedotcom

In your test method you are trying to set the AE_Sponsorship__c recordtypeid value as 'DO Sponsorship' - this field requires a valid record type Id, not the name of the record type.

 

Wrong = RecordTypeID = 'DO Nomination' 
Correct = RecordTypeId = '012000000000000'

 

JA-DevJA-Dev

You're going to either run the same query again and store the values into a map or I'd put this portion of your code into a separate class (say class A):

 

public Map<String, Id> recTypeMap()
{
    List<RecordType> rtypes = [Select Name, Id From RecordType 
                               where sObjectType='AE_Sponsorship__c' limit 1];
     
    //Create a map between the Record Type Name and Id for easy retrieval
    Map<String,Id> accountRecordTypes = new Map<String,Id>{};
    for(RecordType rt: rtypes)
        accountRecordTypes.put(rt.Name,rt.Id);
    return accountRecordTypes;
}

And then I'd just reference recTypeMap() from class A in your test class:

 

@isTest
private class AccUpdateTest {
    private static testmethod void SponsorshipInsert(){
    AE_Sponsorship__c sponsor1 = new AE_Sponsorship__c(
    Overall_Monthly_Volume__c=100, 
    Met_with__c='jim', 
    Expected_Flagstar_Volume__c=100, 
    Approved_for_CFL__c=TRUE, 
    Account__c = '0015000000Jo7v5',
    Date_of_Visit__c=Date.newInstance(2010, 07, 15),
    A myClassA = new A();
    RecordTypeID = myClassA.recTypeMap().get('DO Nomination');
insert sponsor1;
}
}

 

 

 

 

This was selected as the best answer
larrmill59larrmill59

JA:

 

Thanks for the assistance. Your solution to the issue is correct. We had to re-do the query, although we simply did it within the test class itself rather than creating a new class. We also opted to do a Select to get the record type rather than creating a Map, since there was only one record type we were interested in for this trigger.

 

Thanks again,

Larry