Tuesday, December 16, 2014

Get RecordType Id Programmatically

This is when you need to assigned the RecordType to an object in apex.  You don't want to hard code the ID because it's ugly and might break the code during deploy.

It works for standard objects (like Case here):
1:  Case c = new Case();  
2:  RecordType RecType = [Select Id,SobjectType,Name From RecordType Where SobjectType = 'Case' and Name= 'Quality Alerts (QA)'];  
3:  c.RecordTypeId = RecType.Id;  


And custom objects:
1:  Communication__c com = new Communication__c();  
2:  RecordType RecType = [Select Id,SobjectType,Name From RecordType Where SobjectType = 'Communication__c' and Name= 'Outreach'];  
3:  com .RecordTypeId = RecType.Id;  


No comments:

Post a Comment