How To

Post to Chatter When Opportunity Stage Is Not Updated in Over 30 Days

Chatter

This blog post is a built solution based on a post in the Salesforce Automation Trailblazer Community by  Edixon Puglisi. Edixon wanted to automatically create a Chatter post to the opportunity owner reminding them that their opportunity has had the same stage for over 30 days. The solution was posted by my friend, Michelle Hansen, about using a daily scheduled flow that fires when an Opportunity’s Stage Duration is equal to 30/31 days. I took the solution a bit further by having it post only once and not every day until the opportunity stage is updated (which could be a bit much to be posted every day).

TrailblazerCommunity

Here are a few lessons learned from implementing this use case:

  • Learn how to use a before save record triggered flow to update information on the same record. 
  • Learn how to create a scheduled flow to create a Chatter post.

Before save record triggered flow is a relatively new feature, introduced in Spring ’20. This automation is more performant than using process builder (which is after record save) to make updates on the same record that fired the automation. 

Business Use Case:  Addison Dogster is the system administrator at Universal Containers. Mary Markle is the Director of Sales. She noticed that several opportunities are sitting in the same stage for over 30 days and she needed a mechanism to automatically notify the opportunity owner that they need to take action.

Solution: Addison needs a way to capture when the opportunity stage was last updated and then from there, calculate the stage duration in order to send the notification. This will be solved by creating two new opportunity fields.

Then, normally, Addison would build a process that would be triggered when a opportunity record is created or the stage is updated to set the stage last updated field with the current date. But, she remembered there was the new flow feature – before save that she’s been wanting to test out. In this case, she wants to update a field on the same opportunity record so she can perform this action before the record is saved as opposed to after.

Next, once the duration of the Stage Last Updated has met or exceeded 30 days, she needed to post to Chatter automatically. However, she did not want reminder to become an annoyance to the Sales Rep, so she is only going to post it once.

The user experience looks like this for a new record, a stage update to an existing record and when a existing record has exceeded 30 days for the stage duration.

NewOppRecordDemoView image full screen

ExistingOppRecordDemoView image full screen

ExistingOppRecordPostChatterDemoView image full screen

The automation solution looks like this:

UpdatetheStageLastUpdatedDate

This before save record-triggered flow does the following: (1) determines whether the record is new or existing, (2) if an existing record, get the record’s previous stage value, (3) determine if the stage was updated, (4) if the record is new or the stage is updated, then update the Stage Last Updated field to today’s date and set the Chatter Reminder Posted field to “Pending.”PostToChatterforOppStageNotUpdatedOver30Days

This daily scheduled flow runs when the Stage Duration field is equals to or greater than 30 and the Chatter Reminder Posted field to “Pending,” then (1) a chatter post is created to the opportunity owner and (2) the Chatter Reminder Posted field is updated from “Pending” to “Yes.”

Highlighted Steps: 

1.Create a few custom fields on the Opportunity object and grant the FLS for the fields accordingly.

A. We need to create a date field called Stage Last Updated, which will store the date the stage was set.

Implementation Note: You will need set the this field to a date the stage was last set for existing opportunities so this automation can fire on existing records. 

Best practice tip: Provide a description so you and other/future admins know what this flow resource is used for.

StageLastUpdated

View image full screen

B. We need to create a number formula field called Stage Duration that tracks the number of days since the stage was last set. 

Best practice tip: Provide a description so you and other/future admins know what this flow resource is used for.

If the Stage Last Updated field is not blank, then calculate how many days has passed since the stage was last updated. Otherwise, leave the field blank.

The formula syntax is:

If ( NOT(ISBLANK(Stage_Last_Updated__c)), today() – Stage_Last_Updated__c, null)

StageDurationView image full screen

C. Lastly, create a picklist field called Chatter Reminder Posted with values: Yes, No and Pending. Note: The No option will not be never be used in this scenario but may be useful if you have opportunity record types for which you do not want this automation for. You would also need to add the record type into the filter options for the flow as well. 

Implementation Note: You will need set the this field to Pending for existing opportunities so this automation can fire on existing records. 

