
If you need to create a record in C# code in Creatio, the best option to take is using the Entity class. You’re likely already using the Entity class, when you perform a query using EntitySchemaQuery in server-side code, you’re getting back either an Entity object, or an EntityCollection (a collection of Entity objects). The Entity class represents *any* Entity in the system, which this is something I really like about it, rather than using a strongly typed object specific to one Entity. In my opinion this makes it extremely flexible and very easy to use. Let’s take a look at how to use it to create a record.
Creating a Record
In the sample below we will be creating a Contact record.
var schema = UserConnection.EntitySchemaManager.GetInstanceByName("Contact"); // Create an instance of a new object var entity = schema.CreateEntity(UserConnection); // Set any default values for the object columns entity.SetDefColumnValues(); entity.SetColumnValue("Name", "Ryan Farley"); entity.SetColumnValue("Phone", "800-555-1212"); entity.Save();
That is it, very easy. The Save method also has some options we can use. It take an optional boolean parameter for validateRequired – (Default: true) This indicates whether to validate that required fields for the entity are being set. If you change this to false it will allow you to add the record without required values.
Updating a Record
Let’s say you have a record Id and you want to (1) get this record from the database and (2) update a value of column(s) for the record. In this sample, we will be updating a Contact record, given the record Id value.
var schema = UserConnection.EntitySchemaManager.GetInstanceByName("Contact"); // Create an instance of a new object var entity = schema.CreateEntity(UserConnection); // Now load the contact record using it's Id value if (entity.FetchFromDB(Id)) { entity.SetColumnValue("Phone", "800-728-5783"); entity.Save(); }
The code above will load the record into the Entity object, if the load is successful, meaning the record exists, it will update it’s Phone value.
The Entity class includes a lot of useful methods to do things like validate if required fields are populated, serializing it to JSON, reverting the set columns back to a previous state and even setting byte array for object columns.
Note, the Entity class isn’t the only way to create or update records, there are other options like the Insert and Update classes, however, using the Entity class will ensure that added/modified signals are still triggered for processes and access permissions are still applied.
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!