Let’s assume that your Business Science team has created a great Einstein Discovery model and has deployed it for consumption. As an admin, you are responsible for utilizing this model in your Salesforce instance. In this blog, we are going to go over a specific use case discussing embedding Machine Learning generated predictions inside validation rules.
Currently, you can consume Einstein Discovery generated predictions in many ways, so it’s always worth checking out our documentation to make sure you take advantage of all that Einstein Discovery has to offer . Stroll over to our ever-growing list of prediction channels to find the ones that work best for you!
Let’s focus on a specific use case utilizing predictions to solve the following question:
How can I embed machine learning models to enforce company policies across my application data?
Suppose your company’s sales leader Sarah offers the following guidance to her team: “If an opportunity is likely to close, avoid giving discounts over 20%. Basically, do not over-discount our product if not necessary!”
This discount guidance is currently provided by the Einstein Prediction Component on the opportunity record page. This component not only provides the predictions, but also actionable recommendations to improve the predictions. When a salesperson views the record they know how to adjust their offers based on that prediction.
Suppose a salesperson opens an opportunity they are working on. They look at the prediction component and see that the opportunity is likely to close soon. In line with company policy, they decide to offer a 10% discount. But when they edit the record, they accidentally hit the “4” key instead of the “1” since they are right next to each other on the keypad. They inadvertently made an offer of 40%, which goes against the company’s guidance! Invalid data has just entered your application and may have some unintended consequences for your business.
How do you avoid this?
A popular and powerful Salesforce tool that prevents bad data from being saved to your application is validation rules. Validation rules are custom formulas that run on each record during insert or update operations. An evaluation result of true indicates the record contains invalid data, and the record update will be rejected. Further, a helpful error message is shown to explain why the record was rejected.
Let’s go ahead and implement validation rules to solve our scenario with a few easy steps. One thing to point out before we go further is that validation rules will absolutely reject data based on the outcome of the formula . Because the validation rule is based on a predictive model (and no predictive model is perfect), we recommend including a way for the user to override the restriction and/or submit an exception for approval.
Einstein Discovery and the PREDICT Formula Function
We will implement the validation rule using an Einstein Discovery model and the PREDICT formula function. Note that the PREDICT formula function is not only available in validation rules but other Salesforce process automation tools as well.
Einstein Discovery is a unique tool that lets users explore, understand, and create actionable predictions on their data. Starting with a dataset, users can create a story to understand their data using statistical analysis combined with detailed explanations. Further, a comprehensive predictive analysis builds a machine learning model to go with the story. You can easily deploy this model to enhance your Salesforce or external applications with AI-powered predictions. As mentioned in the intro, this post covers just one of many ways to use Einstein predictions.
Let’s write the Validation Rule
As an admin, it’s up to you to decide where to consume predictions made by that model in ways that make sense to your unique business. Here are the key pieces of information that you’ll have at your disposal.
- You have an Einstein Discovery prediction definition that predicts the likelihood to close. It’s been deployed to the Opportunity object and the fields are already mapped. Let’s assume the prediction name is
Likelihood_to_closeand it generates a prediction between 0 and 1. You can find the prediction definition name on the settings tab in Model Manager.
- You have a Discount field on the opportunity that has values between 0 and 1.
- For usage in the formula, the record
Idfield will be required.
- For this specific use case, we recommend you add a new custom text field on the Opportunity object called “Discount Override Explanation”.
A first attempt using the PREDICT function might look like the following. We want to compare the likelihood to close with 80% and the discount to 20%
AND( PREDICT($SmartDataDiscovery.PredictionDefinitions.Likelihood_to_close.Id, Id) > 80, Discount__c > 0.2 )
The first thing to notice is the PREDICT function. Einstein can make a prediction right in a formula! Absolutely no code needed. The first argument is a reference to the prediction definition you’d like to use. The field is long, but don’t worry, use the field picker in the formula editor to help you construct it. The second argument (
Id) tells Einstein on which record to make the prediction.
Translated to text, this validation rule says: “if the prediction is greater than 80 AND the discount is greater than 0.2, reject this record!”
Before we bring out the champagne, remember there’s one more step to add: Let the human override this validation. To do this, add one more statement to the AND function:
AND( PREDICT($SmartDataDiscovery.PredictionDefinitions.Likelihood_to_close.Id, Id) > 80, Discount__c > 0.2, ISBLANK(Discount_Override_Explanation__c) )
Now, the previous statements have to be true AND the discount override explanation has to be blank. Remember: if the formula evaluates to true, the record is invalid. If any one of the statements supplied to the AND function is not true, the record is considered valid.
This new rule means that a user can simply add text in the Discount Override Explanation such as “Einstein predicts 90% but after speaking with the customer they have a hard budget limit this year”. Now they can save the record with a higher discount than what was advised by the sales team.
That’s all there is to it!
Tips for using Einstein Discovery in Validation Rules
- If your business analyst deploys a new model to the prediction definition, the score could change! This means that a record could become invalid without having touched the data! One way to avoid this is to add one more statement to the
ISCHANGED(Discount__c). This makes the validation rule reject a record only if the discount field is changed.
- Predictions made with validation rules count towards your prediction limits: Einstein Discovery Limits
- Predictions can increase processing time. If you are nearing Apex CPU limits you may need to design an asynchronous approach. Check out our prediction service APIs!
- Validation rules run even when managing data through an API. Make sure you consider all use cases!
- You can also make predictions on raw data, or even use prediction definitions that aren’t associated with a record! Check out the documentation.
Your analysts have already done the work of building and deploying predictive models. I hope this example has inspired you to brainstorm on the many ways you can reap the benefits of that work in various other places within Salesforce. Admins are clever and always surprise me with their implementations, so I know that this is only the beginning! So if you have any feedback on this functionality make sure to leave a comment below.
 I recommend these great blogs about other things you can do with Einstein Discovery for further information:
- Einstein Discovery in Tableau – Try it for Yourself
- Complete Guide to Einstein Discovery Model Deployments
- Create Intelligent Applications with the ‘Predict’ node in Recipes
- Supercharge your Automated Business Processes with Einstein Discovery
- Einstein Discovery Prediction Service on Postman — 3 Easy Steps
 If you just want to provide warnings or helpful insights, a great tool might be Dynamic Lightning Pages. You can add a rich text component containing an alert or warning message to your lightning page that only shows up when certain criteria are true. Although you can’t use the PREDICT function in a visibility rule, you can use an automated prediction field.