+ Start a Discussion

RecordTypeId list/map for a custom object class

EE.  I'm looking for a way to obtain an active list of RecordTypeId's along with the name(description) so I can source out the ID for use in a vfpage, to lock in the record type...


I have a custom object, that I have four forms on a "Sites", each form will have a hidden (rendered="false") that field will contain the Record Type,  I only have 4 recordTypes, and each form will hardcode the appropriate recordtype.


I found the following code from a "Best Practices" site http://wiki.developerforce.com/page/Apex_Code_Best_Practices but it doesn't work.  No Big Surprise!


Here is a cNp from a best practice doc, Does anyone know how I can accomplish what I need...

//Query for the Account record types
     List<RecordType> rtypes = [Select Name, Id From RecordType 
                  where sObjectType='CustomObj__c' and isActive=true];
     //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)
      for(Account a: Trigger.new){
     	  //Use the Map collection to dynamically retrieve the Record Type Id
     	  //Avoid hardcoding Ids in the Apex code
     	  	 //do some logic here.....
     	  }else if(a.RecordTypeId==accountRecordTypes.get('High Tech')){
     	  	 //do some logic here for a different record type...

 Line 7

   for(RecordType rt : rtypes)  Errors out with the following error "Expecting Right Curly Bracket found 'For' instead.


Why is SF publishing incomplete documentation that they know doesn't work?


FYI, I have already produced a working controller that has static values for my recordtypes that works in my sandbox, but I can't deploy it because apparently I have to now figure out how to write test for it... Yeah!... Actually I agree with the concept of validating the integrity, it would just be nice if their was a more convenient (better documentation) to do it... Never the less, before I go through the headache of writing test methods, I felt it would be in my best interest to go ahead and follow the Best Practice, of not hard coding ID's as they may not persist in other instances, then write test methods for that instead...  But for some reason I'm just not connecting the dots, 


does anyone have a complete example of how to do this (unike SF unreliable peice mill approach)

To fix this error, you need a pair of curly braces for the for(RecordType rt: rtypes) loop.
I have seen Salesforce accept for loops without curly braces, so I don't understand why you're getting that error.  However, I always use the braces because I find it easier to read