0

I want to prevent sandbox environments from sending Workflow emails to the "Additional Emails" specified in Email Alerts using the Metadata API in Apex.

I tried using the Tooling API, but it only allows metadata retrieval and doesn’t support updates. After researching, I found that the Metadata API might help achieve this. However, I haven’t been able to find a clear solution for this use case.

I have created below Apex class to retrieve and update CCEmail using Metadata API with the help of MetadataService class. Please have a look in the code here:

public class UpdateCCEmail { public void updateEmailAlertCC(String emailAlertFullName, List<String> newCcEmails) { // Step 1: Create MetadataService instance MetadataService.MetadataPort service = new MetadataService.MetadataPort(); // Use Named Credential directly; this assumes you have 'MetadataAPI' defined correctly. service.endpoint_x = 'callout:MetaNameCred/services/Soap/m/58.0'; // Use the appropriate version // Step 2: Prepare the session header (if necessary) service.SessionHeader = new MetadataService.SessionHeader_element(); service.SessionHeader.sessionId = UserInfo.getSessionId(); // Step 3: Read the existing EmailAlert metadata MetadataService.IReadResult readResult = service.readMetadata('WorkflowAlert ', new String[] { emailAlertFullName }); if (readResult == null || readResult.getRecords().size() == 0) { System.debug('No records found for the specified Email Alert: ' + emailAlertFullName); return; } // Step 4: Cast the metadata to EmailAlert type MetadataService.WorkflowAlert emailAlert = (MetadataService.WorkflowAlert) readResult.getRecords()[0]; // Step 5: Update the CC Emails emailAlert.ccEmails = newCcEmails; // Step 6: Prepare the update request as Metadata MetadataService.Metadata[] updateRequest = new MetadataService.Metadata[] { emailAlert }; // Step 7: Perform the update call with the correct parameter MetadataService.SaveResult[] saveResults = service.updateMetadata(updateRequest); // Step 8: Check for success for (MetadataService.SaveResult saveResult : saveResults) { if (saveResult.success) { System.debug('Email Alert updated successfully: ' + emailAlertFullName); } else { System.debug('Failed to update Email Alert: ' + saveResult.errors[0].message); } } } } 

I came across similar issues posted by others:

How to prevent emails to 'Aditional Email' on email alerts

Preventing sandbox from sending Workflow emails to "Additional Emails"

However, none of these discussions provide a concrete solution.

Could anyone suggest how to programmatically mask, remove, or disable these alerts in a sandbox?

10
  • This question is similar to: Preventing sandbox from sending Workflow emails to "Additional Emails". If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 4, 2024 at 17:16
  • 1
    ^^^ Merely 11+ year old answer is still correct. The more things change, the more they stay the same. Commented Oct 4, 2024 at 17:16
  • You need to describe the specific error or issue with your class. Please edit your post to include those details. Commented Oct 4, 2024 at 22:38
  • @identigral, In the suggested solution, they advised changing the deliverability setting to "System Email Only." However, this is not the appropriate fix for my requirement. I need a more generic solution, such as an Apex code script that interacts with the sandbox API to update each Workflow Alert, specifically modifying the value in the "Additional Emails" field, e.g., changing [email protected] to [email protected]. Commented Oct 5, 2024 at 4:27
  • 1
    Are you using the opensource MetadataService? Have you checked the issues in that repository and done any other research? Commented Oct 7, 2024 at 15:45

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.