+ Start a Discussion
Radha Rathinavel PandianRadha Rathinavel Pandian 

how to get my record type name in before insert in apex

Hi,

How to get the record type name in before insert.

Requiremnet is need to check in if condition for record type name 
Ex : if(record type name= 'A' ) 
Best Answer chosen by Radha Rathinavel Pandian
GauravGargGauravGarg
Hi Radha,

You need to compare Obj.RecordTypeId with Actual RecordtypeId, as we cannot find RecordType.Name directly in new list without querying it. 

Please follow this:
 
if(obj.RecordTypeId == Schema.SObjectType.OBJECTAPIName.getRecordTypeInfosByName().get('RecordTypename').getRecordTypeId()


Hope this is useful

Thanks,

Gaurav

All Answers

RamakantRamakant
Hi Radha,
Lets say you are working on Lead object  and you want to execute some code for specific record Type.
trigger recordTypeCheck on Lead (before insert) {
    for(Lead led : Trigger.new) {
		if(led.recordType.Name == 'Your RecordType Name'){
			//Write your Logic here
		}
    }
}

Please mark as best answer if helped.

Regards,
Ramakant 
Radha Rathinavel PandianRadha Rathinavel Pandian
Ramakant,
 I tried the below code, but it is not working . please correct me if I were wrong
 for(Case currCase : trigger.new){
        
        if(currCase.recordType.Name ==  'Certificate of Purchase Assignment form' )
        {
        currCase.Check_Legal_Description__c = true;
        }
{!Piyush_soni__c}{!Piyush_soni__c}
Hi Radha,

 in your if condition use recod type API name, not lable name :

User-added image
Not record type lable ,
 
trigger recordTypeCheck on objectName (before insert) {

 for(objectName obj : trigger.new){
        
        if(obj.recordType.Name ==  'RecordTypeAPI_Name' )
        {
            obj.Check_Legal_Description__c = true;
        }
  }
}

Thanks, let us know if it helps you.
 
GauravGargGauravGarg
Hi Radha,

You need to compare Obj.RecordTypeId with Actual RecordtypeId, as we cannot find RecordType.Name directly in new list without querying it. 

Please follow this:
 
if(obj.RecordTypeId == Schema.SObjectType.OBJECTAPIName.getRecordTypeInfosByName().get('RecordTypename').getRecordTypeId()


Hope this is useful

Thanks,

Gaurav

This was selected as the best answer