Now you’ve all read the last Blog about Product Schedules and I know you're itching to get started with them, let’s look at making them a bit funkier.
Here’s the Scenario.
Toots Printers have implemented Product Schedules, it’s saving a lot of time and producing much more accurate revenue forecasting. However, they've hit a problem as the Close Dates on their Opportunities tend to move quite a lot and they want the Schedule dates of a Product to move with the Close Date.
OK, so how are we going to do this?
First things first, what tools within Salesforce will allow us to get to the Product Schedules?
After a lot of head scratching, turning Workflows and Process Builder inside out, it looked like it would have to be done with a Trigger.
Hmm, we always lean towards standard solutions in advance of code.
Whilst I ‘m hanging on for a developer I remembered an article I came across written by a guy called Nick Spencer (Thank you Nick you’re amazing!) who had done something similar with a mix of workflows, process builders and Visual Flow! Never being one to reinvent the wheel I thought I’d have a go at using his approach and adjusting it to meet the needs of Toots Printers.
So, onwards and upwards!
Let’s list out what we need:
Custom Date field ‘Days Shifted’ on the Opportunity as we need to capture how many days the date has moved by;
- 2 flows;
- Flow 1 to update the Line Schedule
- Flow 2 to update Opportunity Line Item Dates
- Flow 1 will be a sub-flow of Flow 2;
- A Process which will fire the Flow;
- Workflow to update the number of days the date has moved by.
Create the ‘Days Shifted’ number field with no decimal places on the Opportunity.
Workflow to Populate the field in Step 1. This workflow will fire whenever the Close Date changes.
The action for the Workflow sets the value for the ‘Days Shifted’ field, remember to tick the ‘Re-evaluate Workflow Rules after Field Change’.
Before you go to Step 3, if you’re new to Flow then you need to read my previous Blog and also check out the Salesforce guide here.
Step 3 Flow 1 – Update Line Schedules
This flow is the sub-flow in Flow 2. So, don’t worry if you have some loose ends flying around!
This is going to find all the Product Schedule records that are associated to an Opportunity Line record and then it will update all the dates on the records.
Here is an overview of the Process
1. Flow Fast Lookup
The Fast Lookup will find all the OpportunityLineItemSchedule records that’s Opportunity LineItem ID is the same as the svarOpportunityLineItem.Id. – we will use the svarOpportunityLineItem.Id in the other flow.
All records that meet the criteria will be put into a collection (scvLineSchedules) and the ScheduleDate of the records is stored along with the ID’s in the Collection.
1. The Loop
The Loop cycles through all the records in the scvLineSchedules collection and assigns the next record to the SObject variable svarLineSchedule.
If a connection is found a connection from a loop, it gives two options: “for each value in the collection” and “when there are no more values to process”.
3. Assigning the new Date to the Record
Once the Loop has added a single record to svarLineSchedule, now it needs to assign the new date value to that record. The formula for that was set up in the flaNewScheduleDate.
4. Add the updated record to a new Collection
Now we have the new date in our variable we need to add it to a new collection within the Flow.
In this assignment the operator is Add.
The flow will now loop around and repeat the process until all the records are processed.
5. Fast Update Salesforce
Now Salesforce will be updated with the values from the new Collection – scvNewLineSchedules.
Save the Flow.
Flow 2 – Updating Opportunity Line Item Dates
While the first Flow updates the dates on all the Product Schedule Records. This flow loops through the Opportunity Line Items related to the Opportunity and calls Flow 1 as a sub-flow.
- Looks up the Opportunity and populates a new variable
- Finds all the Opportunity Line Item records relating to the Opportunity and assigns them to an sObject Collection variable.
- Assigns the first record in the Collection to a sObject Variable and passes that record to…
- Flow 1
- Assigns the new Date value to the Opportunity Line Item record.
- Adds that record to another sObject Collection Variable
- All the records get updated using the new Collection’s values.
- Sets the custom Field ‘Days Shifted’ back to 0
There are a few similarities between the two flows. The main differences are Steps 1, 4 and 8. This time it is looping through Opportunity Line Items and not Product Schedule records.
Variables and Formulas needed for this flow are below, remember we probably has some of these already floating around at a loose end!
1. Lookup Opportunity
Looks up Opportunity that has had its closed date changed. It uses the value in ‘Days Shifted’ to populate the variable in the Flow.
Steps 2 and 3 are more or less identical to the first flow.
4. Calling Flow 1
Now we’re going to call Flow 1 but we need to let Salesforce know which variables it’s setting in the target flow and what values to set.
Follow Flow 1 for Steps 5, 6, and 7.
8. Resetting the ‘Days Shifted’ to Zero
Simple Record update to reset the value to 0.
Make sure you activate all the Flow and Workflows. Have Fun.
In the next Blog we will take this a little further....