Inserting a Record from Client-Side Code Using the Model Class in a Creatio Freedom UI Page

In classic Creatio pages, to insert a new record you would use an InsertQuery. Now, in Freedom UI pages you can insert records using the Model class that is a part of the Creatio DevKit SDK.

To use the devkit, you’ll need to add “@creatio-devkit/common” to the modules list of your page. This will look like this (note the “@creatio-devkit/common” in the square brackets and the sdk without quotes in the parenthesis):

define("UsrMyCustomEntity_FormPage", /**SCHEMA_DEPS*/["@creatio-devkit/common"] /**SCHEMA_DEPS*/, 
function/**SCHEMA_ARGS*/(sdk)/**SCHEMA_ARGS*/ {
	return {
		// ... the rest of the page here
	};
});

Now, with that module available, you can access things in the SDK, such as the Model class. We’ll use this model class to create a model for a specific entity. Then, we’ll pass a payload of the record to create. In this sample, we’ll be creating an account, so we’ll first create an account model, then pass the payload of the record to create to the Model’s insert function. The payload is simply an object containing the Column’s an values for the record you’re creating. It can contain any fields you’d like, however, if you don’t include fields that are required the insert will fail. Let’s look at the code to insert an account:

// create account model
const accountModel = await sdk.Model.create("Account");

// insert a new account
const result = await accountModel.insert({
    Name: "New account",
    Phone: "800-555-1212",
    Web: "www.someaccount.com"
});

The result returned by the insert provides an object with the following properties:

  1. success: true/false
  2. rowsAffected: Number of rows affected by the operation, in this case, 1
  3. primaryAttributes: An array of column/value pairs. For the insert above, the array contains a single element, which is:
    { Id: "81167933-a9b4-42c2-b589-4e9e55bf7b45" }

This will provide you with the Id of the created account. This is a welcomed change for sure – quite a bit easier than using the InsertQuery.

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

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 *