---
schema_version: "1"
id: 09cc9725f8270c638340a42094df83913c930d98c0e87e6ad92331099a9f40a8
title: Get Predictions in Apex
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/analytics/264-0-0/bi_edd_prediction_apex.md
canonical_url: https://help.salesforce.com/s/articleView?id=analytics.bi_edd_prediction_apex.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 67638b6779f72eadd9ca14323c44c5419a5f7599a7e1760dac79ec927ca9c2c7
description: Use the ConnectApi.SmartDataDiscovery.predict API to get predictions in your Apex code.
products:
  - CRM Analytics
  - Cross Cloud
release: "264"
---
# 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`.

![Note](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-analytics-bi-264-0-0-production-enus/ec791473-f5b4-4f64-a656-9104f455d966/images/icon_note.png)

Note Before you use the Apex Predict API, deploy the model you want to use for predictions. For instructions, see [Deploy Models](https://help.salesforce.com/s/articleView?id=analytics.bi_edd_model_deploy.htm&release=264.0.0&type=5 "Deploy a model so that you can use it to make predictions and improvements.").

For more information about Apex code, see the [Apex Developer Guide.](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dev_guide.htm)

## 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](https://help.salesforce.com/s/articleView?id=analytics.bi_edd_setup_assign_permsets.htm&release=264.0.0&type=5 "After you have defined permission sets for Einstein Discovery users, you can assign the appropriate permission sets to individual users or groups of users. You can assign multiple permission sets to an individual user account.").

## API Usage Limits

- `ConnectApi.SmartDataDiscovery.predict` can be called up to 2,000 times per user per hour.
- Each `ConnectApi.SmartDataDiscovery.predict` can handle up to 200 records in a single call.
- `ConnectApi.SmartDataDiscovery.predict` can 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](https://help.salesforce.com/s/articleView?id=analytics.bi_edd_model_deploy_map.htm&release=264.0.0&type=5 "Define the mapping between variables in the model to fields in the Salesforce object or to columns in the supplemental dataset.")), be sure to follow the requirements for callouts described in [Callout Limits and Limitations](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_timeouts.htm) 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.

```java
// 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);
```