Creatio development guide
PDF
This documentation is valid for Creatio version 7.16.0. We recommend using the newest version of Creatio documentation.

Calling configuration services with ServiceHelper

Glossary Item Box

Case description

Add a button calling the configuration web service to the new contact adding page. As a result, the information window of the page will display the result returned by the web service method.

The procedure for calling a web service from a client JavaScript code is available in the "Calling configuration services with ServiceHelper" article.

Source code

You can download the package with case implementation using the following link.

Case implementation algorithm

1. Creating configuration service

The current case uses the web service from the “Creating a user configuration service” article. The “Method” parameter of the “WebInvoke” attribute is changed for POST in the service.

Service source code:

namespace Terrasoft.Configuration.UsrConfigurationService
{
    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 UsrConfigurationService: BaseService
    {
        // Link to the UserConnection instance needed for addressing the database.
        private SystemUserConnection _systemUserConnection;
        private SystemUserConnection SystemUserConnection {
            get {
                return _systemUserConnection ?? (_systemUserConnection = (SystemUserConnection)AppConnection.SystemUserConnection);
            }
        }
        
        // Method returning the contact identifier by name.
        [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,
        ResponseFormat = WebMessageFormat.Json)]
        public string GetContactIdByName(string Name){
            // Default result.
            var result = "";
            // The EntitySchemaQuery instance, addressing the Contact database table.
            var esq = new EntitySchemaQuery(SystemUserConnection.EntitySchemaManager, "Contact");
            // Adding columns to query.
            esq.AddColumn("Id");
            var colName = esq.AddColumn("Name");
            // Query data filtering.
            var esqFilter = esq.CreateFilterWithParameters(FilterComparisonType.Equal, "Name", Name);
            esq.Filters.Add(esqFilter);
            // Receiving the query results.
            var entities = esq.GetEntityCollection(SystemUserConnection);
            // If data are received.
            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 folowing variant:
                // result = entities[0].GetTypedColumnValue<string>(colId.Name);
            }
            // Return result.
            return result;
        }
    }
}

2. Creating a replacing edit page

Create a replacing client module and specify [Display schema – Contact card] (ContactPageV2) as the parent object in the custom package (Fig. 1). Creating a replacing page is covered in the “Creating a custom client module schema” article.

Add the ServiceHelper module as a dependency to declaring the edit page module.

2. Adding a button to the edit page

Adding a button to the edit page is described in the “How to add a button to an edit page in the new record add mode” and “How to add the button on the edit page in the combined mode” articles.

Add a localizable string with the button caption to the replacing module schema of the contact edit page, for example:

  • [Name] – “GetServiceInfoButtonCaption”
  • [Value] – “Call service”

3. Adding the button handler and calling the web service method

Use the callService() method of the ServiceHelper module to call the web service and pass the following values as parameters:

  • name of the configuration service class (UsrCustomConfigurationService)
  • name of the called service method (GetContactIdByName)
  • the object with the initialized incoming parameters for the service method (serviceData)
  • the callback function where processing of service results is executed
  • execution context

The source code of the edit page replacing module:

define("ContactPageV2", ["ServiceHelper"],
    function(ServiceHelper) {
        return {
            // Name of the edit page object schema.
            entitySchemaName: "Contact",
            details: /**SCHEMA_DETAILS*/{}/**SCHEMA_DETAILS*/,
            // View model methods of the edit page.
            methods: {
                // Verifies if the [Full name] field is populated on the page.
                isContactNameSet: function() {
                    return this.get("Name") ? true : false;
                },
                // Handler-method of clicking the button.
                onGetServiceInfoClick: function() {
                    var name = this.get("Name");
                    // Object initializing incoming parameters for the service method.
                    var serviceData = {
                        // The property name corresponds to the incoming parameter name of the service method.
                        Name: name
                    };
                    // Calling the web service and processing the results.
                    ServiceHelper.callService("UsrConfigurationService", "GetContactIdByName",
                        function(response) {
                            var result = response.GetContactIdByNameResult;
                            this.showInformationDialog(result);
                        }, serviceData, this);
                }
            },
            diff: /**SCHEMA_DIFF*/[
                // Metadata for adding a custom button to a page.
                {
                    // Executing the operation of adding the element to page.
                    "operation": "insert",
                    // Name of the parent control element where the button is added.
                    "parentName": "LeftContainer",
                    // The button is added to the control element collection
                    // of the parent element (whose metaname is specified in parentName).
                    "propertyName": "items",
                    // Name of the added button.
                    "name": "GetServiceInfoButton",
                    // Additional field property.
                    "values": {
                        // The added element type - button.
                        itemType: Terrasoft.ViewItemType.BUTTON,
                        // Binding the button caption to the localizable schema string. 
                        caption: {bindTo: "Resources.Strings.GetServiceInfoButtonCaption"},
                        // Binding the handler-method of clicking the button.
                        click: {bindTo: "onGetServiceInfoClick"},
                        // Binding the "enabled" property ot the button.
                        enabled: {bindTo: "isContactNameSet"},
                        // Field location setup.
                        "layout": {"column": 1, "row": 6, "colSpan": 2, "rowSpan": 1}
                    }
                }
            ]/**SCHEMA_DIFF*/
        };
    });

After you save the schema and update the application page, the [Call service] button will appear on the contact edit page. When you click the button, the configuration service method will be called (fig. 1).

Fig. 1. Case implementation result

See also:

© Creatio 2002-2020.

Did you find this information useful?

How can we improve it?