如何在Salesforce中创建工作流统计Contact记录保存点击次数?
Got it, let's walk through how to set up a workflow that increments the "Number Of Updates" field on a Salesforce Contact every time the record is saved. I'll cover both a code-based approach and a no-code option so you can pick what works best for your team.
Method 1: Apex Trigger (Code-Based)
First, make sure you've created a Number field on the Contact object (let's assume its API name is NumberOfUpdates__c—replace this with your actual field name if it's different). Then create a before update trigger that runs right before the record is saved, which lets us update the counter field without triggering an extra save cycle.
Here's the trigger code:
trigger ContactUpdateCounter on Contact (before update) { for(Contact contactRecord : Trigger.New) { // Handle null values for the first update if(contactRecord.NumberOfUpdates__c == null) { contactRecord.NumberOfUpdates__c = 1; } else { contactRecord.NumberOfUpdates__c += 1; } } }
Key Notes:
- The
before updatecontext ensures the counter is updated in the same database transaction as the original save, so no extra API calls or record locks are needed. - Always handle null values—if the field was never set before, we start at 1 instead of hitting a null pointer error.
- This works seamlessly for both single-record edits and bulk updates (like list view edits or Data Loader operations).
Method 2: Record-Triggered Flow (No-Code)
If you prefer not to write Apex, Salesforce Flow Builder can handle this just as effectively. Here's a step-by-step setup:
- Go to Setup > Flows and click New Flow. Select Record-Triggered Flow and click Create.
- Configure the trigger settings:
- Object:
Contact - Trigger Event:
A record is updated - Entry Conditions: (Optional) If you only want to count updates that modify specific fields, set conditions here. Otherwise, leave it as "All records" to count every save.
- Optimize the flow for:
Fast Field Updates(since we're only updating a single field)
- Object:
- Add an Update Records element to your flow:
- Choose "Select the records that meet specific conditions"
- Object:
Contact - Condition:
Record IDequals{!$Record.Id}(this targets the exact record being updated) - Under "Set Field Values for the Records", find your "Number Of Updates" field. Set its value to this formula:
IF(ISBLANK($Record.NumberOfUpdates__c), 1, $Record.NumberOfUpdates__c + 1)
- Save your flow with a clear name (like "Contact Update Counter Flow") and click Activate.
Key Notes:
- The formula handles null values, so the first save will set the counter to 1 instead of leaving it blank.
- To avoid accidental loopbacks (the flow triggering itself when it updates the counter field), adjust the entry conditions to exclude updates where only the "Number Of Updates" field changed. Just add a condition like
{!$Record.NumberOfUpdates__c} != {!$Record_Old.NumberOfUpdates__c}and uncheck that condition in the entry criteria.
Testing Your Implementation
Whichever method you choose, test it thoroughly to ensure it works as expected:
- Create a new Contact record (the counter field should be blank initially).
- Edit the record and click Save—the counter should jump to 1.
- Edit it again and save—now it should be 2, and so on.
- Try a bulk update (e.g., edit multiple Contacts from a list view) to confirm all counters increment correctly.
内容的提问来源于stack exchange,提问作者liraz yakobovich

