You are here:
Get Predictions in Apex
Use the ConnectApi.SmartDataDiscovery.predict
API to get predictions in your Apex code.
You can programmatically run predictions in Apex using the ConnectApi.SmartDataDiscovery.predict method. There are two
types of input:
- record IDs
- record IDs plus calculated fields that are not stored in the record
This function accepts an input of up to 200 record IDs or raw data rows, along with a
prediction definition ID. It returns up to 200 predictions of type ConnectApi.SmartDataDiscoveryPrediction.
For more information about Apex code, see the Apex Developer Guide.
Permissions Required to View Predictions and Improvements
To view predictions, improvements, and related details, users must have the View Einstein Discovery Recommendations permission assigned to their Salesforce account. To learn more, see Assign Einstein Discovery Permission Sets to Users.
API Usage Limits
ConnectApi.SmartDataDiscovery.predictcan be called up to 2,000 times per user per hour.- Each
ConnectApi.SmartDataDiscovery.predictcan handle up to 200 records in a single call. ConnectApi.SmartDataDiscovery.predictcan be called no more 50,000 times per day (24 hours) per org.
Batch Apex and Callouts for Models Using Supplemental Datasets
For batch Apex, if you are using a model associated with a supplemental dataset (see Map Model Variables), be sure to follow the requirements for callouts described in Callout Limits and Limitations in the Apex documentation.
Get Predictions Using the Record ID as Input
Syntax
public ConnectApi.SmartDataDiscoveryPrediction ConnectApi.SmartDataDiscovery.predict(ConnectApi.SmartDataDiscoveryPredictInputRecords input)SmartDataDiscoveryPredictInputRecords class
| Name | Type | Description |
|---|---|---|
predictionDefinition
|
String | 18-digit prediction definition Id. |
records
|
List<Id> | List of records to be scored. These record Ids must be from the same object type associated with the deployed prediction definition. |
settings
|
ConnectApi.SmartDataDiscoveryDiscoveryPredictSettings | Set the number of top predictors, the number of improvements, and the improvement impact threshold to return in the response. |
Get Predictions Using the Record ID and Calculated Fields as Input
Syntax
public ConnectApi.SmartDataDiscoveryPrediction ConnectApi.SmartDataDiscovery.predict(SmartDataDiscoveryPredictInputRecordOverrides input)SmartDataDiscoveryPredictInputRecordOverrides class
| Name | Type | Description |
|---|---|---|
predictionDefinition
|
String | 18-digit prediction definition Id. |
rows
|
List<ConnectApi.SmartDataDiscoveryPredictInputRowObject> | Ordered List of row details, including the record Id and values for columns that are not mapped from Salesforce to the prediction definition. |
columnNames
|
List<String> | Ordered List of column names that are not mapped. |
settings
|
ConnectApi.SmartDataDiscoveryDiscoveryPredictSettings | Set the number of top predictors, the number of improvements, and the improvement impact threshold to return in the response. |
ConnectApi.SmartDataDiscoveryPredictInputRowObject class
| Name | Type | Description |
|---|---|---|
record
|
String | 18-digit Id for the Salesforce record that gets the score. The record Id must match the object type associated with the deployed prediction definition. |
row
|
List<String> | Ordered List of the values that accompany the ordered list of columnNames. |
ConnectApi.SmartDataDiscoveryDiscoveryPredictSettings class
| Name | Type | Description |
|---|---|---|
maxMiddleValues
|
Integer | Maximum number of top predictors (0-3) to return in the response. The terms “top factor” and “top predictor” mean the same thing. Default: 0 (no top predictors returned). |
maxPrescriptions
|
Integer | Maximum number of improvements (0-200) to return in the response. The terms “improvement” and “prescription” mean the same thing. Default: -1 (unlimited). |
prescriptionImpactPercentage
|
Integer | Improvement impact threshold, specified as a percentage (0 to 100). If set to 20, for example, only improvements that affect the predicted outcome more than 20% are returned. Default: 0. |
Output Classes
ConnectApi.SmartDataDiscoveryPrediction class
| Name | Type | Description |
|---|---|---|
predictions
|
List<ConnectApi.SmartDataDiscoveryPredictObject or List<ConnectApi.SmartDataDiscoveryPredictErrorObject | List of predictions or exceptions returned from the predict method. |
ConnectApi.SmartDataDiscoveryPredictObject class
| Name | Type | Description |
|---|---|---|
prediction
|
ConnectApi.SmartDataDiscoveryPredict
|
Object containing all the prediction data. |
prescriptions
|
List<ConnectApi.SmartDataDiscoveryPredictCondition>() | List of improvements representing suggestions to improve the prediction. This field is blank if no action variables were selected when deploying the prediction. |
ConnectApi.SmartDataDiscoveryPredict class
| Name | Type | Description |
|---|---|---|
total
|
double | Final prediction value. |
other
|
double | Unexplainable portion of the final prediction value. |
baseLine
|
double | Baseline from where the prediction started. Basically the average if there were no model. |
middleValues
|
List<ConnectApi.SmartDataDiscoveryPredictCondition>() | List of top predictors that contribute to the prediction. |
importWarnings
|
List<Connectapi.SmartDataDiscoveryPredictImportWarnings>() | List of warnings associated with the prediction. If there are no warnings, this field returns null. |
ConnectApi.SmartDataDiscoveryPredictCondition class
| Name | Type | Description |
|---|---|---|
value
|
double | Value that this field / value combination contributes to the final prediction. |
columns
|
List<ConnectApi.SmartDataDiscoveryPredictColumn> | Column information. |
ConnectApi.SmartDataDiscoveryPredictColumn class
| Name | Type | Description |
|---|---|---|
columnName
|
String | Name of the column. Example: Account Name |
columnValue
|
String | Value of the column. Example: Acme |
Connectapi.SmartDataDiscoveryPredictImportWarnings class
| Name | Type | Description |
|---|---|---|
outOfBoundsColumns
|
List<ConnectAPI.SmartDataDiscoveryPredictOutOfBoundsFields> | Columns + values that are out of bounds. These values were not used when training the model. |
missingColumns
|
List<String> | List of column names that are required to make a prediction but are missing. Perhaps they are null? |
Connectapi.SmartDataDiscoveryPredictOutOfBoundsFields class
| Name | Type | Description |
|---|---|---|
columnName
|
String | Name of the column that is out of bounds. |
columnValue
|
String | Value of the column that is out of bounds, meaning that the model wasn’t
trained on this particular value. For example, the model was trained on Type =
"Manufacturing" but received a value of
"Manufracturing". |
Exceptions
If there are rows that caused the prediction method to fail, this call returns an exception rather than a prediction.
ConnectApi.SmartDataDiscoveryPredictErrorObject class
| Name | Type | Description |
|---|---|---|
message
|
String | Error message. |
Code Example
This code example is designed for building test cases.
// Initialize the Prediction API Input Record
ConnectApi.SmartDataDiscoveryPredictInputRecords predictInput = new ConnectApi.SmartDataDiscoveryPredictInputRecords();
// Set Prediction Definition
predictInput.predictionDefinition = '1ORB0000000TN1SOAW';
// Set Records
predictInput.records = new List<Id>{'006B0000004ApAZIA0'};
// Set Settings for Prediction
ConnectApi.SmartDataDiscoveryDiscoveryPredictSettings settings = new ConnectApi.SmartDataDiscoveryDiscoveryPredictSettings();
settings.maxMiddleValues = 3;//[0-3] limit of 3
settings.maxPrescriptions = 3;
settings.prescriptionImpactPercentage = 20;
predictInput.settings = settings;
////////////////////////////////////////////////////////////////
// Generate Predictions
ConnectApi.SmartDataDiscoveryPrediction response = ConnectApi.SmartDataDiscovery.predict(predictInput);
// Get Prediction
ConnectApi.SmartDataDiscoveryPredictObject prediction = (ConnectApi.SmartDataDiscoveryPredictObject) response.predictions[0];
// Get Prescriptions (Improvements)
List<ConnectApi.SmartDataDiscoveryPredictCondition> prescriptions = prediction.prescriptions;
// Iterate over the first prescription
ConnectApi.SmartDataDiscoveryPredictCondition first_prescription = prescriptions[0];
Double value = first_prescription.value;
List<ConnectApi.SmartDataDiscoveryPredictColumn> columns = first_prescription.columns;
// Prescriptions usually have one-column information:
ConnectApi.SmartDataDiscoveryPredictColumn column_1 = columns[0];
String columnName = column_1.columnName;
String columnValue = column_1.columnValue;
String columnInputVal = column_1.inputValue;
////////////////////////////////////////////////////////////////
// Get Top Factors (Top Predictors)
List<ConnectApi.SmartDataDiscoveryPredictCondition> topFactors = prediction.prediction.middleValues;
// Iterate over the first TopFactor
ConnectApi.SmartDataDiscoveryPredictCondition first_topFactor = topFactors[0];
Double tf_value = first_topFactor.value;
List<ConnectApi.SmartDataDiscoveryPredictColumn> tf_columns = first_topFactor.columns;
// Top Factors have multiple Columns, so it is recommended to iterate over the list
ConnectApi.SmartDataDiscoveryPredictColumn tf_column_1 = tf_columns[0];
String tf_columnName = tf_column_1.columnName;
String tf_columnValue = tf_column_1.columnValue;
String tf_columnInputVal = tf_column_1.inputValue;
////////////////////////////////////////////////////////////////
// Get Import Warnings
ConnectApi.SmartDataDiscoveryPredictImportWarnings importWarnings = prediction.prediction.importWarnings;
// System Debug
System.debug('Total ' + prediction.prediction.total);
System.debug('Import Warnings ' +importWarnings);
