Populate a field using an install script
The example is relevant to Sales Creatio products.
To implement the example:
- Implement an install script. Read more >>>
- Add the install script to the installation steps. Read more >>>
Implement the install script that populates the Delivery type field on the order page as follows:
- Set the field to "Customer pickup" for products whose Product.Category column has the "Hardware" or "Services" value.
- Set the field to "Courier" for products whose Product.Category column has other values.
Populate the Delivery type field for orders that contain a single product.
1. Implement an install script
-
Open the Configuration section. Instructions: Open the Configuration section.
-
Create a user-made package to add the schema. To do this, click → fill out the package properties → Save.
For this example, create the
sdkPopulateDeliveryType
user-made package. -
Create the source code schema. To do this, click Add → Source code.
-
Fill out the schema properties.
For this example, use the schema properties as follows.
Property
Property value
Code
UsrPopulateDeliveryType
Title
PopulateDeliveryType
-
Apply the changes.
-
Implement the business logic of the delivery type population.
- Find the ID of the "Courier" and "Customer pickup" value in the Delivery type lookup. For this example, the "Courier" ID is "50df77d0-7b1f-4dbc-a02d-7b6ebb95dfd0," the "Customer pickup" ID is "ab31305f-7c6d-4158-bd0a-760ac7897755."
- Set "Customer pickup" for products that have the "Hardware" and "Services" category.
- Set "Courier" for products that have other categories.
- Populate the delivery type based on the product category.
UsrPopulateDeliveryTypenamespace Terrasoft.Configuration {
using System;
using System.Collections.Generic;
using Terrasoft.Core;
using Terrasoft.Core.Entities;
public class UsrPopulateDeliveryType: IInstallScriptExecutor {
private static object GetDeliveryIdByName(string deliveryName) {
/* The unique ID of the delivery type. */
switch (deliveryName) {
case "Courier":
return new Guid("50df77d0-7b1f-4dbc-a02d-7b6ebb95dfd0");
case "Customer pickup":
return new Guid("ab31305f-7c6d-4158-bd0a-760ac7897755");
default:
return Guid.Empty;
}
}
private static object GetDeliveryTypeByProductCategory(string category) {
/* The category of the product. */
switch (category) {
case "Hardware":
return GetDeliveryIdByName("Customer pickup");
case "Services":
return GetDeliveryIdByName("Customer pickup");
default:
return GetDeliveryIdByName("Courier");
}
}
public void Execute(UserConnection userConnection) {
/* Create an ESQ that contains the "Id" column of the product and "Name" column from the "OrderProduct" database table. The "Name" column is the name of the product category. */
EntitySchemaQuery esqResult = new EntitySchemaQuery(userConnection.EntitySchemaManager, "OrderProduct");
EntitySchemaQueryColumn orderIdColumn = esqResult.AddColumn("Order.Id");
EntitySchemaQueryColumn productCategoryColumn = esqResult.AddColumn("Product.Category.Name");
/* Retrieve the collection of products in the order. */
EntityCollection productsInOrder = esqResult.GetEntityCollection(userConnection);
EntitySchemaManager entitySchemaManager = userConnection.EntitySchemaManager;
Entity orderEntity = entitySchemaManager.GetEntityByName("Order", userConnection);
foreach(Entity entity in productsInOrder) {
/* Retrieve the order object using the ID. */
Guid id = entity.GetTypedColumnValue <Guid> (orderIdColumn.Name);
string category = entity.GetTypedColumnValue <string> (productCategoryColumn.Name);
var conditions = new Dictionary <string,
object> {
{
"Id",
id
}
};
orderEntity.FetchFromDB(conditions);
/* Populate the delivery type based on the product category. */
orderEntity.SetColumnValue("DeliveryTypeId", GetDeliveryTypeByProductCategory(category));
/* Save the order. */
orderEntity.Save();
}
}
}
} -
Publish the schema.
2. Add the install script to the installation steps
- Open the package properties. To do this, click → Properties. This opens the Package properties page.
- Open the Installation steps tab.
- Click Add in the After package installation block. This opens the Select schemas window.
- Select the checkbox for the
UsrPopulateDeliveryType
source code schema. - Click Select.
- Apply the changes.
View the result
To view the result of the example for the current Creatio instance:
- Open the
UsrPopulateDeliveryType
source code schema. - Execute the install script. To do this, click Actions → Run as install script.
- Open the Orders section.
- Open an order that has an arbitrary name.
- Check the category of the product. To do this, open the Products tab → Product.Category column.
- Check the delivery type. To do this, open the Delivery tab → Delivery type field.
As a result:
- Creatio will populate the Delivery type field using the "Customer pickup" value for products whose Product.Category column has the "Hardware" or "Services" value.
- Creatio will populate the Delivery type field using the "Courier" value for products whose Product.Category column has other values. View the result >>>
To view the result of the example for other Creatio instances:
- Open the Configuration section. Instructions: Open the Configuration section.
- Export the package. To do this, click → Export.
- Install the package into the needed Creatio instance. Instructions: Install an app from a file (user documentation).
- Open the Orders section.
- Open an order that has an arbitrary name.
- Check the category of the product. To do this, open the Products tab → Product.Category column.
- Check the delivery type. To do this, open the Delivery tab → Delivery type field.
Source code
namespace Terrasoft.Configuration {
using System;
using System.Collections.Generic;
using Terrasoft.Core;
using Terrasoft.Core.Entities;
public class UsrPopulateDeliveryType: IInstallScriptExecutor {
private static object GetDeliveryIdByName(string deliveryName) {
/* The unique ID of the delivery type. */
switch (deliveryName) {
case "Courier":
return new Guid("50df77d0-7b1f-4dbc-a02d-7b6ebb95dfd0");
case "Customer pickup":
return new Guid("ab31305f-7c6d-4158-bd0a-760ac7897755");
default:
return Guid.Empty;
}
}
private static object GetDeliveryTypeByProductCategory(string category) {
/* The category of the product. */
switch (category) {
case "Hardware":
return GetDeliveryIdByName("Customer pickup");
case "Services":
return GetDeliveryIdByName("Customer pickup");
default:
return GetDeliveryIdByName("Courier");
}
}
public void Execute(UserConnection userConnection) {
/* Create an ESQ that contains the "Id" column of the product and "Name" column from the "OrderProduct" database table. The "Name" column is the name of the product category. */
EntitySchemaQuery esqResult = new EntitySchemaQuery(userConnection.EntitySchemaManager, "OrderProduct");
EntitySchemaQueryColumn orderIdColumn = esqResult.AddColumn("Order.Id");
EntitySchemaQueryColumn productCategoryColumn = esqResult.AddColumn("Product.Category.Name");
/* Retrieve the collection of products in the order. */
EntityCollection productsInOrder = esqResult.GetEntityCollection(userConnection);
EntitySchemaManager entitySchemaManager = userConnection.EntitySchemaManager;
Entity orderEntity = entitySchemaManager.GetEntityByName("Order", userConnection);
foreach(Entity entity in productsInOrder) {
/* Retrieve the order object using the ID. */
Guid id = entity.GetTypedColumnValue <Guid> (orderIdColumn.Name);
string category = entity.GetTypedColumnValue <string> (productCategoryColumn.Name);
var conditions = new Dictionary <string,
object> {
{
"Id",
id
}
};
orderEntity.FetchFromDB(conditions);
/* Populate the delivery type based on the product category. */
orderEntity.SetColumnValue("DeliveryTypeId", GetDeliveryTypeByProductCategory(category));
/* Save the order. */
orderEntity.Save();
}
}
}
}