LaceySnr.com - Salesforce Development Posts by Matt Lacey

RecordTypes in Test Methods... I don't get this one

Posted: 2011-04-14

Disclaimer: This particular issue may easily have been caused by validation rules, workflow or something that I just didn't see - I don't have time for a full investigation right now and I'm posting this just in case it helps someone somewhere from beating their head against a wall as I've been doing!

I've been trying to hammer out some test coverage as quickly as possible for a small piece of work and just lost an hour due to some weird issue around record types, namely trying to insert a contact record for testing with a specific record type. I was selecting the record type from the database, then setting it on the contact as follows:

sContact.RecordType = sRT;
update sContact;

Seems simple enough, didn't produce any warnings or errors (at least ones that were visible in an hour of trying to get this working) but the contact still had the wrong record type when I subsequently queried the database. The fix? This:

sContact.RecordTypeId = sRT.Id;
update sContact;

At this point I genuinely don't understand why this works if the former example does not produce an error; When I get the chance I shall endeavour to find out.