Custom request handler implemented using remote module
Creatio lets you implement the business logic of the Freedom UI page using request handlers. Creatio executes request handlers when an event is triggered on a visual element of a Freedom UI page. You can use base request handlers or implement a new one. Base handlers are executed at different life cycle stages. Learn more: Creatio front-end architecture.
For example, use request handlers to customize the data export from section list. To do this, add a button to the Freedom UI page, implement a custom request handler and bind the sending of custom request to the clicked button event. Learn more: Excel data export (user documentation).
You can modify the business logic of a request handler based on the type of related request. View the request type in the component documentation or source code of the schema that implements the request logic.
You can implement a custom request handler in the following ways:
- Use the
handlersschema section of the Freedom UI page. View examples that invoke handlers: Page customization examples. - Use a remote module.
To implement a custom request handler using remote module:
- Create an Angular project to develop a custom request handler using remote module.
- Create a custom request using remote module.
- Create a custom service using remote module.
- Create custom request handlers using remote module.
- Add the request implemented using remote module to the Freedom UI page.
1. Create an Angular project to develop a custom request handler using remote module
Develop a custom request handler in a dedicated npm package using an external IDE. This example covers the request handler development in Microsoft Visual Studio Code.
You can create an Angular project to develop a custom request handler in multiple ways, similarly to creating an Angular project to develop a custom UI component using remote module. To do this, follow the instructions: Custom UI component implemented using remote module.
2. Create a custom request using remote module
-
Create an Angular class in the project. To do this, run the
ng g class some-request-name.requestcommand at the command line terminal of Microsoft Visual Studio Code.As a result, Microsoft Visual Studio Code will add the
SomeRequestNameRequestclass files to thesrc/app/project directory. -
Implement the request.
- Open the
some-request-name.request.tsfile. - Inherit the
BaseRequestclass from the@creatio-devkit/commonlibrary. - Add the type and parameter of the request.
- Import the required functionality from the libraries into the class.
- Save the file.
some-request-name.request.ts file/* Import the required functionality from the libraries. */
import { BaseRequest } from "@creatio-devkit/common";
export class SomeRequestNameRequest extends BaseRequest{
/* The type and parameters of the request. */
public someParameterName!: someParameterType;
} - Open the
-
Build the project. To do this, run the
npm run buildcommand at the command line terminal of Microsoft Visual Studio Code.
As a result, Microsoft Visual Studio Code will add the build to the dist directory of the Angular project. The build will have the <%projectName%> name specified on step 1.
3. Create a custom service using remote module
-
Create an Angular class in the project. To do this, run the
ng g class some-service-name.servicecommand at the command line terminal of Microsoft Visual Studio Code.As a result, Microsoft Visual Studio Code will add the
SomeServiceNameServiceclass files to thesrc/app/project directory. -
Implement the service that receives data from external web service.
- Open the
some-service-name.service.tsfile. - Flag the
SomeServiceNameServiceclass using theInjectabledecorator. - Implement the
someMethodName()method that receives data from external web service. - Import the required functionality from the libraries into the class.
- Save the file.
some-service-name.service.ts file/* Import the required functionality from the libraries. */
import { Injectable } from "@angular/core";
@Injectable({
providedIn: 'root',
})
export class SomeServiceNameService {
/* Receive data from external web service. */
public someMethodName(someParameterName: someParameterType): Promise<{
someParameterName: Record<string, number>
}> {
/* Implement the business logic. */
...
/* Return data from external web service. */
return Promise.resolve({
someParameterName: {'someParameterName': /* Receive data from external web service. */ }
});
}
} - Open the
-
Build the project. To do this, run the
npm run buildcommand at the command line terminal of Microsoft Visual Studio Code.
As a result, Microsoft Visual Studio Code will add the build to the dist directory of the Angular project. The build will have the <%projectName%> name specified on step 1.
4. Create custom request handlers using remote module
-
Create a custom handler that displays data received from the external web service on the Freedom UI page.
-
Set up the Freedom UI page.
-
Repeat steps 1-6 of the procedure to implement a custom UI component using remote module.
-
Add a
SomeAttributeNameattribute that stores data received from the external web service to theviewModelConfigDiffschema section.viewModelConfigDiff schema sectionviewModelConfigDiff: /**SCHEMA_VIEW_MODEL_CONFIG_DIFF*/[
{
"operation": "merge",
"path": [
"attributes"
],
"values": {
...,
/* The attribute that stores data received from the external web service. */
"SomeAttributeName": {}
}
}
]/**SCHEMA_VIEW_MODEL_CONFIG_DIFF*/, -
Bind the
captionproperty of the corresponding component to the$SomeAttributeNamemodel attribute in theviewConfigDiffschema section.viewConfigDiff schema sectionviewConfigDiff: /**SCHEMA_VIEW_CONFIG_DIFF*/[
...,
{
"operation": "insert",
"name": "SomeComponentName",
"values": {
...,
/* Bind the SomeAttributeName attribute to the caption property. */
"caption": "$SomeAttributeName",
...
},
...
}
]/**SCHEMA_VIEW_CONFIG_DIFF*/, -
Save the changes.
-
-
Create an Angular class in the project. To do this, run the
ng g class some-handler-name.handlercommand at the command line terminal of Microsoft Visual Studio Code.As a result, Microsoft Visual Studio Code will add the
SomeHandlerNameHandlerclass files to thesrc/app/project directory. -
Implement the handler.
-
Open the
some-handler-name.handler.tsfile. -
Add the configuration object that declares the request handler.
- Set
typeproperty tousr.SomeHandlerNameHandler. Thetypeproperty is the type of handler. - Set
requestTypeproperty tocrt.UpdateCurrentTimeRequest. Thetypeproperty is the type of request to execute the handler specified in thetypeproperty.
- Set
-
Flag the
SomeHandlerNameHandlerclass using theCrtRequestHandlerdecorator. -
Inherit the
BaseRequestHandlerclass from the@creatio-devkit/commonlibrary. -
Implement the handling of the request result.
-
Generate the value to display in the component of Freedom UI page.
-
Import the required functionality from the libraries into the class.
-
Save the file.
some-handler-name.handler.ts file/* Import the required functionality from the libraries. */
import { BaseRequestHandler, CrtRequestHandler } from "@creatio-devkit/common";
import { SomeServiceNameService } from "./some-service-name.service";
import { SomeRequestNameRequest } from "./some-request-name.request";
/* Add the CrtRequestHandler decorator to the SomeHandlerNameHandler class. */
@CrtRequestHandler({
type: 'usr.SomeHandlerNameHandler',
requestType: 'crt.SomeRequestNameRequest',
})
export class SomeHandlerNameHandler extends BaseRequestHandler{
constructor(private _someServiceNameService: SomeServiceNameService) {
super();
}
public async handle(request: SomeRequestNameRequest): Promise<unknown> {
const result = await this._someServiceNameService.someMethodName(request.someParameterName);
const someParameter1Name = result.someParameterName[request.someParameterName] ?? 0
/* Generate the value to display in the component of Freedom UI page. */
request.$context['SomeAttributeName'] = someParameter1Name;
return someParameter1Name;
}
} -
-
-
Create a custom handler that is executed when Creatio initializes a Freedom UI page.
-
Create an Angular class in the project. To do this, run the
ng g class some-handler1-name.handlercommand at the command line terminal of Microsoft Visual Studio Code.As a result, Microsoft Visual Studio Code will add the
SomeHandler1NameHandlerclass files to thesrc/app/project directory. -
Implement the handler.
-
Open the
some-handler1-name.handler.tsfile. -
Add the configuration object that declares the request handler.
- Set
typeproperty tousr.SomeHandler1NameHandler. - Set
requestTypeproperty tocrt.HandleViewModelInitRequest. - Set
scopesproperty to code of Freedom UI page. The request handler is executed when Creatio initializes the Freedom UI page that has the specified code. If the property is empty or missing, it is a global handler that Creatio executes on all Freedom UI pages.
- Set
-
Flag the
SomeHandler1NameHandlerclass using theCrtRequestHandlerdecorator. -
Inherit the
BaseRequestHandlerclass from the@creatio-devkit/commonlibrary. -
Implement the update of data received from the external web service when Creatio initializes the Freedom UI page.
-
Import the required functionality from the libraries into the class.
-
Save the file.
some-handler1-name.handler.ts file/* Import the required functionality from the libraries. */
import { BaseRequest, BaseRequestHandler, CrtRequestHandler, HandlerChainService } from "@creatio-devkit/common";
import { SomeRequestNameRequest } from "./some-request-name.request";
/* Add the CrtRequestHandler decorator to the SomeHandler1NameHandler class. */
@CrtRequestHandler({
type: 'usr.SomeHandler1NameHandler',
requestType: 'crt.HandleViewModelInitRequest',
scopes: ['CodeOfSomeFreedomUIPage'],
})
export class SomeHandler1NameHandler extends BaseRequestHandler{
public async handle(request: BaseRequest): Promise<unknown> {
/* Update data received from the external web service when Creatio initializes the Freedom UI page. */
await HandlerChainService.instance.process({
type: 'crt.SomeRequestNameRequest',
someParameterName: 'someParameterValue',
$context: request.$context
} as SomeRequestNameRequest);
return this.next?.handle(request);
}
} -
-
-
Register the handlers.
- Open the
app.module.tsfile. - Add the
SomeHandlerNameHandlerandSomeHandler1NameHandlerhandlers to therequestHandlerssection in theCrtModuledecorator. - Import the required functionality from the libraries into the class.
- Save the file.
app.module.ts file/* Import the required functionality from the libraries. */
import { CrtModule } from '@creatio-devkit/common';
import { SomeHandlerNameHandler } from './some-handler-name.handler';
import { SomeHandler1NameHandler } from './some-handler1-name.handler';
...
@CrtModule({
...,
/* Specify that SomeHandlerNameHandler and SomeHandler1NameHandler are request handlers. */
requestHandlers: [
SomeHandlerNameHandler,
SomeHandler1NameHandler
],
})
... - Open the
-
Build the project. To do this, run the
npm run buildcommand at the command line terminal of Microsoft Visual Studio Code.
As a result, Microsoft Visual Studio Code will add the build to the dist directory of the Angular project. The build will have the <%projectName%> name specified on step 1.
5. Add the request implemented using remote module to the Freedom UI page
-
Repeat steps 2-7 of the procedure to implement custom UI component using remote module.
-
Change the property value of the corresponding component in the
viewConfigDiffschema section to request configuration object.- Enter the request name in the
requestproperty. - Enter the configuration object of parameters in the
paramsproperty.
viewConfigDiff schema sectionviewConfigDiff: /**SCHEMA_VIEW_CONFIG_DIFF*/[
{
"operation": "insert",
"name": "SomeButtonName",
"values": {
...,
"clicked": {
/* Bind the sending of the custom request to the button click event. */
"request": "crt.SomeRequestNameRequest",
"params": {
"someParameterName": "someParameterValue"
}
}
},
...
},
...
]/**SCHEMA_VIEW_CONFIG_DIFF*/, - Enter the request name in the
-
Save the changes.
As a result, Creatio will add the request to the Freedom UI page. When an event initiates a request, the related handler will be executed.
See also
Creatio front-end architecture
Custom UI component implemented using remote module