如何编写Workflow阻止特定记录被删除?例如Account Name为"ABC"的记录
Absolutely! Let's clear up a quick point first: traditional Salesforce Workflow Rules can't directly block record deletion—they don't fire when records are deleted, and they don't have a way to halt the delete action. But don't worry, you've got two reliable ways to make this happen: a no-code Record-Triggered Flow, or an Apex Trigger if you're comfortable with code. Let's walk through both options step by step:
Option 1: No-Code Solution with Record-Triggered Flow
This is my go-to for admin-friendly requirements:
- Head to Setup → Process Automation → Flows and click New Flow.
- Pick Record-Triggered Flow and hit Create.
- Configure the trigger settings:
- Object: Select Account
- Trigger the Flow When: Choose A record is deleted
- Entry Conditions: Add a rule where
Account Nameequals "ABC" (double-check case sensitivity here—if your org ignores case, you can toggle that setting in the condition)
- Next, drag an Error element onto the canvas and connect it to your entry condition.
- Fill in the error message with something clear, like: "Deletion of the 'ABC' Account is not allowed for any user."
- Save your flow, give it a descriptive name (like "Block ABC Account Deletion"), and activate it.
Option 2: Code-Based Solution with Apex Trigger
If you prefer using code, this trigger will do the job cleanly:
trigger BlockABCAccountDeletion on Account (before delete) { for (Account accountToDelete : Trigger.old) { // Check if the account name matches exactly (adjust to equalsIgnoreCase if needed) if (accountToDelete.Name == 'ABC') { accountToDelete.addError('Deletion of the "ABC" Account is prohibited for all users.'); } } }
A few quick notes on this trigger:
- It runs before delete, so it catches the deletion attempt before it completes.
- The
addError()method stops the deletion and shows your custom message to the user. - If your org uses case-insensitive field matching, swap
== 'ABC'with.equalsIgnoreCase('ABC')to cover variations like "abc" or "Abc". - Don't forget to write a test class for this trigger to meet Salesforce's deployment requirements!
Extra Tips
- If you need to let specific users (like admins) delete this account, you can add an extra condition. For example, in the flow, check the current user's profile ID; in the trigger, use
UserInfo.getProfileId()to exclude certain profiles. - Always test these solutions in a sandbox first to make sure they work as expected without breaking other processes.
内容的提问来源于stack exchange,提问作者vidhi
相关产品推荐
相关产品推荐

