trigger AnypointEmailNotificationsTrigger on acm_pkg__AnypointContracts__ChangeEvent (after insert) {
List<acm_pkg__AnypointContracts__ChangeEvent> events = Trigger.new;
acm_pkg__.AnypointEmailNotifications.notifyOnContractUpdates(events);
}
Optionally Configure Branded Emails
Anypoint API Community Manager sends notification emails when an API contract is approved or revoked. By default these emails use Anypoint Platform addresses and branding. If you wish to configure them to use your own addresses and branding instead, use these steps.
Disable Standard Email Security
Branded emails require disabling Salesforce’s standard configuration for Sender Policy Framework (SPF) for email. For more information about this feature, see the Salesforce Help article Enable Email Security Compliance.
To disable the standard configuration, open Setup > Deliverability > Email Security Compliance and ensure the option Enable compliance with standard email security mechanisms is not selected.
Configure Email Addresses
-
Navigate to Setup and use Quick Find to go to Organization-Wide Addresses.
-
Set Display Name to
EventEmailSender. -
Set Email Address to the email address at your organization that sends the API contract event notification emails.
Ensure you can read confirmation emails sent to this address.
-
Enable Allow All Profiles to Use this From Address.
-
Click Save.
-
Open the confirmation email sent to the notification sender email address and click the link to confirm the address.
Enable Custom Email Notifications
To enable custom email notifications, follow these steps.
Configure Data Change Capture
-
Navigate to Setup > Home > Integrations > External Data Sources and ensure Eligible for External Data Change Capture is enabled.
-
Set Polling Interval for External Change Data Capture (seconds).
The default is 1800 and the minimum is 300.
-
Navigate to Setup.
-
Use Quick Find to navigate to Change Data Capture.
-
Navigate to Entities > Change Manually > Available Entities.
-
Move AnypointContracts to the Selected Entities list, which enables polling on this object.
Create Apex Trigger
-
Navigate to Setup, use Quick Find to go to Apex Triggers, and click the Developer Console button.
-
Click File > New > Apex Trigger, set Name to
AnypointEmailNotificationsTrigger, and set sObject toacm_pkg__AnypointContracts__ChangeEvent. -
Enter this code block:
Promote Apex Trigger to Production
-
In the sandbox organization, open Setup and use Quick Find to go to Outbound Change Sets.
-
Click Continue.
-
Click New, enter a name and description, and click Save.
-
Click Add under Change Set Components, set Component Type to Apex Trigger, and add
AnypointEmailNotificationsTrigger. -
Click Upload, set the target organization to the production organization, and click Upload.
-
In the production organization, open Setup and use Quick Find to go to Inbound Change Sets.
-
Click Continue.
-
If no change sets appear, do these steps:
-
Open Setup and use Quick Find to go to Deployment Settings. Click Edit next to your sandbox organization, enable Allow Inbound Changes, and click Save.
-
Return to Outbound Change Sets in the sandbox organization, click Upload, set the target organization to the production organization, and click Upload.
-
Wait for the confirmation banner, then return to Inbound Change Sets in the production organization.
-
-
Click Validate on the change set, set Test Option to Default Test Option, and click Validate.
The change set validation might fail with this coverage error: AnypointEmailNotificationsTrigger has 0% coverage. Each trigger must have at least 1% code coverage.This error occurs because the trigger has no test coverage. To resolve it, create a test class as described in the next steps. -
In the sandbox organization, open the Developer Console, click File > New > Apex Class, set Name to
AnypointEmailNotificationsTriggerTest, and click OK. -
Enter these code and save:
@IsTest public class AnypointEmailNotificationsTriggerTest { @IsTest static void testEvent() { acm_pkg__AnypointContracts__ChangeEvent event = new acm_pkg__AnypointContracts__ChangeEvent(); EventBus.ChangeEventHeader header = new EventBus.ChangeEventHeader(); header.changeType='CREATE'; header.entityName='AnypointContracts__x'; header.changeOrigin='here'; header.transactionKey = 'some'; header.commitUser = 'me'; event.changeEventHeader = header; event.put('ExternalId', '476111:1094813'); event.put('acm_pkg__ApiVersionId__c', '66c1049c-ed5c-4f46-9751-33061eb2ce2b:productinformation:v1'); event.put('acm_pkg__ApiId__c', '15764979'); event.put('acm_pkg__InstanceId__c', '15764979'); event.put('acm_pkg__ApplicationId__c', '476111'); event.put('acm_pkg__Status__c', 'REVOKED'); event.put('acm_pkg__TierId__c', '218623'); event.put('acm_pkg__ContractType__c', 'Api'); Test.startTest(); Database.SaveResult sr = EventBus.publish(event); Test.getEventBus().deliver(); Test.stopTest(); System.assertEquals(true, sr.isSuccess()); } } -
Create a new outbound change set and click Add under Change Set Components. Add both:
-
AnypointEmailNotificationsTriggerunder Apex Trigger -
AnypointEmailNotificationsTriggerTestunder Apex Class
-
-
Upload the change set to the production organization.
-
In the production organization, open Inbound Change Sets, click Validate on the new change set, set Test Option to Run specified tests, enter
AnypointEmailNotificationsTriggerTest, and click Validate. -
After successful validation, click Deploy.
-
To verify the deployment, navigate to Setup > Custom Code > Apex Triggers and confirm that
AnypointEmailNotificationsTriggerappears in the list.
Configure Branded Email Metadata
-
Navigate to Setup and use Quick Find to go to Custom Metadata Types.
-
Click ACM Community Configuration Metadata > Manage Records > New to add a record.
-
Set Label to your API community’s name and press Tab to automatically complete the ACM Community Config Metadata Name.
-
Open the Developer Console and open the Query Editor.
-
Enter
SELECT Id, Name FROM Networkand click Execute. -
In the list of IDs and communities, find the row with Name equal to your community’s name, double-click the ID, and copy it.
-
In the ACM Community Config Metadata field NetworkId, paste the ID.
-
In the Query Editor, enter
SELECT Id, Name FROM EmailTemplateand click Execute. -
In the list of IDs and email templates, find the row with Name equal to Contract Approved, double-click the ID, and copy it.
-
In the ACM Community Config Metadata field ContractApprovedEmailTemplateId, paste the ID.
-
In the list of IDs and email templates, find the row with Name equal to Contract Revoked, double-click the ID, and copy it.
-
In the ACM Community Config Metadata field ContractRevokedEmailTemplateId, paste the ID.
-
In the Query Editor, enter
SELECT Id, Address FROM OrgWideEmailAddressand click Execute. -
In the list of IDs and email addresses, find the row with Address equal to the organization-wide email address you set, double-click the ID, and copy it.
-
In the ACM Community Config Metadata field OrgWideEmailAddressId, paste the ID.
-
Click Save.
-
Repeat this process for all additional communities, creating one record for each community.
NetworkId is different for each community. ContractApprovedEmailTemplateId, ContractRevokedEmailTemplateId, and OrgWideEmailAddressId may be the same for multiple communities.
Configure Branded Email Templates
-
Navigate to Setup, use Quick Find to go to Classic Email Templates, and navigate to Folder > ACM.
-
Customize the Contract Approved and Contract Revoked templates, or create your own templates and configure the ACM Community Config Metadata to use them.
-
Configure an email relay as described in the Salesforce documentation page Send Email Through Email Relay.
If the relay is not configured, notification emails are sent from a Salesforce domain with a sender email address in your organization’s domain. If the actual sender domain and sender email address domain do not match, many email providers mark the notification emails as spam.
Disable Default Anypoint Email Notifications
The JSON Web Token (JWT) authentication mechanism creates Anypoint user records with member email addresses. To prevent a community member from receiving the default Anypoint notification emails, alter these email addresses so the Anypoint user record email is not a valid address.
-
In Salesforce, navigate to Setup > Named Credentials.
-
Click the Name of the named credential
Anypointto open it in view mode.Anypointis the default named credential set in the external data source by the guided setup application. If the named credential has been changed manually, use the named credential configured in the Anypoint external data source. -
Click Edit.
-
In the Per User Subject formula, add
& ".donotsend" ` after `$User.Email. Example:"v2|8745gh23-590d-4h80-b339-1637854e289f|" & $User.Username & "|" & $User.FirstName & "|" & $User.LastName & "|" & $User.Email & ".donotsend" & "|[\"Community User\"]"
Anypoint Permissions Configuration
-
Log in to your API community as an administrative user.
-
In Access Management, click Users.
-
Click the impersonated Anypoint user with the email address of the Salesforce administrator.
-
Add the Organization Administrator role to the user on each organization and suborganization.
-
Click Permissions, click API Manager, and for each environment, open the permissions list and enable the Manage Contracts permission.
-
In the public side of the community, request access to an API with manual approval.
-
Log in to Anypoint and approve the requested API contract.
This creates the Anypoint user named Automated Process, which works with the Salesforce user named Automated Process to process triggers.
-
Grant the Anypoint user named Automated Process the Organization Administrator role in the main organization using Anypoint Access Management.
This enables the Anypoint user to process API contract events when API contracts are approved or revoked.



