Restocker_Opportunity_Builder__mdt
What is a Restocker Opportunity Builder
Different finance platforms, and different customers, have different requirements for how Opportunities are formed prior to syncing.
To address this problem we have created 'Opportunity Builders'.
An Opportunity Builder is a mix of Apex and Custom Metadata used to control how Opportunities and Opportunity Line Items are created in the Restocker App.
At launch there are two Opportunity Builders: Detailed and Asset Summary.
The Detailed Opportunity Builder works in the same way as other Opportunities on the platform.
The Work Order Line Items from all Work Orders related to a Service are tallied together and added to the Opportunity.
For example if Asset A had 1 bandage and Asset B had 2 bandages, the resultant Opportunity would have one Line Item for the bandage Product with a Quantity of 3.
The Asset Summary Opportunity Builder is designed to represent each Asset as its own line and does not show the individual Line Items.
At launch, this Opportunity Builder cannot be used as it relies on the MYOB integration understanding Line Item Descriptions.
It also has the downside of disabling the ability to track inventory usage inside of MYOB.
The Asset Summary Opportunity Builder uses the 'Asset Summary Product' from the Restocker_Default_Settings__mdt to create one Line Item per Asset on a Service with a Quantity of 1 and a Price matching the total for that Asset. (Technically, the Work Order for the Asset)
The Builder Framework
The Opportunity Builder Framework consists of two parts: an Apex Class and a Restocker_Opportunity_Builder__mdt record.
The Apex Class controls the behaviour of the builder while the Metadata Record determines who can access the Builder and informs the Restocker App of the Builder's existence.
Apex Classes
The core of the Builder Framework is the ServiceOpportunityBuilder abstract Class.
This abstract Class is extended by the individual Opportunity Builder's and contains core methods and properties and shared functionality for the Builders.
The main methods in the abstract Class are:
auraCall - The method called by the LWC to execute the Opportunity Builder
call - The method executed by the auraCall method
build - A virtual method used to run both the createOpportunity and then the createOpportunityLineItems methods.
This virtual method may be overridden by the Builder instance.
createOpportunity - A virtual method used to create the Opportunity record.
This virtual method may be overridden by the Builder instance, but can be used as is.
createOpportunityLineItems - An abstract method used to create the Opportunity Line Items.
This abstract method must be implemented by the Builder instance.
saveOpportunity - A helper method that may be used by the Builder instance.
saveOpportunityLineItems - A helper method that may be used by the Builder instance.
Metadata
The Restocker_Opportunity_Builder__mdt Custom Metadata is a straightforward record that announces a Builder class to the Restocker App and grants access to a region.
It has a Class Api Name field which contains the DeveloperName of the ServiceOpportunityBuilder Apex Class.
There are also several checkbox fields named after each region.
To grant access to a Builder for a region, check the box for that region.
Creating a New Builder
Creating a new Opportunity Builder begins with making an implementation of the ServiceOpportunityBuilder class and then creating the Restocker_Opportunity_Builder__mdt record.
Creating the New Builder's Class
An Opportunity Builder Apex class is an implementation of the ServiceOpportunityBuilder class.
First create your Apex Class as a public class that extends ServiceOpportunityBuilder.
Convention is to Name your Apex Class as 'ServiceOpportunityBuilder_{NAME}' e.g. ('ServiceOpportunityBuilder_Detailed').
Your Opportunity Builder must override the createOpportunityLineItems method of the ServiceOpportunityBuilder.
Once you have created your class, ensure you have also created a test class that covers your implemented methods.
Creating the New Builder's Metadata
After creating the Apex Class you must create a Restocker_Opportunity_Builder__mdt record.
Create a new record and for the Class_API_Name__c put the Name of the Apex Class (e.g. 'ServiceOpportunityBuilder_Detailed').
Grant access to any regions that need it by ticking the checkbox for that region.