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
DhairyaDhairya 

Using Record Type value in Trigger

The scenario is : Record automatically created when record type is  equal to  "New InstallBase".

 

                               Trigger is working fine, just want to add if condition with record type.

 

Trigger Code: 

 

 

rigger addInstallBase on Case (after insert, after update) {
  // Install bases to create
  List<Install_Base__c> installBases = new List<Install_Base__c>();
  for(Case c:Trigger.new) {
  
      
  
    // if(c.recordtype.name.equals("Post Install Warranty Registration")
   // {
    // If the old version had an unchecked box...
    if(Trigger.isInsert || !Trigger.oldmap.get(c.id).Approve__c) {
      // And the new version has a checked box...
      if(c.Approve__c) {
        // Add a new Install Base
        installBases.add(new Install_Base__c(
          // Assign values from case here, some examples...
          Case__c=c.id,project_size__c=c.Project_Size_kWp__c));
      }
   // }
    }
  }
  
 Appreciate your response.
Thanks,
Dhairya

 

 

Best Answer chosen by Admin (Salesforce Developers) 
skodisanaskodisana

Hi,


Use the below code to get the Record Type Id without querying RecordType table.
Id recId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('New InstallBase').getRecordTypeId();

for(Case c:Trigger.new){
    if(c.RecordTypeId == recId){
            // your logic
    }
}

Thanks,
Kodisana

All Answers

skodisanaskodisana

Hi,


Use the below code to get the Record Type Id without querying RecordType table.
Id recId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('New InstallBase').getRecordTypeId();

for(Case c:Trigger.new){
    if(c.RecordTypeId == recId){
            // your logic
    }
}

Thanks,
Kodisana

This was selected as the best answer
DhairyaDhairya

Thanks For your response.

 

But as per best practises hard coed of recid is not preferable.

 

I fixed this with following logic : 

 

 

 

  List<RecordType> rtypes = [Select Name, Id From RecordType
                  where sObjectType='Case' and isActive=true];
      
     //Create a map between the Record Type Name and Id for easy retrieval
     Map<String,String> caseRecordTypes = new Map<String,String>{};
     for(RecordType rt: rtypes)
        caseRecordTypes.put(rt.Name,rt.Id);

  List<RecordType> rtypes = [Select Name, Id From RecordType
                  where sObjectType='Case' and isActive=true];
      
     //Create a map between the Record Type Name and Id for easy retrieval
     Map<String,String> caseRecordTypes = new Map<String,String>{};
     for(RecordType rt: rtypes)
        caseRecordTypes.put(rt.Name,rt.Id);

 

 

Thanks a lot for prompt reply.

 

Dhairya

 

DhairyaDhairya

Its Solve.

 

Thanks a lot.

 

Dhairya

vkanthanrkvkanthanrk

Thank You for ur information...solved the issue...

 

Vignesh kanthan R K