How to Build Milestone Process for Custom Objects





I have seen several request to implement Milestones Process, which is limited to Cases/Work Orders, on other objects.
For instance, you might want to define milestones for the lead object. In the first milestone, the lead should entered into status in progress, and in the next milestone it should be either converted or disqualified.

I will show in this article some of the fundamentals for such process - setting milestones for any object (standard/custom) and working with them. 
The complete process contains some complex features that might not be relevant for your case.

1.Data modal
Need 5 custom objects:
Milestone Process
Milestone
Milestone Actions
Milestone Record
Milestone Action Record
First 3 are for the setup - you want to define the header milestone process, below 1 or more milestones and each milestone might have 1 or more actions.
The last 2 are instances of the Milestone/Milestone Action which related to specific record. When the process will find that specific record should enter into the milestone process, it will generate the Milestone/Action for the record.






2.Setup

Under the milestone process you will most likely need name, description, related object, active flag and optional criteria to limit the milestone process only for specific records.

Few notes:
  • For related object can dynamically get all the objects in the org using the schema in apex or it can be done with Javascript, but in any way getting all standard objects might result in very long list that most of them are not relevant. This can be solved by custom metadata that define which custom objects should be displayed.

  • Active flag is very important piece,  as you don't want the process will be used before the setup complete. The setup can be complete only after defining the milestones/actions.

  • Filter - you might want to run the milestone process only for specific records. For that I used complex component that allowed to construct complex criteria logic. If it is too complex you can simply write there SOQL where criteria (for instance Rating = 'Hot'), main issue is that it is not user friendly and require technical knowledge. 



In the image you can see additional input, which support additional functionality:
  • Version - allow to manage version for each milestone process
  • Milestone Order - relation between milestones. For example, what should happened if the second milestone completed before the first.
  • Business Hours - reference to business hour setup. This will make the process count the milestone/action timeline only within the business hours.

Next, setup the Milestones
Each milestone should have name, sequence, target time for completion and target criteria.
The target time define when the milestone should be completed, and the target criteria define the criteria. For the criteria, again I used the same component which I used to define criteria in the milestone process, it can be simplified by using string value. It can save efforts but make the tool less user oriented.


After adding 2 milestones the milestone process look like this



Next, under each milestone we might want to setup actions to run. Each action can run either before/after violation or it can run when the milestone complete.
Therefore each Action have Type either Violation or Completion and Action.
In my case I implemented 4 action type:
Email Alert
Update Record
Lock Record
Unlock Record

The Lock/Unlock are special implementation therefore can ignore them in this article. In the alert you should get setup for the subject, email content, notify. For Update Record action, you will need to save pair of field-value for the related object that will be used to update.



After completing the setup Milestone Process, Milestone, Milestone Action, you should go and activate the main Milestone Process. 


3.Transaction process.
You should implement several processes with apex. Some of them can be done in trigger and other should be done with apex scheduler.
Should have at least trigger on every object that you want to setup milestone process.
In this trigger you will to do 3 things:
  • When record created - check it apply to any related milestone process. If it does, generate the milestones records and the milestone actions according to the setup.
  • When record updated - check if any of the related milestone was completed according to the milestone target criteria, if it does close it and run any related actions of type Completion.
  • When record deleted - simply delete its related milestone/action records (if exists).

Note that during the update trigger you will need to evaluated the the Milestone Target against the records. First check if any relevant field was changed and if so check if the record apply to the criteria. 
Obviously, it will be inefficiency to query the milestone setup and run this evaluation on every change, therefore you might want to store the relevant fields from the target criteria in setup (e.g. custom metadata). In the trigger you will first check if any of the fields from the setup was change, and only if it does run the complex logic.

4.Apex Scheduler
This process will run on specific interval and will check 2 things:
  • If any milestone date was violated (due date > now), then it should update their status to Violated and cancel the relevant actions.
  • If any action of type Violation date is arrived then it should run this action
You might also want to add screen for scheduling/aborting the job or view its logs, but it is not mandatory.

5.Record layout component
Probably want to view in each record, its related milestones.
In Salesforce classic you might had technical problems to implement such, because you must related visualforce page to specific object in order to have it in the page layout, but in Salesforce Lightning you can easily write component (Aura or LWC) that can be used in any layout.





Also note that in my case I added button in the component Recreate Milestones. This will recreate the milestone/action for the related record. It can be used for exceptional cases where you need to run the process on old records. Anyway, if needed suggest to show this button only with special permission, as the process cannot revert.



Finally - quick demo that shows my implementation















Retire of Permission on Profiles

If you are working as a Salesforce admin/developer you've probably heard somewhere that Salesforce is planning to make a significant cha...