Create a new record using DataService
To implement the example:
- Set up the page UI. Read more >>>
- Set up how to create a record. Read more >>>
Add an Add demo contact button to the Contacts section. Using the Model
class, the button must add the demo contact that contains the following data:
- Full name – "John Best."
- Full job title – "Developer."
- Business phone – "+12 345 678 00 00."
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
sdkAddDemoContactViaInsertQuery
user-made package. -
Change the current package. Instructions: Change the current package.
For this example, change the current package to
sdkAddDemoContactViaInsertQuery
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 adds a demo contact.
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 adds a demo contact
Title
Add demo contact
Style
Focus
-
-
Save the changes.
2. Set up how to create a record
Configure the business logic in the Client Module Designer. For this example, set up how to create a record.
-
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 →UsrAddDemoContactButton
element. - Bind the sending of the custom
usr.CreateDemoContactRequest
request to theclicked
button event.
viewConfigDiff schema sectionviewConfigDiff: /**SCHEMA_VIEW_CONFIG_DIFF*/[
/* Button that adds a demo contact. */
{
"operation": "insert",
"name": "UsrAddDemoContactButton",
"values": {
...,
"clicked": {
/* Bind the sending of the custom request to the "clicked" button event. */
"request": "usr.CreateDemoContactRequest"
}
},
...
}
]/**SCHEMA_VIEW_CONFIG_DIFF*/, - Go to the
-
Implement the custom request handler.
For this example, implement the custom
usr.CreateDemoContactRequest
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. - Specify values of the contact columns. Fill out the Full name, Full job title, Business phone fields using "John Best," "Developer," "+12 345 678 00 00," respectively. View the ID of the "Developer" job in the Job titles lookup. For this example, the ID is "11d68189-ced6-df11-9b2a-001d60e938c6."
handlers schema sectionhandlers: /**SCHEMA_HANDLERS*/[
{
request: "usr.CreateDemoContactRequest",
/* The implementation of the custom request handler. */
handler: async () => {
/* Create an instance of the "Contact" model from "@creatio-devkit/common." */
const model = await sdk.Model.create('Contact');
/* Specify values of the contact columns. */
const response = await model.insert({
Name: 'John Best',
Job: '11d68189-ced6-df11-9b2a-001d60e938c6',
MobilePhone: '+12 345 678 00 00'
});
if (response) {
/* If a contact is created, display the message box. */
alert("'John Best' demo contact has been created.");
} else {
/* If a contact is not created, display the message box. */
alert("'John Best' demo contact has not been created. Check the business logic that creates a demo contact.");
}
/* 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 Add demo contact.
As a result, Creatio will create a demo contact and populate the Full name, Full job title, Business phone fields using the "John Best," "Developer," "+12 345 678 00 00," respectively. 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 adds a demo contact. */
{
"operation": "insert",
"name": "UsrAddDemoContactButton",
"values": {
"type": "crt.Button",
"caption": "#ResourceString(UsrAddDemoContactButton_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.CreateDemoContactRequest"
}
},
"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.CreateDemoContactRequest",
/* The implementation of the custom request handler. */
handler: async () => {
/* Create an instance of the "Contact" model from "@creatio-devkit/common." */
const model = await sdk.Model.create('Contact');
/* Specify values of the contact columns. */
const response = await model.insert({
Name: 'John Best',
Job: '11d68189-ced6-df11-9b2a-001d60e938c6',
MobilePhone: '+12 345 678 00 00'
});
if (response) {
/* If a contact is created, display the message box. */
alert("'John Best' demo contact has been created.");
} else {
/* If a contact is not created, display the message box. */
alert("'John Best' demo contact has not been created. Check the business logic that creates a demo contact.");
}
/* 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*/
};
});