+ Start a Discussion

Using RecordId in a unit test

Is it legal or not to hard code a RecordId in a unit test (i.e. instead of querying the RecordType by name at runtime).  To me is seems a bad practice since the RecordId may be different on Prduction then the Sandbox.

If I deploy the sandbox to production and later create a new sandbox from production, will the recordid of the same objects (by name) be the same on Production and the Sandboxes?

In practice I have seen hard coded Record IDs in Sandbox unit test that seems to be deployable to Production.

Best Answer chosen by Admin (Salesforce Developers) 
It's bad form.   I never hardcode an Id, and there's really no reason to.  Something like below is simple enough.

 Account a = new Account(
  RecordTypeId = [Select r.SobjectType, r.Name, r.Id From RecordType r 
    where r.SobjectType='Account' and
    r.Name = 'Account - Multinational Master'].id
 ); insert a;

 Hope it helps, Steve.