Skip to main content
Version: 8.3

Implement custom prediction model

Level: advanced
note

The functionality is relevant to Classic UI.

To implement the example:

  1. Add an ML model. Read more >>>
  2. Train the model. Read more >>>
  3. Implement prediction business logic. Read more >>>
Example

Implement automatic prediction for the Category account field based on the values of the Country, No. of employees and Industry fields while saving the account record. Implement the following prediction conditions:

  • Create a model that learns from account records for last 90 days.
  • Re-train the model every 30 days.
  • Set permissible prediction accuracy for the model to "0.6."

To implement the example, you can also use page fields and filters, as well as the Predict data system action element in the business process. Learn more: Predictive data analysis (user documentation).

1. Add an ML model

  1. Open the Configuration section. Instructions: Open the Configuration section.

  2. Create a sdkCustomPredictionModel package. Instructions: Create a user-made package using Configuration section.

  3. Change the current package to sdkCustomPredictionModel. Instructions: Change the current package.

  4. Click in the top right → System setupLookups.

  5. Open the ML model (MLModel code) lookup.

  6. Click NewLookup prediction to add the lookup value.

  7. Fill out the properties of the lookup value.

    Property

    Property value

    Name

    Predict account category

    Type

    Lookup prediction

    Object

    Category of Account

  8. Click Next. This opens the Predict account category page.

  9. Fill out the parameters of the prediction model.

    Property

    Property value

    Prediction enabled

    Select the checkbox

    Parameters → Automatic model training settings

    Quality metric lower limit

    0.60

    Retrain after, days

    30

    Advanced settings → Advanced tools to add columns

    Query for selecting additional training data

    new Select(userConnection)
    .Column("a", "Id")
    .Column("a", "CountryId")
    .Column("a", "EmployeesNumberId")
    .Column("a", "IndustryId")
    .Column("a", "AccountCategoryId")
    .Column("c", "Name").As("AccountCategory")
    .From("Account").As("a")
    .InnerJoin("AccountCategory").As("c")
    .On("a", "AccountCategoryId").IsEqual("c", "Id")
    .Where("a", "CreatedOn")
    .IsGreater(Func.DateAddDay(-90, Func.CurrentDateTime()))

    Query metadata for selecting additional training data

    {
    "inputs": [
    {
    "name": "CountryId",
    "type": "Lookup",
    "isRequired": true
    },
    {
    "name": "EmployeesNumberId",
    "type": "Lookup",
    "isRequired": true
    },
    {
    "name": "IndustryId",
    "type": "Lookup",
    "isRequired": true
    }
    ],
    "output": {
    "name": "AccountCategoryId",
    "type": "Lookup",
    "displayName": "AccountCategory"
    }
    }
  10. Click Save.

  11. Click Close. This opens the page of the ML model (MLModel code) lookup.

2. Train the model

  1. Select the Predict account category.

  2. Click ActionsExecute model training job.

    Wait until the Status field value is set to "Done." The process might take several hours. It depends on the amount of passed data and general workload of the predictive service.

3. Implement prediction business logic

1. Create a business process

  1. Open the Configuration section. Instructions: Open the Configuration section.

  2. Select the sdkCustomPredictionModel package.

  3. Click AddBusiness process.

  4. Open the Settings tab.

  5. Fill out the schema properties.

    Property

    Property value

    Title

    Predict account category

    Code

    UsrPredictAccountCategory

  6. Click Save.

As a result:

  • The "Predict account category" business process will be created.
  • Creatio will add the "Predict account category" business process to the Process library section.

