Read records using DataService
To implement the example:
- Set up the page UI. Read more >>>
- Set up how to read records. Read more >>>
Add a Receive summarized contacts button to the Contacts section. Using the Model
class, the button must receive the following data of all contacts:
- Full name
- Number of activities that contains aggregated number of contact activities.
Display the result in the browser console.
1. Set up the page UI
-
Open the Customer 360 app in the No-Code Designer.
-
Open the Advanced settings tab in the No-Code Designer. To do this, click in the top right → "Application management" → "Application Hub" → Customer 360 app → "Advanced settings."
-
Create a user-made package to add the report. To do this, click → Create new package → fill out the package properties → Save.
For this example, create the
sdkReadContactsViaSelectQuery
user-made package. -
Change the current package. Instructions: Change the current package.
For this example, change the current package to
sdkReadContactsViaSelectQuery
user-made package. -
Open list page in the Freedom UI Designer.
For this example, open the Contacts list page.
-
Add a button.
For this example, add the button that receives summarized contact data.
To do this:
-
Add a Button type component to the toolbar of the Freedom UI Designer.
-
Click and fill out the button properties.
Element
Property
Property value
Button that receives summarized contact data
Title
Receive summarized contacts
Style
Focus
-
-
Save the changes.
2. Set up how to read records
Configure the business logic in the Client Module Designer. For this example, set up how to read records.
-
Open the source code of the Freedom UI page. To do this, click .
-
Add the dependencies. To do this, add
@creatio-devkit/common
library as a dependency. The library includes theModel
class to access the data source.AMD module dependencies/* Declare the AMD module. */
define("Contacts_ListPage", /**SCHEMA_DEPS*/["@creatio-devkit/common"] /**SCHEMA_DEPS*/, function/**SCHEMA_ARGS*/(sdk)/**SCHEMA_ARGS*/ {
return {
...
}
}); -
Set up how to handle the action executed on button click.
- Go to the
viewConfigDiff
schema section →UsrReceiveContactsButton
element. - Bind the sending of the custom
usr.ReceiveContactsRequest
request to theclicked
button event.
viewConfigDiff schema sectionviewConfigDiff: /**SCHEMA_VIEW_CONFIG_DIFF*/[
/* Button that receives summarized contact data. */
{
"operation": "insert",
"name": "UsrReceiveContactsButton",
"values": {
...,
"clicked": {
/* Bind the sending of the custom request to the "clicked" button event. */
"request": "usr.ReceiveContactsRequest"
}
},
...
}
]/**SCHEMA_VIEW_CONFIG_DIFF*/, - Go to the
-
Implement the custom request handler.
For this example, implement the custom
usr.ReceiveContactsRequest
request handler.- Go to the
handlers
schema section. - Create an instance of the model from
@creatio-devkit/common
. For this example, create an instance of theContact
model. - Load required data of the model.
- Add columns to the query. For this example, add Full name, Number of activities using the
Name
,ActivitiesCount
codes respectively. - Receive required data from the collection.
handlers schema sectionhandlers: /**SCHEMA_HANDLERS*/[
{
request: "usr.ReceiveContactsRequest",
/* The implementation of the custom request handler. */
handler: async (request, next) => {
/* Create an instance of the "Contact" model from "@creatio-devkit/common." */
const model = await sdk.Model.create('Contact');
/* Load required data of the "Contact" model. */
const result = await model.load({
/* Add columns to the query. */
attributes: [
{
name: 'Name',
path: 'Name',
},
{
name: 'ActivitiesCount',
path: '[Activity:Contact].Id',
type: 'aggregation',
aggregationConfig: {
aggregationFunction: 'Count',
}
}
],
parameters: [],
loadOptions: {},
});
/* Query result. */
var message = "";
/* Receive required data from the collection. */
result.forEach(function(item) {
message += `Full name: ${item.Name}. Number of activities: ${item.ActivitiesCount}\n`;
});
/* Display the result in the browser console. */
console.info(message);
/* Call the next handler if it exists and return its result. */
return next?.handle(request);
},
}
] /**SCHEMA_HANDLERS*/, - Go to the
-
Save the changes.
View the result
- Open the Contacts section.
- Click Receive summarized contacts.
As a result, Creatio will display Full name and Number of activities data of all contacts in the browser console. View the result >>>
Source code
/* Declare the AMD module. */
define("Contacts_ListPage", /**SCHEMA_DEPS*/["@creatio-devkit/common"]/**SCHEMA_DEPS*/, function/**SCHEMA_ARGS*/(sdk)/**SCHEMA_ARGS*/ {
return {
viewConfigDiff: /**SCHEMA_VIEW_CONFIG_DIFF*/[
/* Button that receives summarized contact data. */
{
"operation": "insert",
"name": "UsrReceiveContactsButton",
"values": {
"type": "crt.Button",
"caption": "#ResourceString(UsrReceiveContactsButton_caption)#",
"color": "accent",
"disabled": false,
"size": "large",
"iconPosition": "only-text",
"visible": true,
"clicked": {
/* Bind the sending of the custom request to the "clicked" button event. */
"request": "usr.ReceiveContactsRequest"
}
},
"parentName": "ActionButtonsContainer",
"propertyName": "items",
"index": 0
}
]/**SCHEMA_VIEW_CONFIG_DIFF*/,
viewModelConfigDiff: /**SCHEMA_VIEW_MODEL_CONFIG_DIFF*/[]/**SCHEMA_VIEW_MODEL_CONFIG_DIFF*/,
modelConfigDiff: /**SCHEMA_MODEL_CONFIG_DIFF*/[]/**SCHEMA_MODEL_CONFIG_DIFF*/,
handlers: /**SCHEMA_HANDLERS*/[
{
request: "usr.ReceiveContactsRequest",
/* The implementation of the custom request handler. */
handler: async (request, next) => {
/* Create an instance of the "Contact" model from "@creatio-devkit/common." */
const model = await sdk.Model.create('Contact');
/* Load required data of the "Contact" model. */
const result = await model.load({
/* Add columns to the query. */
attributes: [
{
name: 'Name',
path: 'Name',
},
{
name: 'ActivitiesCount',
path: '[Activity:Contact].Id',
type: 'aggregation',
aggregationConfig: {
aggregationFunction: 'Count',
}
}
],
parameters: [],
loadOptions: {},
});
/* Query result. */
var message = "";
/* Receive required data from the collection. */
result.forEach(function(item) {
message += `Full name: ${item.Name}. Number of activities: ${item.ActivitiesCount}\n`;
});
/* Display the result in the browser console. */
console.info(message);
/* Call the next handler if it exists and return its result. */
return next?.handle(request);
},
}
] /**SCHEMA_HANDLERS*/,
converters: /**SCHEMA_CONVERTERS*/{}/**SCHEMA_CONVERTERS*/,
validators: /**SCHEMA_VALIDATORS*/{}/**SCHEMA_VALIDATORS*/
};
});