Question

How can we read a collection of items using Model class

Hello, 

 

I am able to read a single record using Model class that helps me alot. 

For reading a collection I am using ESQ, but I wanted to use Model class which simplifies my work.

Is it possible to read a collection of records using Model class?

 

Like 0

Like

3 comments

Hello,



Could you please clarify what you mean by "Model class"? Also, could you kindly provide us with an example of how you read a record?



Thank you.

For Orkhan: The model class is the new method for working with data objects in Freedom UI pages, available in the DevKit SDK, replacing EntitySchemaQuery, InsertQuery, UpdateQuery, and DeleteQuery. An example of reading a record using the Model class is here https://customerfx.com/article/retrieving-a-record-via-the-model-class-…

 

However there are issues currently when reading a collection of records since the filters are not properly set. I have an open case with support on this topic (case #SR-01182879). When filters are used with the model load it sets the filters incorrectly and causes a server error. I have an article ready to publish on this topic as soon as the issue is resolved but I’ve not heard anything from support yet. 

The code to load a collection of records using the model class would look something like this (however, as I mentioned, the filter gets applied incorrectly and the query fails with a server error 500 due to the invalid filter)

const accountModel = await sdk.Model.create("Account");
 
const filters = new sdk.FilterGroup();
await filters.addSchemaColumnFilterWithParameter(sdk.ComparisonType.Equal, "Type.Name", "Partner");
 
const partners = await accountModel.load({
    attributes: ["Id", "Name", "Type"],
    parameters: [{
        type: sdk.ModelParameterType.Filter,
        value: filters
    }]
});



Ryan

Also, I do have a workaround for the issue with querying a collection of records using the Model class. The issue that occurs is with how the filters are serialized to JSON for the request. However, if you shallow copy the filters first, then replace the filter.items, it will work. Here is a sample with this workaround (this does work successfully):

const accountModel = await sdk.Model.create("Account");
 
// create the filters
const filters = new sdk.FilterGroup();
await filters.addSchemaColumnFilterWithParameter(sdk.ComparisonType.Equal, "Type.Name", "Partner");
 
// shallow copy the filters to a new object and replace the items
const newFilters = Object.assign({}, filters);
newFilters.items = filters.items;
 
const accounts = await accountModel.load({
    attributes: ["Id", "Name", "Type"],
    parameters: [{
        type: sdk.ModelParameterType.Filter,
        value: newFilters
    }]
});

Hopefully I'll hear back on why this only works with this workaround and this will no longer be necessary.

Ryan

Show all comments