Implement a custom request handler using remote module
To implement the example:
- Create an Angular project. Read more >>>
- Install
npmpackages. Read more >>> - Create a custom request. Read more >>>
- Create a custom service. Read more >>>
- Create custom request handlers. Read more >>>
- Add the request to the Freedom UI page. Read more >>>
Add custom request handlers to the custom request page. The request handlers execute the following actions:
- Display current date and time as a Label component.
- Change the date and time value after a user clicks the Refresh date and time button.
Implement handlers using a remote module created in Angular framework.
1. Create an Angular project
To create an Angular project, follow the instructions: Create an Angular project.
For this example:
<%projectName%>macro is set to "sdk_custom_request_handler."<%vendorPrefix%>macro is set to "usr."
As a result, an Angular project to develop a custom request handler using a remote module will be added. The project contains:
- A
RemoteEntryDefinitionobject exported from the "src/main.ts" file. - The runtime feature files used to implement request handler.
- The optional design feature files.
- The "sdk_custom_request_handler-runtime" runtime feature ID.
2. Install npm packages
To install npm packages, follow the instructions: Install npm packages.
As a result, required npm packages are installed.
3. Create a custom request
-
Ensure the project includes the "AGENTS.md" file. Instructions: Create a custom Freedom UI component (step 1).
-
Run the
ng g class features/runtime/requests/update-current-time.requestcommand in the Visual Studio Code terminal to create an Angular class in the project. This adds theUpdateCurrentTimeRequestclass files to the "src/app/features/runtime/requests" project directory. -
Implement the request.
- Open the "update-current-time.request.ts" file.
- 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.
"update-current-time.request.ts" file/* Import the required functionality from the libraries. */import { BaseRequest } from "@creatio-devkit/common";export class UpdateCurrentTimeRequest extends BaseRequest{/* The type and parameters of the request. */public dateTime!: string;}
4. Create a custom service
-
Run the
ng g class features/runtime/services/date-time.servicecommand in the Visual Studio Code terminal to create an Angular class in the project. This adds theDateTimeServiceclass files to the "src/app/features/runtime/services" project directory. -
Implement the service that receives current date and time.
- Open the "date-time.service.ts" file.
- Flag the
DateTimeServiceclass using the@Injectabledecorator. - Implement the
getCurrentDateTime()method that receives current date and time. - Import the required functionality from the libraries into the class.
- Save the file.
"date-time.service.ts" file/* Import the required functionality from the libraries. */import { formatDate } from "@angular/common";import { Injectable } from "@angular/core";@Injectable({providedIn: 'root',})export class DateTimeService {/* Receive current date and time. */public getCurrentDateTime(dateTime: string): Promise<{dateTime: Record<string, unknown>}> {/* Return current date and time. */return Promise.resolve({dateTime: { dateTime: formatDate(new Date(), 'medium', 'en')}});}}
5. Create custom request handlers
-
Create a custom handler that displays current date and time on the Freedom UI page.
-
Set up the Freedom UI page.
-
Repeat steps 1-4 of the instructions: Add the custom Freedom UI component to the Freedom UI page. For this example, create a Requests app.
-
Add a label to display the current date and time.
-
Drag the Label component to the canvas.
-
Fill out the component parameters.
Parameter
Value
Style
Body
Text color
#757575 Element code
UsrDateAndTime
-
-
Add a button that refreshes current date and time.
-
Drag the Button component to the canvas.
-
Fill out the component parameters.
Parameter
Value
Title
Refresh date and time
Style
Focus
Element code
UsrRefreshDateAndTime
-
-
Click
→ Source code to open the source code of the Freedom UI page. -
Add an attribute.
- Go to the
viewModelConfigDiffschema section →valuesconfiguration object. - Add a
CurrentDateTimeattribute that stores data about the current date and time.
viewModelConfigDiff schema sectionviewModelConfigDiff: /**SCHEMA_VIEW_MODEL_CONFIG_DIFF*/[{"operation": "merge","path": ["attributes"],"values": {/* The attribute that stores the current date and time. */"CurrentDateTime": {}}}]/**SCHEMA_VIEW_MODEL_CONFIG_DIFF*/, - Go to the
-
Bind an attribute to the label.
- Go to the
viewConfigDiffschema section →UsrDateAndTimeelement. - Bind the
CurrentDateTimeattribute to thecaptionproperty.
viewConfigDiff schema sectionviewConfigDiff: /**SCHEMA_VIEW_CONFIG_DIFF*/[{"operation": "insert","name": "UsrDateAndTime","values": {/* Bind the CurrentDateTime attribute to the caption property. */"caption": "$CurrentDateTime",},}]/**SCHEMA_VIEW_CONFIG_DIFF*/, - Go to the
-
Click Save.
-
-
Run the
ng g class features/runtime/request-handlers/update-current-time.handlercommand in the Visual Studio Code terminal to create an Angular class in the project. This adds theUpdateCurrentTimeHandlerclass files to the "src/app/features/runtime/request-handlers" project directory. -
Implement the handler.
-
Open the "update-current-time.handler.ts" file.
-
Add the configuration object that declares the request handler.
- Set the
typeproperty to "usr.UpdateCurrentTimeHandler." - Set the
requestTypeproperty to "crt.UpdateCurrentTimeRequest."
- Set the
-
Flag the
UpdateCurrentTimeHandlerclass using the@CrtRequestHandlerdecorator. -
Inherit the
BaseRequestHandlerclass from the@creatio-devkit/commonlibrary. -
Implement the handling of the request result.
-
Generate the value to display on the Freedom UI page.
-
Import the required functionality from the libraries into the class.
-
Save the file.
"update-current-time.handler.ts" file/* Import the required functionality from the libraries. */import {BaseRequestHandler,CrtRequestHandler} from "@creatio-devkit/common";import { DateTimeService } from "../services/date-time.service";import {UpdateCurrentTimeRequest} from "../requests/update-current-time.request";/* Add the CrtRequestHandler decorator to the UpdateCurrentTimeHandler class. */@CrtRequestHandler({type: 'usr.UpdateCurrentTimeHandler',requestType: 'crt.UpdateCurrentTimeRequest',})export class UpdateCurrentTimeHandler extends BaseRequestHandler{constructor(private _dateTimeService: DateTimeService) {super();}public async handle(request: UpdateCurrentTimeRequest): Promise<unknown> {const result = await this._dateTimeService.getCurrentDateTime(request.dateTime);const dateTime = result.dateTime[request.dateTime] ?? 0/* Generate the value to display on the Freedom UI page. */request.$context['CurrentDateTime'] = dateTime;return dateTime;}} -
-
-
Create a custom handler that is executed when Creatio initializes a Freedom UI page.
-
Run the
ng g class features/runtime/request-handlers/page-init-current-time.handlercommand in the Visual Studio Code terminal to create an Angular class in the project. This adds thePageInitCurrentTimeHandlerclass files to the "src/app/features/runtime/request-handlers" project directory. -
Implement the handler.
-
Open the "page-init-current-time.handler.ts" file.
-
Add the configuration object that declares the request handler.
- Set the
typeproperty to "usr.PageInitCurrentTimeHandler." - Set the
requestTypeproperty to "crt.HandleViewModelInitRequest." - Set the
scopesproperty to "UsrRequests_FormPage."
- Set the
-
Flag the
PageInitCurrentTimeHandlerclass using the@CrtRequestHandlerdecorator. -
Inherit the
BaseRequestHandlerclass from the@creatio-devkit/commonlibrary. -
Implement the update of the current date and time when Creatio initializes the Freedom UI page.
-
Import the required functionality from the libraries into the class.
-
Save the file.
"page-init-current-time.handler.ts" file/* Import the required functionality from the libraries. */import {BaseRequest,BaseRequestHandler,CrtRequestHandler,HandlerChainService} from "@creatio-devkit/common";import {UpdateCurrentTimeRequest} from "../requests/update-current-time.request";/* Add the CrtRequestHandler decorator to the PageInitCurrentTimeHandler class. */@CrtRequestHandler({type: 'usr.PageInitCurrentTimeHandler',requestType: 'crt.HandleViewModelInitRequest',scopes: ['UsrRequests_FormPage'],})export class PageInitCurrentTimeHandler extends BaseRequestHandler{public async handle(request: BaseRequest): Promise<unknown> {/* Update the current date and time when Creatio initializes the Freedom UI page. */await HandlerChainService.instance.process({type: 'crt.UpdateCurrentTimeRequest',dateTime: 'dateTime',$context: request.$context} as UpdateCurrentTimeRequest);return this.next?.handle(request);}} -
-
-
Register the handlers. The template already contains the runtime feature activation and
bootstrapCrtModule()call. Extend the existing runtime feature files to register the request handlers.-
Open the "runtime-feature.module.ts" file.
-
Add the
UpdateCurrentTimeHandlerandPageInitCurrentTimeHandlerhandlers to therequestHandlerssection in the@CrtModuledecorator."runtime-feature.module.ts" file/* Import the required functionality from the libraries. */import { ApplicationRef, DoBootstrap, NgModule } from '@angular/core';import { BrowserModule } from '@angular/platform-browser';import { CrtModule } from '@creatio-devkit/common';import {PageInitCurrentTimeHandler} from './request-handlers/page-init-current-time.handler';import {UpdateCurrentTimeHandler} from './request-handlers/update-current-time.handler';@CrtModule({requestHandlers: [UpdateCurrentTimeHandler, PageInitCurrentTimeHandler],})@NgModule({declarations: [],imports: [BrowserModule],})export class RuntimeFeatureModule implements DoBootstrap {ngDoBootstrap(_appRef: ApplicationRef): void {}} -
Open the "runtime.feature-definition.ts" file.
-
Add the public
typeproperty of the handlers to thediscovery.requestHandlerssection."runtime.feature-definition.ts" file/* Import the required functionality from the libraries. */import type {RemoteFeatureDefinition,RemoteDesignerDefinitionsLoadContext} from '@creatio-devkit/common';/*** Root definition for the runtime feature.** Describes the view elements this remote package contributes at runtime* and provides the entry points used to load designer metadata and* activate the feature.*/export const runtimeFeatureDefinition = {id: 'sdk_custom_request_handler-runtime',discovery: {viewElements: [],mobileViewElements: [],requestHandlers: [{type: 'usr.UpdateCurrentTimeHandler',requestType: 'crt.UpdateCurrentTimeRequest',},{type: 'usr.PageInitCurrentTimeHandler',requestType: 'crt.HandleViewModelInitRequest',scopes: ['UsrRequests_FormPage'],},],},loadDesignerDefinitions: (context: RemoteDesignerDefinitionsLoadContext) =>import('./runtime.designer-definitions').then((m) => m.loadRuntimeDesignerDefinitions(context)),activate: () =>import('./runtime.feature-activation').then((m) => m.activateRuntimeFeature()),} satisfies RemoteFeatureDefinition; -
Import the required functionality from the libraries into the files.
-
Save the files.
-
-
Run the
npm run buildcommand in the Visual Studio Code terminal to build the project.
As a result, the build will be added to the "dist" directory of the Angular project. The build will have the "sdk_custom_request_handler" name.
6. Add the request to the Freedom UI page
-
Repeat step 2 of the add the custom Freedom UI component to the Freedom UI page instructions to upload packages to Creatio using the Clio utility.
-
Open the Requests app in the Application Designer.
-
Open the Requests form page.
-
Drag the Button component to the canvas.
-
Click
→ Source code to open the source code of the Freedom UI page. -
Set up how to handle the action executed on button click.
- Go to the
viewConfigDiffschema section →UsrRefreshDateAndTimeelement. - Bind the sending of the
crt.UpdateCurrentTimeRequestrequest to theclickedbutton event.
viewConfigDiff schema sectionviewConfigDiff: /**SCHEMA_VIEW_CONFIG_DIFF*/[{"operation": "insert","name": "UsrRefreshDateAndTime","values": {"clicked": {/* Bind the sending of the crt.UpdateCurrentTimeRequest requestto the button click event. */"request": "crt.UpdateCurrentTimeRequest","params": {"dateTime": "dateTime"}}},},]/**SCHEMA_VIEW_CONFIG_DIFF*/, - Go to the
-
Click Save.
As a result, the custom request handler will be added to the request page.
View the result
- Open the Requests section.
- Create a request that has an arbitrary name. For example, "Vacation."
- Click the Refresh date and time button.
As a result:
- Creatio will display the current date and time on the request page.
- Creatio will change the date and time value after clicking the Refresh date and time button.
Both request handlers are implemented using a remote module created in the Angular framework.