Call a custom web service from the front-end
This functionality is available for Creatio 8.1.4 and later.
To implement the example:
- Implement a custom web service. Read more >>>
- Set up the page UI. Read more >>>
- Implement the calling of the web service. Read more >>>
Add a Show contact ID button to the base contact page. The button calls the UsrReceiveContactDataService
custom web service that uses cookie-based authentication and returns the ID of the current contact by its name. Creatio must display the data in a dialog window.
1. Implement a custom web service
Instructions: Implement a custom web service that uses cookie-based authentication.
2. Set up the page UI
-
Open form page in the Freedom UI Designer.
For this example, open the Contacts form page in the Customer 360 app.
-
Add a button.
For this example, add the button that calls the
UsrReceiveContactDataService
custom web service.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 calls the
UsrReceiveContactDataService
custom web serviceTitle
Show contact ID
Style
Focus
-
-
Save the changes.
3. Implement the calling of the web service
Configure the business logic in the Client Module Designer. For this example, implement the calling of the web service.
-
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 theHttpClientService
service that sends HTTP requests.AMD module dependencies/* Declare the AMD module. */
define("Contacts_FormPage", /**SCHEMA_DEPS*/["@creatio-devkit/common"] /**SCHEMA_DEPS*/, function/**SCHEMA_ARGS*/(sdk)/**SCHEMA_ARGS*/ {
return {
...
}
}); -
Add an attribute.
- Go to the
viewModelConfigDiff
schema section →values
configuration object. - Add a
ContactID
attribute that stores data of the contact ID.
viewModelConfigDiff schema sectionviewModelConfigDiff: /**SCHEMA_VIEW_MODEL_CONFIG_DIFF*/[
{
"operation": "merge",
"path": [
"attributes",
...,
],
"values": {
...,
/* The attribute that stores data of the contact ID. */
"ContactID": {},
}
},
...
]/**SCHEMA_VIEW_MODEL_CONFIG_DIFF*/, - Go to the
-
Set up how to handle the action executed on button click.
- Go to the
viewConfigDiff
schema section →ShowContactIdButton
element. - Bind the sending of the base
crt.ShowDialog
request to theclicked
button event.
viewConfigDiff schema sectionviewConfigDiff: /**SCHEMA_VIEW_CONFIG_DIFF*/[
/* Button that calls the "UsrReceiveContactDataService" custom web service. */
{
"operation": "insert",
"name": "ShowContactIdButton",
"values": {
...,
"clicked": {
/* Bind the sending of the base request to the "clicked" button event. */
"request": "crt.ShowDialog"
}
},
...
}
]/**SCHEMA_VIEW_CONFIG_DIFF*/, - Go to the
-
Implement the base request handler.
-
Go to the
handlers
schema section. -
Add a custom implementation of the
crt.ShowDialog
base request handler.- Create an instance of the HTTP client from
@creatio-devkit/common
. - Specify the URL to retrieve the contact information by the specified name. Use the
UsrReceiveContactDataService
custom web service. - Retrieve the contact name from the response and specify the contact name in the
ContactName
variable. - Send a
GET
request. - Retrieve the contact ID from the response and specify the contact ID in the
ContactID
attribute. - Create an instance of the dialog service from
@creatio-devkit/common
. - Display the response from the
UsrReceiveContactDataService
custom web service in the dialog window.
- Create an instance of the HTTP client from
handlers schema sectionhandlers: /**SCHEMA_HANDLERS*/[
{
request: "crt.ShowDialog",
/* The custom implementation of the system request handler. */
handler: async (request, next) => {
/* Create an instance of the HTTP client from "@creatio-devkit/common." */
const httpClientService = new sdk.HttpClientService();
/* Specify the URL to retrieve the contact information by the specified name. Use the "UsrReceiveContactDataService" web service. */
const endpoint = "/rest/UsrReceiveContactDataService/GetContactIdByName?Name=";
/* Retrieve the contact name from the response and specify the contact name in the "ContactName" variable. */
const ContactName = await request.$context.PDS_Name_bckhanw;
/* Send a GET request. The HTTP client converts the response body from JSON to a JS object automatically. */
const response = await httpClientService.get(`${endpoint}`+ ContactName);
/* Retrieve the contact ID from the response and specify the contact ID in the "ContactID" attribute. */
request.$context.ContactID = response?.body?.GetContactIdByNameResult;
/* Create an instance of the dialog service from "@creatio-devkit/common." */
const dialogService = new sdk.DialogService();
/* Display the response from the "UsrReceiveContactDataService" web service in the dialog window. */
const result = await dialogService.open({
message: request.$context.ContactID,
actions: [
{
key: 'Yes',
config: {
color: 'primary',
caption: 'Close',
}
},
]
});
/* Call the next handler if it exists and return its result. */
return next?.handle(request);
},
},
] /**SCHEMA_HANDLERS*/, -
-
Save the changes.
View the result
- Open the Contacts section.
- Open a contact that has an arbitrary name. For example, "Alice Phillips."
- Click Show contact ID.
As a result, Creatio will display the dialog window that includes the ID of the current contact. View the result >>>
Source code
- UsrReceiveContactDataService
- Contacts_FormPage
namespace Terrasoft.Configuration.UsrReceiveContactDataServiceNamespace {
using System;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using Terrasoft.Core;
using Terrasoft.Web.Common;
using Terrasoft.Core.Entities;
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class UsrReceiveContactDataService: BaseService {
/* The method that returns the contact ID by the contact name. */
[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
public string GetContactIdByName(string Name) {
/* The default result. */
var result = "";
/* The EntitySchemaQuery instance that accesses the "Contact" database table. */
var esq = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "Contact");
/* Add columns to the query. */
var colId = esq.AddColumn("Id");
var colName = esq.AddColumn("Name");
/* Filter the query data. */
var esqFilter = esq.CreateFilterWithParameters(FilterComparisonType.Equal, "Name", Name);
esq.Filters.Add(esqFilter);
/* Retrieve the query results. */
var entities = esq.GetEntityCollection(UserConnection);
/* If the service receives data. */
if (entities.Count > 0) {
/* Return the "Id" column value of the first query result record. */
result = entities[0].GetColumnValue(colId.Name).ToString();
/* You can also use the following option.
result = entities[0].GetTypedColumnValue<string>(colId.Name); */
}
/* Return the results. */
return result;
}
}
}
/* Declare the AMD module. */
define("Contacts_FormPage", /**SCHEMA_DEPS*/["@creatio-devkit/common"]/**SCHEMA_DEPS*/, function/**SCHEMA_ARGS*/(sdk)/**SCHEMA_ARGS*/ {
return {
viewConfigDiff: /**SCHEMA_VIEW_CONFIG_DIFF*/[
{
"operation": "remove",
"name": "AddressSearchFilter",
"properties": [
"targetAttributes"
]
},
{
"operation": "merge",
"name": "AddressSearchFilter",
"values": {
"_filterOptions": {
"expose": [
{
"attribute": "AddressSearchFilter_AddressList",
"converters": [
{
"converter": "crt.SearchFilterAttributeConverter",
"args": [
"AddressList"
]
}
]
}
],
"from": [
"AddressSearchFilter_SearchValue",
"AddressSearchFilter_FilteredColumnsGroups"
]
}
}
},
{
"operation": "remove",
"name": "CareerSearchFilter",
"properties": [
"targetAttributes"
]
},
{
"operation": "merge",
"name": "CareerSearchFilter",
"values": {
"_filterOptions": {
"expose": [
{
"attribute": "CareerSearchFilter_CareerList",
"converters": [
{
"converter": "crt.SearchFilterAttributeConverter",
"args": [
"CareerList"
]
}
]
}
],
"from": [
"CareerSearchFilter_SearchValue",
"CareerSearchFilter_FilteredColumnsGroups"
]
}
}
},
/* Button that calls the "UsrReceiveContactDataService" custom web service. */
{
"operation": "insert",
"name": "ShowContactIdButton",
"values": {
"type": "crt.Button",
"caption": "#ResourceString(ShowContactIdButton_caption)#",
"color": "accent",
"disabled": false,
"size": "large",
"iconPosition": "only-text",
"visible": true,
"clicked": {
/* Bind the sending of the base request to the "clicked" button event. */
"request": "crt.ShowDialog"
}
},
"parentName": "ActionButtonsContainer",
"propertyName": "items",
"index": 1
}
]/**SCHEMA_VIEW_CONFIG_DIFF*/,
viewModelConfigDiff: /**SCHEMA_VIEW_MODEL_CONFIG_DIFF*/[
{
"operation": "merge",
"path": [
"attributes"
],
"values": {
"PDS_Name_bckhanw": {
"modelConfig": {
"path": "PDS.Name"
}
}
}
},
{
"operation": "merge",
"path": [
"attributes",
"AddressList",
"modelConfig"
],
"values": {
"filterAttributes": [
{
"name": "AddressList_PredefinedFilter",
"loadOnChange": true
},
{
"name": "AddressSearchFilter_AddressList",
"loadOnChange": true
}
],
/* The attribute that stores data of the contact ID. */
"ContactID": {},
}
},
{
"operation": "merge",
"path": [
"attributes",
"CareerList",
"modelConfig"
],
"values": {
"filterAttributes": [
{
"name": "CareerSearchFilter_CareerList",
"loadOnChange": true
}
]
}
}
]/**SCHEMA_VIEW_MODEL_CONFIG_DIFF*/,
modelConfigDiff: /**SCHEMA_MODEL_CONFIG_DIFF*/[]/**SCHEMA_MODEL_CONFIG_DIFF*/,
handlers: /**SCHEMA_HANDLERS*/[
{
request: "crt.ShowDialog",
/* The custom implementation of the system request handler. */
handler: async (request, next) => {
/* Create an instance of the HTTP client from "@creatio-devkit/common." */
const httpClientService = new sdk.HttpClientService();
/* Specify the URL to retrieve the contact information by the specified name. Use the "UsrReceiveContactDataService" web service. */
const endpoint = "/rest/UsrReceiveContactDataService/GetContactIdByName?Name=";
/* Retrieve the contact name from the response and specify the contact name in the "ContactName" variable. */
const ContactName = await request.$context.PDS_Name_bckhanw;
/* Send a GET request. The HTTP client converts the response body from JSON to a JS object automatically. */
const response = await httpClientService.get(`${endpoint}`+ ContactName);
/* Retrieve the contact ID from the response and specify the contact ID in the "ContactID" attribute. */
request.$context.ContactID = response?.body?.GetContactIdByNameResult;
/* Create an instance of the dialog service from "@creatio-devkit/common." */
const dialogService = new sdk.DialogService();
/* Display the response from the "UsrReceiveContactDataService" web service in the dialog window. */
const result = await dialogService.open({
message: request.$context.ContactID,
actions: [
{
key: 'Yes',
config: {
color: 'primary',
caption: 'Close',
}
},
]
});
/* 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*/
};
});