Skip to main content
Version: 8.1

Examples of package install scripts

Level: intermediate

Install scripts let you execute CRUD operations with data using Entity and EntitySchemaQuery classes. Learn more about the Entity class in the .NET class library. Learn more about the EntitySchemaQuery class in the .NET class library.

Retrieve data

Retrieve a contact that has specified data

Example

Retrieve a contact record that has the "SomeContactName" name.

Example that retrieves a contact record by name
public static Entity GetContact(UserConnection userConnection, string name) {
EntitySchemaManager entitySchemaManager = userConnection.EntitySchemaManager;
Entity contact = entitySchemaManager.GetEntityByName("Contact", userConnection);
var contactConditions = new Dictionary<string, object> {
{ "SomeContactName", name }
};
return !contact.FetchFromDB(contactConditions) ? null : contact;
}

Retrieve data using pagination

Example

Fill out the type of delivery. Retrieve data using pagination.

Example that retrieves data using pagination
namespace Terrasoft.Configuration {
using System;
using System.Collections.Generic;
using Terrasoft.Core;
using Terrasoft.Core.Entities;

public class FillDeliveryType: IInstallScriptExecutor {

public void Execute(UserConnection userConnection) {
/* Create an ESQ that contains the [Id] column of the product and [Name] column from the [OrderProduct] database table. */
EntitySchemaQuery esqResult = new EntitySchemaQuery(userConnection.EntitySchemaManager, "OrderProduct");
EntitySchemaQueryColumn orderIdColumn = esqResult.AddColumn("Order.Id");
EntitySchemaQueryColumn productCategoryColumn = esqResult.AddColumn("Product.Category.Name");
/* Implement pagination. */
var options = new EntitySchemaQueryOptions {
PageableDirection = Core.DB.PageableSelectDirection.First,
RowsOffset = 0,
PageableRowCount = userConnection.AppConnection.MaxEntityRowCount,
PageableConditionValues = new Dictionary <string, object> ()
};
/* Retrieve the collection of the orders. */
EntityCollection productsInOrder = esqResult.GetEntityCollection(userConnection, options);
EntitySchemaManager entitySchemaManager = userConnection.EntitySchemaManager;
Entity orderEntity = entitySchemaManager.GetEntityByName("Order", userConnection);
/* The first page. */
EditOrders(productsInOrder, orderIdColumn, productCategoryColumn, orderEntity);
int rowsRead = productsInOrder.Count;
/* Use the pagination. */
while (productsInOrder.Count > 0) {
options.RowsOffset = rowsRead;
/* Scroll forward. */
options.PageableDirection = Core.DB.PageableSelectDirection.Next;
/* Retrieve next page. */
esqResult.ResetSelectQuery();
productsInOrder = esqResult.GetEntityCollection(userConnection, options);
rowsRead += productsInOrder.Count;
EditOrders(productsInOrder, orderIdColumn, productCategoryColumn, orderEntity);
}
}

private void EditOrders(EntityCollection productsInOrder, EntitySchemaQueryColumn orderIdColumn,
EntitySchemaQueryColumn productCategoryColumn, Entity orderEntity) {
/* Implement the modifying logic. */
}
}
}

To work with big data, follow the recommendations listed in a separate article: Customize delivery process.

Add data

Add a contact that has specified data

Example

Add a contact that has the specified name and account.

Example that adds a contact that has the specified name and account
public static void CreateContact(UserConnection userConnection, string contactName, string accountName) {
if (GetContact(userConnection, contactName) != null) {
return;
}
EntitySchemaManager entitySchemaManager = userConnection.EntitySchemaManager;
Entity contact = entitySchemaManager.GetEntityByName("Contact", userConnection);
contact.SetDefColumnValues();
contact.SetColumnValue("Id", Guid.NewGuid());
/* The AccountHelper class is a helper class. */
contact.SetColumnValue("AccountId", AccountHelper.GetAccountId(userConnection, accountName));
contact.SetColumnValue("Name", contactName);
contact.Save();
}

Add a new job title that supports different cultures

Example

Add a new job title that supports Spanish and English cultures.

Example that adds a new job title that supports different cultures
namespace Terrasoft.Configuration {
using System;
using System.Globalization;
using Terrasoft.Common;
using Terrasoft.Core;
using Terrasoft.Core.Entities;

public class CreateJobTitle: IInstallScriptExecutor {

public void Execute(UserConnection userConnection) {
EntitySchemaManager entitySchemaManager = userConnection.EntitySchemaManager;
Entity job = entitySchemaManager.GetEntityByName("Job", userConnection);
job.SetDefColumnValues();
job.SetColumnValue("Id", Guid.NewGuid());
var jobName = new LocalizableString();
var esCulture = new CultureInfo("es-ES");
var enCulture = new CultureInfo("en-US");
jobName.SetCultureValue(esCulture, "Mentor");
jobName.SetCultureValue(enCulture, "Master");
job.SetColumnValue("Name", jobName);
job.Save();
}
}
}

Modify data

Example

Modify a contact field.

Example that modifies a contact field
public static void SetContactField(UserConnection userConnection, string contactName, string columnValue) {
/* The ContactHelper class is a helper class. */
var entity = ContactHelper.GetContact(userConnection, contactName);
/* The LanguageHelper class is a helper class. */
entity.SetColumnValue("ProgrammingLanguageId", LanguageHelper.GetLanguageId(userConnection, columnValue));
entity.Save();
}

Delete data

Example

Delete a contact record.

Example that deletes a contact record
public static void DeleteContact(UserConnection userConnection, string contactName) {
/* The ContactHelper class is a helper class. */
var contact = ContactHelper.GetContact(userConnection, contactName);
contact.Delete();
}