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
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

RKSalesforceRKSalesforce
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;
        }
sfdcMonkey.comsfdcMonkey.com
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
Ravi KoduruRavi Koduru
Hi Gaurav,

Can we capture the recordtypeid before saving the new opportunity in the controller class?

Thanks,
Ravi
GauravGargGauravGarg
Hi Ravi, 

RecordType is always available you just need to fire the below statement to get the value. 
Schema.SObjectType.OBJECTAPIName.getRecordTypeInfosByName().get('RecordTypename').getRecordTypeId()

Thanks, 
Gaurav