function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ThathulaThathula 

Get the record type of Case

Hi all,

I need to check the record type we selected when we create the case. As an example please check below screenshot. 
i need to capture which record type user has created (i need to capture that in Triger.Insert
User-added image

Thanks alot
Best Answer chosen by Thathula
Vinoth Vijaya BaskerVinoth Vijaya Basker
Hi, 

Please find the below sample code, 
 
trigger LeadTriggerRT on Lead(before insert){

     Map<ID, RecordType> recordTypeMap = New Map<ID, RecordType>([Select ID, Name From RecordType Where sObjectType = 'Lead']);
     //Here 'TestLeadRecordType' is a record type name
     for(Lead leadNewRec : trigger.new){

          if(recordTypeMap.get(leadNewRec.recordTypeID).name.containsIgnoreCase('TestLeadRecordType')){
               
               System.debug('Record Type Name Matches');
          }

     }


}

Thanks,
Vinoth

All Answers

Vinoth Vijaya BaskerVinoth Vijaya Basker
Hi, 

Please find the below sample code, 
 
trigger LeadTriggerRT on Lead(before insert){

     Map<ID, RecordType> recordTypeMap = New Map<ID, RecordType>([Select ID, Name From RecordType Where sObjectType = 'Lead']);
     //Here 'TestLeadRecordType' is a record type name
     for(Lead leadNewRec : trigger.new){

          if(recordTypeMap.get(leadNewRec.recordTypeID).name.containsIgnoreCase('TestLeadRecordType')){
               
               System.debug('Record Type Name Matches');
          }

     }


}

Thanks,
Vinoth
This was selected as the best answer
Abhishek BansalAbhishek Bansal
Hi,

Please try the below code :
trigger checkRecordType on Case(before insert){
	Set<Id> recordTypeIds = new Set<Id>();
	for(Case cs : trigger.new){
		recordTypeIds.add(cs.RecordTypeId);
	}
	Map<ID, RecordType> recordTypeMap = New Map<ID, RecordType>([Select ID, Name From RecordType Where Id IN : recordTypeIds]);
	
	for(Case cs : trigger.new){
		if(recordTypeMap.containsKey(cs.RecordTypeId)){
			system.debug('Selected Record Type Name is :'+recordTypeMap.get(cs.RecordTypeId).Name);
			//recordTypeMap.get(cs.RecordTypeId).Name) This statement will give you the name of selected record type.
		}
	}
}

Let me know if you need more help on this.

Thanks,
Abhishek
ThathulaThathula
Thanks a lot for both you of..
I used following code  with the help of your code snippts.  i can only mark ones answer as the best answer. So i considered Vinoth answer.
I'm very sorry Abishek, you are soo awesome.. Thanks a lot for your kind answer. !

         Map<ID, RecordType> recordTypeMap = New Map<ID, RecordType>([Select ID, Name From RecordType Where sObjectType = 'Case']);
         
          if(recordTypeMap.get(caseO.recordTypeID).name.containsIgnoreCase('My record type')){


    }
ThathulaThathula
i considered Vinoth because he is the first one who replied me.