How to Change the Record Type based on a Field Value?

How to Change the Record Type based on a Field Value?

Lets directly dive into the problem. This article is purely about changing the record type in a Salesforce Object based on a field value. We are going to take the example of Case Object and a Picklist field. By the end of this article, if you have further questions, feel free to reach out to us

Problem Statement

Let us take a “Case” object and look at automatically changing the record type based on the value of the “Type__c” picklist field. This Picklist has 2 values – Complaint and Request.:

  • If Type__c is “Complaint”, the record type should be set to “Complaint”.
  • If Type__c is “Request”, the record type should be set to “Request”.

Solution

We’ll create a trigger on the Case object that executes before insert and before update events. This trigger will check the value of Type__c and set the RecordTypeId accordingly.

Here’s the complete code for the trigger:

apex
trigger CaseTypeTrigger on Case (before insert, before update) {
// Initialize record type IDs to null
Id complaintRecordTypeId = null;
Id requestRecordTypeId = null;

// Retrieve the record type IDs based on record type names
Map<String, Schema.RecordTypeInfo> recordTypeInfoMap = Schema.SObjectType.Case.getRecordTypeInfosByName();

if (recordTypeInfoMap.containsKey(‘Complaint’)) {
complaintRecordTypeId = recordTypeInfoMap.get(‘Complaint’).getRecordTypeId();
}

if (recordTypeInfoMap.containsKey(‘Request’)) {
requestRecordTypeId = recordTypeInfoMap.get(‘Request’).getRecordTypeId();
}

// Iterate over the cases and set the record type ID based on Type__c value
for (Case caseRecord : Trigger.new) {
if (caseRecord.Type__c == ‘Complaint’ && complaintRecordTypeId != null) {
caseRecord.RecordTypeId = complaintRecordTypeId;
} else if (caseRecord.Type__c == ‘Request’ && requestRecordTypeId != null) {
caseRecord.RecordTypeId = requestRecordTypeId;
}
}
}

Let’s get to the explanation

  1. Initialization: We start by initialising the record type IDs for “Complaint” and “Request” to null.
  2. Retrieve Record Type IDs: Using the Schema.SObjectType.Case.getRecordTypeInfosByName() method, we retrieve the record type IDs based on their names. We check if the record type names “Complaint” and “Request” exist, and if they do, we store their IDs.
  3. Iterate Over Cases: We loop through each case in Trigger.new, which represents the new or updated cases.
  4. Set Record Type: For each case, we check the value of Type__c. If it matches “Complaint” and the complaintRecordTypeId is not null, we set the case’s RecordTypeId to complaintRecordTypeId. Similarly, if Type__c is “Request” and the requestRecordTypeId is not null, we set the case’s RecordTypeId to requestRecordTypeId.

This technique can be used in other objects, fields and field values as well. Let us know if you have any questions, You can write to [email protected]. For other technical white papers on salesforce, please visit the technical resources we have written over the years. Happy reading!