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
DevelopementDevelopement 

Trigger to track what a previous Case Record Type is?

Hello,

We have situation where people are changing Case Record Type from one to another. I know we can track this by turning on the history tracking. But we want a way to see it as a field on the Case.

 

So I need to capture the old value of Record type in Old Value field

 

Record Type. 1) Regular 2) Strategic

Text field: Old Value

 

Can anyone help me to write an trigger?

 

 

Thanks a lot!

 

Shruti

Best Answer chosen by Admin (Salesforce Developers) 
Prafull G.Prafull G.

If you want to store the Old Record Type Id then the above code (posted by sales force) will work. However if you want to store the record type name then try the below one

 

trigger StoreOldRecordType on Object(before update) {
  map<Id, RecordType> mapRecordTypes = new map<Id, RecordType> ([select id, name from recordtype where sobjecttype='OBJECT API NAME']);

  

  for(Object var : trigger.New) {
    var.Old_Value__c = mapRecordTypes.get(trigger.oldMap.get(var.id).RecordTypeId).Name;
  }
}

 

Let me know if it helps.

All Answers

VaasuVaasu
The follwoing code stores record type id in the old value.If you want to store name you can query record type object with the id and store the name.

trigger triggerName on Object(before update)
{
for(Object var : trigger.New)
{
var.Old_Value__c = trigger.oldMap.get(var.id).RecordTypeId;
}
}
Prafull G.Prafull G.

If you want to store the Old Record Type Id then the above code (posted by sales force) will work. However if you want to store the record type name then try the below one

 

trigger StoreOldRecordType on Object(before update) {
  map<Id, RecordType> mapRecordTypes = new map<Id, RecordType> ([select id, name from recordtype where sobjecttype='OBJECT API NAME']);

  

  for(Object var : trigger.New) {
    var.Old_Value__c = mapRecordTypes.get(trigger.oldMap.get(var.id).RecordTypeId).Name;
  }
}

 

Let me know if it helps.

This was selected as the best answer