2

We're seeing an issue with email messages (created via the API) not showing up on a contact's activity timeline if the contact is the sender, but does show up if the contact is the recipient. Below are sample API requests and responses that insert the EmailMessage object, setting a handful of properties. The FromAddressRelationId__c property is set with the Salesforce ContactId because there is a trigger called InsertEmailMessageRelationForFromAddress that runs after EmailMessage insertion and uses the value of the field to create an EmailMessageRelation record for the FromAddress RelationType, using the ContactId for the RelationId property. The code for the trigger is listed below.

We're seeing that the EmailMessageRelation records look correct for both of the below example cases, but that there is no Task record created for the Contact in the case where the Contact is the sender of the email, and there also is no ActivityId set on the EmailMessage record. The trigger is essentially the same sample code we see at the bottom of the EmailMessage page here - https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_emailmessage.htm. It would seem as though the trigger doesn't create a corresponding Task record for the Contact, but if you ran an API request to insert an EmailMessageRelation record instead, a Task would automatically be created for the Contact, it and it would also be linked to the EmailMessage.

Is there something else that needs to be done in the trigger when inserting the EmailMessageRelation record to have the system automatically create the linked Task for the Contact/EmailMessage like the API does?

# Activity does NOT show up in Contact 0035c00002TOjhQAAT timeline
# Task record with WhoId 0035c00002TOjhQAAT does NOT exist
POST /services/data/v53.0/composite/sobjects
{
    "records": [
        {
            "attributes": {
                "type": "emailmessage"
            },
            "FromName": "Some Name",
            "MessageDate": "2023-02-01T22:05:51.000-05:00",
            "Subject": "Test Email 1",
            "TextBody": "Content for Test Email",
            "Status": "3",
            "Incoming": true,
            "ToAddress": "[email protected]",
            "CcAddress": "",
            "BccAddress": "",
            "ToIds": [
                "005o0000000q37tAAA"
            ],
            "FromAddressRelationId__c": "0035c00002TOjhQAAT"
        }
    ]
}

[
    {
        "id": "02s5c000019g09hAAA",
        "success": true,
        "errors": []
    }
]
# Activity does show up in Contact 0035c00002TOjhQAAT timeline
# Task record with WhoId 0035c00002TOjhQAAT exists
POST /services/data/v53.0/composite/sobjects
{
    "records": [
        {
            "attributes": {
                "type": "emailmessage"
            },
            "FromName": "Other Name",
            "MessageDate": "2023-02-01T22:47:27.000-05:00",
            "Subject": "Re: Test Email 1",
            "TextBody": "Reply content for Test Email",
            "Status": "3",
            "Incoming": false,
            "ToAddress": "[email protected]",
            "CcAddress": "",
            "BccAddress": "",
            "ToIds": [
                "0035c00002TOjhQAAT"
            ],
            "FromAddressRelationId__c": "005o0000000q37tAAA"
        }
    ]
}

[
    {
        "id": "02s5c000019g0DFAAY",
        "success": true,
        "errors": []
    }
]
trigger InsertEmailMessageRelationForFromAddress on EmailMessage (after insert) {
    if (Trigger.isInsert) {
        List<EmailMessageRelation> emrsToInsert = new List<EmailMessageRelation>();
        for (EmailMessage emailMessage : Trigger.new) {
            if (emailMessage.FromAddressRelationId__c == null) {
                return;
            }
            string fromAddressRelationId = emailMessage.FromAddressRelationId__c;

            EmailMessageRelation emailMessageRelation = new EmailMessageRelation(
                EmailMessageId = emailMessage.Id,
                RelationId = fromAddressRelationId,
                RelationType = 'FromAddress'
            );

            emrsToInsert.add(emailMessageRelation);
        }
        
        if (!emrsToInsert.isEmpty()) {
            insert emrsToInsert;
        }
    }
}

1 Answer 1

0

Salesforce support was able to provide a solution to this by splitting out the trigger into a separate class that's called as a @future method

trigger InsertEmailMessageRelationForFromAddress on EmailMessage (after insert) {
    
    if (Trigger.isInsert) {
        for (EmailMessage emailMessage : Trigger.new) {
            EmailMessageRelationshipClass.createEmailMessageRelationship(emailMessage.Id);
        }
    }
}

public class EmailMessageRelationshipClass {

    @future
    public static void createEmailMessageRelationship(String emailMessageId) {
    
        List<EmailMessage> emsgList =[Select Id, FromAddressRelationId__c from EmailMessage where id=:emailMessageId];
        List<EmailMessageRelation> emrsToInsert = new List<EmailMessageRelation>();
       
        for (EmailMessage emailMessage : emsgList) {
            if (emailMessage.FromAddressRelationId__c != null) {
                string fromAddressRelationId = emailMessage.FromAddressRelationId__c;
                EmailMessageRelation emailMessageRelation = new EmailMessageRelation(
                    EmailMessageId = emailMessage.Id,
                    RelationId = fromAddressRelationId,
                    RelationType = 'FromAddress'
                );
            
                emrsToInsert.add(emailMessageRelation);
            }
        }
        
        if (!emrsToInsert.isEmpty()) {
            insert emrsToInsert;
        }
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .