0

can anyone please shed some light on how to reference the RecordTypeId of the record related to an EmailMessage via the RelatedToId field?

I would like to update the IsExternallyVisible field based on the Record Types of the related object.

Example trigger code:

trigger MakeEmailsPublic on EmailMessage (after insert) {

    for(EmailMessage em: Trigger.new){

        if(em.RelatedToId == '*RecTypeId_1*' || em.RelatedToId == '*RecTypeId_2*' ){

            em.IsExternallyVisible=true;
        }
    }
}
0

This is the standard aggregate-query-update pattern, but with a twist: the relationship is polymorphic, so we need to do dynamic queries. Here's what the trigger would look like:

trigger MakeEmailsPublic on EmailMessage (after insert) {
    // Aggregate //
    Map<sObjectType, Set<id>> recordIdsByType = new Map<sObjectType, Set<Id>>();
    for(EmailMessage message: Trigger.new) {
        if(message.RelatedToId != null) {
            sObjectType key = message.RelatedToId.getSObjectType();
            Set<Id> recordIds = recordIdsByType.get(key);
            if(recordIds == null) {
                recordIdsByType.put(key, recordIds = new Set<Id>());
            }
            recordIds.add(message.RelatedToId);
        }
    }
    // Query //
    Map<Id, sObject> records = new Map<Id, sObject>();
    for(sOBjectType sType: recordIdsByType.keySet()) {
        if(sType.getDescribe().fields.getMap().containsKey('RecordTypeId')) {
            Set<Id> ids = recordIdsByType.get(sType);
            records.putAll(
                Database.query('SELECT RecordTypeId FROM '+sType+' WHERE Id = :ids')
            );
        }
    }
    // Update //
    for(EmailMessage message: Trigger.new) {
        sObject record = records.get(message.RelatedToId);
        if(record != null) {
            Id recordTypeId = (Id)record.get('RecordTypeId');
            if(recordTypeId == 'rectype1' || recordTypeId == 'rectype2') {
                message.IsExternallyVisible = true;
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.