Question

how to get Collection of data using model Class

Hello Everyone,

I am working on a scenario where I need to get Records (With Id and Name) from another object.

In Model class, we can get one record but how to get a Collection of records with a filter/Parameter "Name" it should get all the records with the same Name. 

 

Like 0

Like

4 comments

There is an issue in current versions of Creatio with the filter classes, such as FilterGroup etc, being applied to model load/queries. I've been reporting this issue since 8.0.6 (#SR-01182879) but it's still not yet fixed in 8.0.9, very frustrating (I've been told it will be fixed in 8.1 #SR-01198668 🤞🏻). However you can still use them with a small workaround. The example below will get all Accounts that have Type = Customer:

// make sure to add "@creatio-devkit/common" as sdk
 
const accountModel = await sdk.Model.create("Account");
 
const customerFilter = new sdk.FilterGroup();
await customerFilter.addSchemaColumnFilterWithParameter(sdk.ComparisonType.Equal, "Type.Name", "Customer");
 
// This is the workaround you have to do for the filters to work.
// Shallow copy to a new object and then reassign the items collection.
const newCustomerFilter = Object.assign({}, customerFilter);
newCustomerFilter.items = customerFilter.items;
 
// now use new shallow copied filters in the query
const customers = await accountModel.load({
    attributes: ["Id", "Name", "Type"],
    parameters: [{
        type: sdk.ModelParameterType.Filter,
        value: newCustomerFilter
    }]
});
 
// the customers variable is now an array of accounts with Id, Name, and Type columns
console.log(customers);

The whole issue with the FilterGroup is that the items collection doesn't get unrolled from a collection to an array - the copy forces the object to flatten out how it's expected by the underlying DataService call.

Ryan

Ryan = Creatio's code gap workaround master

Ryan Farley,

 

This is my code : 

handler: async (request, next) => {

        // Get the selected date values

        var dateFrom = new Date(await request.$context.DateTimeAttribute_6jtq65k);

        var dateTo = new Date(await request.$context.DateTimeAttribute_vg9eydm);

        var datesBetween = [];

          var currentDate = new Date(dateFrom);

          while (currentDate <= dateTo) {

        datesBetween.push(new Date(currentDate));

        currentDate.setDate(currentDate.getDate() + 1);

          }

  // Convert dates to integers without the time component

          var datesAsIntegers = datesBetween.map(date => {

        return parseInt(date.toISOString().slice(0, 10).replace(/-/g, ""), 10);

          });

        const accountModel = await sdk.Model.create("UsrEntity_a6e9b72");      

        // Load the accounts based on the filter.

        const accounts = await accountModel.load({

            attributes: ["DateTimeAttribute_x2whel6"],

        });

        console.log(accounts); 

            for (const value of accounts) 

            {

                for (const value1 of datesAsIntegers)

                {

                   const integerValue = parseInt(value1);

                value.NumberAttribute_ejh4wbq === integerValue;

                }

                console.log("1");

            }

return next?.handle(request);

        }

Here we are trying to get value of "NumberAttribute_ejh4wbq" but we are only getting "Id" .

Can you help us with this?

 

 

Verify that the column name is correct. In the accountModel.load your passing the attributes array with "DateTimeAttribute_x2whel6", this needs to be the column name in the object/table (which will likely start with a Usr)

Ryan

Show all comments