Executing Direct SQL Statements in a Process or Configuration Web Service in Creatio (formerly bpm’online)

Creatio does have a lot of options for working with objects/data with an Entity object model, EntitySchemaQuery, Select/Update/Insert objects (I’ll get around to writing articles on these topics as well). You can also execute a SQL statement directly when needed as well.

Before we get into the code, it’s worth mentioning, executing a SQL statement directly in the database won’t fire signals for processes listening for various events, it won’t consider any user security or restrictions, and it won’t fire any entity object events. Still, it’s a valid tool to use in Creatio when the situation requires a direct SQL update for performance reasons.

The code is pretty simple, it uses a CustomQuery object, which takes a UserConnection. Note, to use the CustomQuery class, you must include Terrasoft.Core.DB.

using Terrasoft.Core.DB;

Let’s take a look at using it to execute a SQL statement that don’t return any results, like an INSERT/UPDATE/DELETE statement.

var sql = "update mytable set myfield = 'my value' where somecondition = true";

var query = new CustomQuery(UserConnection, sql);

You can set parameters with CustomQuery as well:

var sql = "update mytable set myfield = @myvalue where somecondition = @mycondition";

var query = new CustomQuery(UserConnection, sql);
query.Parameters.Add("@myvalue", "some value");
query.Parameters.Add("@mycondition", "some condition");

If you need a value returned, you can get that as well:

var sql = "select count(*) from mytable where somecondition = true";

var query = new CustomQuery(UserConnection, sql);
var count = query.ExecuteScalar<int>();

Or even get a DataReader with the results:

var sql = "select myfield1, myfield2 from mytable where somecondition = true";

var query = new CustomQuery(UserConnection, sql);
query.ExecuteReader(reader => {
        var myfield1 = reader.GetColumnValue<string>("myfield1");
        // do something...
    while (reader.Read());

These methods will work in ScriptTasks in a process or in a configuration web service as well. You can even use this method to fill the DataSet for a DevExpress report.

Want content like this delivered to your inbox? Sign up for our newsletter!

Ryan Farley

Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix, now Infor CRM, since 2001 and believes in sharing with the community. His new passion for CRM is Creatio, formerly bpm'online. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

Submit a Comment

Your email address will not be published. Required fields are marked *