---
schema_version: "1"
id: 7f20e4b29cf0b957af07e3f3eb6d38f7bf740ada24e6f5fc5580828547cfc988
title: Get Predictions in Apex
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/data/264-0-0/c360_a_ai_apex.md
canonical_url: https://help.salesforce.com/s/articleView?id=data.c360_a_ai_apex.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: d4fb82f58e46defed3b04517d436d57a6f892659daa4bcaa2f1f70189f3337e1
description: Use Apex code to get predictions, top factors, and prescriptions from a no-code model in Model Builder.
products:
  - Data 360
release: "264"
---
# Get Predictions in Apex

Use Apex code to get predictions, top factors, and prescriptions from a no-code model in Model Builder.

### Required Editions

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition"><colgroup><col></colgroup><tbody><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in: <strong>All Editions</strong> supported by Data 360. See <a href="https://help.salesforce.com/s/articleView?id=data.c360_a_dc_editions.htm&amp;language=en_US" target="_blank">Data 360 edition availability</a>.</td></tr></tbody></table>

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small permission">
        <colgroup>
          <col style="width:50%">
          <col style="width:50%">
        </colgroup>
        <thead class="slds-text-title_caps ">
          <tr class="">
            <th scope="colgroup" class="slds-cell-wrap" colspan="2" style="text-align:center;vertical-align:top;" id="d69684e59">USER PERMISSIONS
              NEEDED</th>
          </tr>
        </thead>
        <tbody>
          <tr class="">
            <td data-label="USER PERMISSIONS NEEDED" class="slds-cell-wrap" style="vertical-align:top;" headers="d69684e59 ">Allow users to manage models in AI Models</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d69684e59 ">Enables you to create, update, and delete models in AI Models.</td>

          </tr>
        </tbody>
      </table> <table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small permission">
        <colgroup>
          <col style="width:50%">
          <col style="width:50%">
        </colgroup>
        <thead class="slds-text-title_caps ">
          <tr class="">
            <th scope="colgroup" class="slds-cell-wrap" colspan="2" style="text-align:center;vertical-align:top;" id="d69684e89">PERMISSION SETS </th>
          </tr>
        </thead>
        <tbody>
          <tr class="">
            <td data-label="PERMISSION SETS" class="slds-cell-wrap" style="vertical-align:top;" headers="d69684e89 ">Data Cloud Architect</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d69684e89 ">Admin-level access to all AI Models features, including the ability to create,
              update, delete, and activate models.</td>

          </tr>
          <tr class="">
            <td data-label="PERMISSION SETS" class="slds-cell-wrap" style="vertical-align:top;" headers="d69684e89 ">Data Cloud User</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d69684e89 ">Restricted access to use a model, including getting predictions and
              improvements derived from a model.</td>

          </tr>
        </tbody>
      </table>

## Invocable Action

To use a predictive model with Apex, create an invocable action with this method. The method creates an invocable action that provides input data to a predictive model. It triggers the model request and processes the returned output, including predictions, top factors, and prescriptions (recommended actions).

`Invocable.Action.createCustomAction`

## REST Request

As a model is activated, an invocable action is automatically created and it can be used in Flow or Apex. When using this invocable action in Apex, you may need to get the input parameters using this REST Endpoint.

`GET /services/data/v63.0/actions/custom/cdpMlPrediction/{ModelIdOrName}`

![Note](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-data-data_cloud-264-0-0-production-enus/f3569962-3c9b-4f83-9d0c-b6a574e785da/images/icon_note.png)

Note You can use the predictive model name (this example) or model id (API Name). Some APIs also require the model API name “{ModelIdOrName}”. You can find a model's API name in Model Builder, on the model details page.

![The model detail page displays the model API name.](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-data-data_cloud-264-0-0-production-enus/f3569962-3c9b-4f83-9d0c-b6a574e785da/data_cloud/images/c360_a_ai_ec_model_details_api_name.png)

Let's look at how to use a REST request for a 'Predicted Purchased' model.

`GET /services/data/v63.0/actions/custom/cdpMlPrediction/Predicted_Purchased_CM_12l_YSs01128465`

The request retrieves metadata about the invocable action such as the model input parameters.

`{ "inputs" : [  {     "label" : "Top Predictors",     "name" : "maxTopFactors",     "type" : "INTEGER"   },  {     "label" : "Top Prescriptions",     "name" :  "maxPrescriptions",     "type" : "INTEGER"   }, {     "label" : "Lead Source",     "name" : "param_Lead_Source_c__c",     "type" : "STRING"   },  {     "label" : "Industry",     "name" : "param_Industry_c__c",     "type" : "STRING"   },  {     "label" : "Title",     "name" : "param_Title_c__c",     "type" : "STRING"   },  {     "label" : "Response Time From Assignment",     "name" : "param_Response_Time_From_Assignment_c__c",     "type" : "DOUBLE"   },  {     "label" : "No. of Employees",     "name" : "param_No_of_Employees_c__c",     "type" : "DOUBLE"   },  {     "label" : "Response Time from Creation",     "name" : "param_Response_Time_from_Creation_c__c",     "type" : "DOUBLE"   } ] }`

## Apex for Predictions

You can then use Apex code to invoke the 'Predicted Purchased' model to provide predictions, top factors, and prescriptions.

`Invocable.Action action = Invocable.Action.createCustomAction('cdpMlPrediction', 'Predicted_Purchased_CM_12l_YSs01128465');         action.setInvocationParameter('param_Industry_c__c', 'High Tech');         action.setInvocationParameter('param_No_of_Employees_c__c', 100);         action.setInvocationParameter('param_Lead_Source_c__c', 'Chat');         action.setInvocationParameter('param_Response_Time_from_Creation_c__c', 100);         action.setInvocationParameter('param_Response_Time_From_Assignment_c__c', 300);         action.setInvocationParameter('maxTopFactors', 3);         action.setInvocationParameter('maxPrescriptions', 3);         List<Invocable.Action.Result> results = action.invoke();         if (results.size() > 0 && results[0].isSuccess()) {         System.debug('Prediction =' + results[0].getOutputParameters().get('prediction')); //output will be in the same data type as the model output         System.debug('Top Factors =' + results[0].getOutputParameters().get('factors')); //output is a custom Apex Type ConnectAPI.CdpMlPredictionContribution         System.debug('Prescriptions =' + results[0].getOutputParameters().get('prescriptions')); //output is a custom Apex Type ConnectAPI.CdpMlPredictionContribution         }`

#### See Also

- [_Salesforce Developer:_ Apex Developer Guide](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dev_guide.htm)