0
votes

I'm need to create email in Sales Force with the recipient as a custom object rather than a User, Contact or Person. Unfortunately, it appears my client has created a custom object for the entity we need to contact. I tried several methods in the UI and they have all ended up needing a User or Contact record to succeed. I'm now using APEX code in a trigger, it also requires a User, but I have managed to get past that using this work around: Sending Emails in Salesforce to Non-Contacts Using Apex

Now I need to pass my custom object into the Email Template to get the merge fields from it. I believe setWhatId is the correct approach, but it returns "Invalid entity type for whatId". Not sure where to turn now....

In short, I have a custom object that is not a user or a contact, but it does have a related custom object with an email address field (also not user or contact). I need to send an email to the email address recorded on the related custom object passing the custom object to the template to gain its merge fields.

Any help or guidance would be much appreciated. Can I even do this?

Best Regards, Rod

1
Is the related custom object with the email address a parent or child of the object you want to use?Gareth Jordan
Yes it is, I've just discovered the relationship, Its goes from my custom object to a Client 'Custom Object' then to the Contact. Not sure on the syntax yet, but I need to find that in the after insert trigger on my custom object.Rod
This is what I have, but it doesn't appear to like the dot notation this deep. Contact c = [select id, Email from Contact where id=q.Client__c.Participant_Portal_Contact__c.id limit 1];Rod
Your relationship can go 5 levels, but you didn't answer my initial question depending on if the related custom object is a parent record or child record the answer would be different.Gareth Jordan

1 Answers

0
votes

From your comment and without knowing much more, the relationship traversal should look like this:

Contact c = [select id, Email from Contact where id=q.Client__r.Participant_Portal_Contact__r.id limit 1];

Assuming that the email address field is on a parent object you dont need to do this with code you can probably do this using a Visualforce Email template and the relatedTo set to the object with the details you want to use as merge fields. That way you can use it in workflow or process builder or flows without the need for code