Best practice tip: Provide a description so you and other/future admins know what this flow resource is used for.

ChatterReminderPosted

View image full screen

2. The first flow (before save record-triggered flow) we are going to create is to update the Stage Last Updated and Chatter Reminder Posted fields for new records or existing records where the stage has changed.

UpdatetheStageLastUpdatedDate

For those using Salesforce Classic, flow can be found in Create | Workflows & Approvals | Flows. In Lightning Experience, it is found under Process Automation | Flows. Click on “New Flow.” Select Record-Triggered Flow. Click on the Create button.

In the flow, we would configure the following flow resources.

A. We need to create a formula resource which will set the date to today.

Best practice tip: Provide a description so you and other/future admins know what this flow resource is used for.

This is how that flow resource would be configured.

B. Let’s configure the record trigger by clicking the Edit link in the Start box for the Trigger. Configure as follows:

    • Trigger the Flow When: A record is created or updated
    • Run the Flow: Before the record is saved 

UpdatetheStageLastUpdatedDate-ConfigureTrigger

Then, click the Edit link next to the Object and set it to Opportunity.

C. First, we need to configure a Decision flow element called New or Existing? We have different paths for if the record is new or if the record is existing. In the case that the record is existing, we will retrieve the previous stage. We don’t need this step for a new record.

New records, since they are not yet created in Salesforce, do not have an Id so to identify a new record, the opportunity Id is null. For existing records, an opportunity Id will exist (is null = false).

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure as follows:

    • Outcome: New
      • $Record>Opportunity Id Is Null True
    • Outcome: Existing
      • $Record>Opportunity Id Is Null False

UpdatetheStageLastUpdatedDate-DecisionView image full screen

D. Next, create a Get Records flow element called Get Previous Record’s Opp Stage. For an existing record, we need the opportunity stage before the change so we can do a comparison and see if the opportunity stage has changed since there is no IsChanged function in flow yet. 

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure to match the below:

    • Object: Opportunity
    • Condition Requirements: All Conditions Are Met (AND)
    • Filter: Id Equals $Record>Opportunity Id
    • How Many Records to Store: Only the first record
    • How to Store Record Data: Choose fields and let Salesforce do the rest
    • Select Opportunity Fields to Store in Variable: StageName

UpdatetheStageLastUpdatedDate-GetRecordsView image full screen

E. We need another Decision flow element called Was the Stage Updated to determine whether the opportunity stage was updated or not. This is done comparing the new stage value to the previous stage value.

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure to match the below:

    • Outcome: Stage Updated
      • $Record>Stage Does Not Equal Opportunity from Get_Previous_Record_s_Opp_Stage>Stage
    • Default Outcome: Stage Not Updated

UpdatetheStageLastUpdatedDate-Decision2View image full screen

F. For our last flow element, we will create an Assignment called Update Opp. Here, we will stamp the Stage Last Updated field with today’s date and update the Chatter Reminder Posted field to “Pending.”

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure to match the below:

    • $Record> Stage Last Updated Equals TodayFormula
    • $Record> Chatter Reminder Posted Equals Pending

UpdatetheStageLastUpdatedDate-AssignmentView image full screen

G. Now, we need to connect the flow elements to match the screenshot below.

UpdatetheStageLastUpdatedDate-Connectors

H. Save your flow. Let’s call it Update the Stage Last Updated Date.

Best practice tip: Provide a description so you and other/future admins know what this flow is for.

UpdatetheStageLastUpdatedDate-Properties

I. Activate the flow.

3. Now, let’s create our scheduled flow that will run daily and create the chatter post for any opportunities that have had the same opportunity stage for over 30 days and the chatter reminder has not already been posted.

PostToChatterforOppStageNotUpdatedOver30Days

For those using Salesforce Classic, flow can be found in Create | Workflows & Approvals | Flows. In Lightning Experience, it is found under Process Automation | Flows. Click on “New Flow.” Select Scheduled Flow. Click on the Create button.

In the flow, we would configure the following flow resources.

