
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); query.Execute();
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"); query.Execute();
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 => { do { 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.
Subscribe To Our Newsletter
Join our mailing list to receive the latest Infor CRM (Saleslogix) and Creatio (bpm'online) news and product updates!
You have Successfully Subscribed!