[SalesForce] Filter the trigger on parent object based on the record type type on child object

I have 2 custom objects with master-detail relationship:

custom_object__1(parent)
custom_object__2(child)

I have an after update trigger on the Custom_Object__1(parent) and also I have the if condition where the fields are set as criteria based on which the trigger.new is fired now I need to need to include the record type of the child object also on the filter type any approach on how to do this?

Best Answer

Code of this sort will allow you to use the developer name of the record type in your logic:

trigger MyTrigger on custom_object__1 (after update) {

    // Map is custom_object__1 to developer name of custom_object__2 record type
    Map<Id, String> m = new Map<Id, String>();
    for (custom_object__2 c2 : [
            select custom_object__1, RecordType.DeveloperName
            from custom_object__2
            where custom_object__1 in :Trigger.newMap.keySet()
            ]) {
        m.put(c2.custom_object__1, c2.RecordType.DeveloperName);
    }

    for (custom_object__1 c1 : Trigger.new) {
        String developerName = m.get(c1.Id);
        if (developerName == 'xyz') {
            ...
        }
    }
}
Related Topic