Emails / Sendgrid
Introduction
Wattyo comes out of the box with the Sendgrid implementation for email sending and handling.
With sendgrid you can send custom emails, templated based ones and a lot more options. It also allows to send attachments along with the emails.
Additionally, with Sendgrid is also possible to track the sent email activity as when it was processed, opened and other events.
Sendgrid External Credentials
You must set the Named Principals Token parameter in the Sendgrid External Credentials in order to enable the integration.
The Token value must be retrieved from the Sendgrid Control Panel.
Sendgrid Settings CMT
In this CMT you need to setup different options:
Field | Type | Required | Description | Example |
---|---|---|---|---|
From Email | String | Required | The from email address to use that must be validated in the Sendgrid Control Panel | contact@wattyo.com |
Default Template Id | String | Optional | The default Sendgrid template Id to use in the outgoing emails. You must create it in Sendgrid and get the id from there. | d-46ee6f1d0cc044309929defe26e02d04 |
Setting Up Webhook Events
If you want to enable the email events management you need to tell Sendgrid to send them.
In Salesforce:
- Go to the Sendgrid Connected App -> Manage
- Select the default User for User Credential Flow
This configuration is performed in the Sendgrid Control Panel:
- Select the default User for User Credential Flow
- Mail Settings > Webhooks
-
Create or modify a webhook events setup and:
-
Set the endpoint url which should be
https:/**.my.salesforce.com/services/apexrest/wattyo/sendgrid
-
Set the Token URL:
https://****.my.salesforce.com/services/oauth2/token
-
Select the desire events. By default Wattyo handles only the ‘open’ event.
-
sendNotification()
The send notification method creates the sendrig request according to the received envelope.
Depending on the envelop it can:
- Send a custom email.
- Send an template based email.
- Send attachments.
When sending the email, Wattyo sets the wattyo_id custom_arg that Sendgrid will store along with the email. This id is populated with the envelope’s notificationId and it’s sent back from Sendgrid with any event related to this email.
Webhook Listener
The SengridWebhookListener class is a RestResource that is in charge of listening for all the email events from Sendrig.
Out of the box it will listen for the open events and update the History Object notification status field with the Opened value.
Setup Sendgrid
- Go to App Manager -> Sendrgrid -> View:
- Edit and check the enable Client Credential Flow.
- Click Manage button -> Edit Policies -> Set the Client Credential User to the integration / admin user.
- Set the Consumer key and secret in sendgrid
- The scratch endpoint is like this: https://page-momentum-3932-dev-ed.scratch.my.salesforce.com/services/apexrest/wattyo/sendgrid
- The token url: https://page-momentum-3932-dev-ed.scratch.my.salesforce.com/services/oauth2/token
- You must set the TOKEN in the External Named Credential for sendgrid.
- Go to Principals, edit the Authorization record, and add the parameter “Token” with the Token value
- Optional: In the Sengrid control panel you must setup the desired webhook events.