Skip to main content
Version: 8.2

Populate a field using an install script

Level: intermediate
note

The example is relevant to Order and Contract Management app.

To implement the example:

  1. Implement an install script. Read more >>>
  2. Add the install script to the installation steps. Read more >>>
Example

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 Category column has the "Hardware" or "Services" value.
  • Set the field to "Courier" for products whose Category column has other values.

Populate the Delivery type field for orders that contain a single product.

1. Implement an install script

  1. Open the Order and Contract Management app in the No-Code Designer.

  2. Open the Advanced settings tab in the No-Code Designer. To do this, click in the top right → Application managementApplication HubOrder and Contract Management app → Advanced settings.

  3. Create a user-made package to add the schema. To do this, click Create new package → fill out the package properties → Save.

    For this example, create the sdkPopulateDeliveryType user-made package.

  4. Create the source code schema. To do this, click AddSource code.

  5. Fill out the schema properties.

    For this example, use the following schema properties.

    Property

    Property value

    Code

    UsrPopulateDeliveryType

    Title

    PopulateDeliveryType

  6. Apply the changes.

  7. Implement the business logic of the delivery type population.

    1. 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."
    2. Set "Customer pickup" for products that have the "Hardware" and "Services" category.
    3. Set "Courier" for products that have other categories.
    4. Populate the delivery type based on the product category.
    UsrPopulateDeliveryType
    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();
    }
    }
    }
    }
  8. Publish the schema.

2. Add the install script to the installation steps

  1. Open the package properties. To do this, click Properties. This opens the Package properties page.
  2. Open the Installation steps tab.
  3. Click Add in the After package installation block. This opens the Select schemas window.
  4. Select the checkbox for the UsrPopulateDeliveryType source code schema.
  5. Click Select.
  6. Apply the changes.

View the result

To view the result of the example for the current Creatio instance:

  1. Open the UsrPopulateDeliveryType source code schema.
  2. Execute the install script. To do this, click ActionsRun as install script.
  3. Open the Orders section.
  4. Open an order that has an arbitrary name.
  5. Check the category of the product. To do this, open the Products tab → Category column.
  6. Check the delivery type. To do this, open the Payment & Delivery tab → Delivery type field.

As a result:

  • Creatio will populate the Delivery type field using the "Customer pickup" value for products whose Category column has the "Hardware" or "Services" value.
  • Creatio will populate the Delivery type field using the "Courier" value for products whose Category column has other values. View the result >>>

To view the result of the example for other Creatio instances:

  1. Open the Configuration section. Instructions: Open the Configuration section.
  2. Export the package. To do this, click Export.
  3. Install the package into the needed Creatio instance. Instructions: Install an app from a file (user documentation).
  4. Open the Orders section.
  5. Open an order that has an arbitrary name.
  6. Check the category of the product. To do this, open the Products tab → Category column.
  7. Check the delivery type. To do this, open the Payment & Delivery tab → Delivery type field.

Source code

UsrPopulateDeliveryType
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();
}
}
}
}

Resources

Package with example implementation