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

Question about RecordType

I am writing a trigger about the "Timesheet" object and other six objects which have the lookup relationship with the 'Timesheet' and also the Record Types of 'Timesheet' .  There is a field named Hours in the 'Timesheet' and Hours are also in all the other six objects. When i create a new 'Timesheet'  i need to choose the RecordType first. For example, it is a Project Timesheet. Then, when i put number  in the Hours field and click Save button. The Hours of Project should also be updated. I have tested my trigger in a single object, it does work. The problem comes when i need to make it works for six objects. I need to write a if statement to judge which recordType is selected. How can i get the value of the selected recordType? Thanks very much for any help

Best Answer chosen by Admin (Salesforce Developers) 

I think there's 2 questions you're asking:


1. Checking the record type for a record in an Apex trigger

2. Using common code for your 6 objects (unless you just have 6 record types).


1.  If an object has record types it will have a RecordTypeId field you can access and a relationship to record types. To get the record type name for an Account use the following SOQL:


Select RecordTypeId, RecordType.Name From Account


2. You can write an Apex class with a generic method that takes an SObject (or collection thereof). This can use the instanceof keyword to determine which SObject you have and cast the SObject record back to the appropriate object for processing. This allows you to encapsulate your business logic in one method instead of 6 - but it's obviously harder to debug and needs the same number of unit tests.