A. We need to create a text template resource that will hold our Chatter message that at mentions the Opportunity owner and references the opportunity name, current opportunity stage and stage duration using merge fields.

Best practice tip: Provide a description so you and other/future admins know what this flow resource is used for.

This is how that flow resource would be configured.

    • Body: @[{!$Record.Owner.Id}] – Opportunity {!$Record.Name} has been in the {!$Record.StageName} for {!$Record.Stage_Duration__c} days. Please review the opportunity and update it accordingly.

ChatterMessageView image full screen

B. In the Start box, click the Edit link to specify the scheduled flow start date, time and frequency.

C. Also, in the Start box, click the Edit link to specify the object and the filter conditions.

Configure as follows:

    • Object: Opportunity
    • Condition Requirements: All Conditions Are Met (AND)
      • Stage_Duration__c Greater Than or Equal 30
      • Chatter_Reminder_Posted__c Equals Pending

PostToChatterforOppStageNotUpdatedOver30Days-ObjectandFilterConditionsView image full screen

D. First, we are going to drag an Action flow element. In the Action search box, type Chatter and select Post to Chatter.

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure as follows:

    • Message: {!ChatterMessage}
    • Target Name or ID: {$Record.Id}

PostToChatterView image full screen

E. Next, we need an Update Records flow element called Update the Chatter Reminder Posted Field that will update the Chatter Reminder Posted Field from “Pending” to “Yes.”

Best practice tip: Provide a description so you and other/future admins know what this flow element is used for.

Configure as follows:

    • How to Find Records to Update and Set Their Values: Specify conditions to identify records, and set fields individually
    • Object: Opportunity
    • Condition Requirements for Records to Update: All Conditions Are Met (AND)
    • Id Equals $Record> Opportunity Id
    • Set Field Values for the Opportunity Records: Chatter_Reminder_Posted__c Yes

PostToChatterforOppStageNotUpdatedOver30Days-UpdateRecordsView image full screen

F. Now, we need to connect the flow elements to match the screenshot below.

PostToChatterforOppStageNotUpdatedOver30Days-Connectors

H. Save your flow. Let’s call it Post to Chatter for Opp Stage Not Updated Over 30 Days.

Best practice tip: Provide a description so you and other/future admins know what this flow is for.

PostToChatterforOppStageNotUpdatedOver30Days-Properties

I. Test the scheduled flow by setting updating or creating a record that will be picked up in the scheduled flow. Then click the Debug function. This will pick up one record for processing.

Set the Stage Last Updated date so that the Stage Duration is 30 days or more and set the Chatter Reminder Posted field to “Pending.”

J. Activate the flow.

Now, before you deploy the changes to Production, don’t forget to test the rest of your configuration changes.

  1. Create a new opportunity. Verify that the Stage Last Updated is set to today, Stage Duration is 0 and Chatter Reminder Posted is “Pending.”
  2. Update an existing opportunity by updating the stage. Verify that the  Stage Last Updated is set to today, Stage Duration is 0 and Chatter Reminder Posted is “Pending.”
  3. Update an existing opportunity and do not update the stage. Verify that the Stage Last Updated, Stage Duration and Chatter Reminder Posted fields were not updated.
  4. Manually run the scheduled flow (by updating the start time, saving and activating) and verify that the chatter post was created and the Chatter Reminder Post field on the opportunities was updated to “Yes” for records picked up in the scheduled flow.

Troubleshooting tip: If your scheduled flow is not working as expected, set a debug log on the Automated Process user, run the scheduled flow and review the debug logs.

Deployment Notes/Tips:

  • Flow can be deployed to Production in a change set (or can be deployed using a tool such as Metazoa’s Snapshot).
  • You will find the flow in a change set under the Flow Definition component type.
  • Activate the flow post deployment as flows deploy inactive in Production, unless you have opted in on the Process Automation Settings screen, to “Deploy processes and flows as active.” NOTE: With this change, in order to successfully deploy a process or flow, your org’s Apex tests must cover at least 75% of the total number of active processes and active autolaunched flows in your org or you can select 0%, which will run the apex classes not related to your flow.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s