2. Set up the business process parameters

  1. Click an arbitrary place in the working area of the Process Designer.

  2. Open the Parameters tab.

  3. Add the business process parameter.

    1. Click Add parameterLookup.

    2. Fill out the parameter properties.

      Element

      Element type

      Property

      Property value

      Parameter that contains the model ID

      Lookup

      Title

      ML model ID

      Code

      MLModelID

      Data type

      Lookup

      Lookup

      ML model

      Value

      Click Lookup valuePredict account categorySelect. This populates the property using the "[#Lookup.ML model.Predict account category.99402097-a6ac-4930-9540-6a5cb8d6d9b2#]" value.

      Parameter that contains the account ID

      Lookup

      Title

      Account ID

      Code

      AccountID

      Data type

      Lookup

      Lookup

      Account

  4. Click Save.

3. Set up running the business process

  1. Delete the Simple page element.

  2. Click .

  3. Place the Throw signal element in the working area.

  4. Click → connect the elements.

  5. Click Wait for signal.

  6. Fill out the element properties.

    Property

    Property value

    Title

    Run the process

  7. Fill out the element properties.

    Property

    Property value

    Which type of signal is received?

    Record signal

    Record Id

    Click Process parameterProcess parameters tab → Account IDSelect.

    Which event should trigger the signal?

    Record added

    The added record must meet filter conditions

    1. Click Add condition. This opens the Select column window.
    2. Select "Country" in the Column property.
    3. Click Select.
    4. Click → "is filled in."
    5. Repeat steps 1–4 for both No. of employees and Industry columns.
  8. Click Save.

4. Implement populating the Category account field

  1. Add a Script task element. To do this, click → place the Script task element between the Throw signal and Terminate page elements.

  2. Fill out the element properties.

    Property

    Property value

    Title

    Call the automatic prediction

  3. Implement the logic of working with process parameters. To do this, go to the element setup area and add the source code.

    Call the automatic prediction script task
    // IMPORTANT: When implementing
    // long-running operations,
    // it is crucial to enable timely and
    // responsive cancellation. To achieve
    // this, ensure that your code
    // is designed to respond appropriately
    // to cancellation requests using
    // the context.CancellationToken
    // mechanism. For more detailed
    // information and examples,
    // please, refer to our documentation.

    /* Get an instance of the "UserConnection." */
    var userConnection = Get<UserConnection>("UserConnection");
    /* ID of selected account. */
    Guid entityId = Get<Guid>("RecordId");
    /* ID of selected ML model. */
    var modelId = Get<Guid>("MLModelID");
    var connectionArg = new ConstructorArgument("userConnection", userConnection);
    /* Create an instance of the "MLEntityPredictor." */
    var predictor = ClassFactory.Get<MLEntityPredictor>(connectionArg);
    /* Call the prediction service. Creatio saves data to the "MLPrediction"
    database table. If high probability is predicted, Creatio saves data
    to the required field on the account page. */
    predictor.PredictEntityValueAndSaveResult(modelId, entityId);

    return true;
  4. Click Save.

As a result, the prediction will be performed for new accounts and displayed on the account edit page.

This implementation of the prediction slows down saving an account record because because calling the prediction service is executed in 2 seconds. This can reduce the performance of bulk operations with data saving, like Excel import.

Source code

Call the automatic prediction script task
// IMPORTANT: When implementing
// long-running operations,
// it is crucial to enable timely and
// responsive cancellation. To achieve
// this, ensure that your code
// is designed to respond appropriately
// to cancellation requests using
// the context.CancellationToken
// mechanism. For more detailed
// information and examples,
// please, refer to our documentation.

/* Get an instance of the "UserConnection." */
var userConnection = Get<UserConnection>("UserConnection");
/* ID of selected account. */
Guid entityId = Get<Guid>("RecordId");
/* ID of selected ML model. */
var modelId = Get<Guid>("MLModelID");
var connectionArg = new ConstructorArgument("userConnection", userConnection);
/* Create an instance of the "MLEntityPredictor." */
var predictor = ClassFactory.Get<MLEntityPredictor>(connectionArg);
/* Call the prediction service. Creatio saves data to the "MLPrediction"
database table. If high probability is predicted, Creatio saves data
to the required field on the account page. */
predictor.PredictEntityValueAndSaveResult(modelId, entityId);

return true;