In previous articles I discussed using the Model class for reading a single record, as well as inserting, updating, and deleting data. I have been holding off on writing an article on the topic of using the model class to retrieve data using filter conditions until the filter classes in the SDK were fixed and working, which is finally the case in Creatio 8.1.1.
Note: The filter classes in the SDK are working as of version 8.1.1 – if you’re using a prior version of Creatio, I’ll show a workaround at the end of the article.
In this article I will show how to use the Model class to retrieve a list of records using filter conditions. This is the Freedom UI equivalent to performing an EntitySchemaQuery. The Model class is available via the new 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 define some filter conditions and load the list of records for the conditions. We’ll start with a simple example of loading all Accounts where the Type column has the value of “Partner”. Let’s look at the code:
const accountModel = await sdk.Model.create("Account");
// define filter for Type = Partner
const filters = new sdk.FilterGroup();
await filters.addSchemaColumnFilterWithParameter(sdk.ComparisonType.Equal, "Type.Name", "Partner");
// now load the records and provide the filters
const partners = await accountModel.load({
attributes: ["Id", "Name", "Type"],
parameters: [{
type: sdk.ModelParameterType.Filter,
value: filters
}]
});
console.log(partners);
The end result is we’ll see a list of Accounts who have the Type of Partner output to the console. Well, not the entire record, just the columns we’re asking for in the attributes array. Obviously, the magic here is in the filters. Those should look somewhat familiar since they are very similar to how EntitySchemaQuery filters look.
Workaround for Creatio versions prior to 8.1.1
I mentioned previously, this only works in 8.1.1 and later. Why? The filter classes in the SDK were broken in prior versions. However, you can use a workaround if you’re not on 8.1.1 or later. The workaround is to shallow copy the filter object and then reassign the filter items. That would look like this:
// first, create the filter normally
const customerFilters = new sdk.FilterGroup();
await customerFilters.addSchemaColumnFilterWithParameter(sdk.ComparisonType.Equal, "Type.Name", "Customer");
// now, shallow copy to new object and reassign items
const newCustomerFilters = Object.assign({}, customerFilters);
newCustomerFilters.items = customerFilters.items;
// the newCustomerFilters is the one you'd now use in the model.load
Obviously, that is only necessary if you’re on a version prior to 8.1.1.




Do you know if it’s possible to return aggregated data in a Model class query? e.g. if you wanted to have some Count of Leads against an Account, alongside other Account data?
I believe so, yes, although I’ve not taken the time to figure that out yet. The SDK does have aggregation column types defined, so I assume the capability is there.
However, I am not sure how that would work since the columns are passed as an array of column names. Maybe it also allows passing more complex types instead of of just string column names?
Thanks Ryan, I also asked on an existing Community question about it, but apparently it’s not currently possible yet which is unfortunate: https://community.creatio.com/questions/i-want-use-aggregation-function
To meet the more important part of the requirement, I was able to use the EXISTS filter functionality (or in my case NOT EXISTS) that is supported by the model class that you describe in another article, but it’s a shame this isn’t part of the Model class feature set yet.
We still need to do the “pre-8.1.1” workaround even in version 8.3, is there something we’re missing about how Creatio is supposed to have resolved this in 8.1.1 onwards? The error we get is TypeError: t.clone is not a function
at creatio-devkit-common.umd.js?hash=944eddd99866469c94a2d7a8a2b6954a:2:53397
Works fine when using the workaround.
Yeah, it’s weird. In some conditions I don’t need the workaround, and in others I do not. I think if I am doing a model load I typically don’t need to do the workaround, however, for adding filters to other requests, such as a load request for lookups, I do need to do the workaround (or maybe it’s the other way around, I can’t remember). I keep thinking I need to report again to Creatio since obviously, it’s fixed in some places and not others, but haven’t had the time to nail down the scenarios that still don’t work correctly.