Implement custom prediction model
The functionality is relevant to Classic UI.
To implement the example:
- Add an ML model. Read more >>>
- Train the model. Read more >>>
- Implement prediction business logic. Read more >>>
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
-
Open the Configuration section. Instructions: Open the Configuration section.
-
Create a
sdkCustomPredictionModel
package. Instructions: Create a user-made package using Configuration section. -
Change the current package to
sdkCustomPredictionModel
. Instructions: Change the current package. -
Click
in the top right → System setup → Lookups.
-
Open the ML model (
MLModel
code) lookup. -
Click New → Lookup prediction to add the lookup value.
-
Fill out the properties of the lookup value.
Property
Property value
Name
Predict account category
Type
Lookup prediction
Object
Category of Account
-
Click Next. This opens the Predict account category page.
-
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"
}
} -
Click Save.
-
Click Close. This opens the page of the ML model (
MLModel
code) lookup.
2. Train the model
-
Select the Predict account category.
-
Click Actions → Execute 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
-
Open the Configuration section. Instructions: Open the Configuration section.
-
Select the
sdkCustomPredictionModel
package. -
Click Add → Business process.
-
Open the Settings tab.
-
Fill out the schema properties.
Property
Property value
Title
Predict account category
Code
UsrPredictAccountCategory
-
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
-
Click an arbitrary place in the working area of the Process Designer.
-
Open the Parameters tab.
-
Add the business process parameter.
-
Click Add parameter → Lookup.
-
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 value → Predict account category → Select. 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
-
-
Click Save.
3. Set up running the business process
-
Delete the Simple page element.
-
Click
.
-
Place the Throw signal element in the working area.
-
Click
→ connect the elements.
-
Click
→ Wait for signal.
-
Fill out the element properties.
Property
Property value
Title
Run the process
-
Fill out the element properties.
Property
Property value
Which type of signal is received?
Record signal
Record Id
Click
→ Process parameter → Process parameters tab → Account ID → Select.
Which event should trigger the signal?
Record added
The added record must meet filter conditions
- Click Add condition. This opens the Select column window.
- Select "Country" in the Column property.
- Click Select.
- Click
→ "is filled in."
- Repeat steps 1–4 for both No. of employees and Industry columns.
-
Click Save.
4. Implement populating the Category account field
-
Add a Script task element. To do this, click
→ place the Script task element between the Throw signal and Terminate page elements.
-
Fill out the element properties.
Property
Property value
Title
Call the automatic prediction
-
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; -
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
// 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;