Skip to main content
Version: 8.1

Run the business process using a web service

Level: advanced
Example

Run custom business processes that create a new contact and read all Creatio contacts. Run the processes from the browser address bar and console app. Use the ProcessEngineService.svc web service to run the processes.

1. Create a process that adds a contact

note

Learn more about the specifics of and best practices for creating business processes in Creatio in the user documentation: BPM tools.

  1. Open the Configuration section and select a custom package to add the business process.

  2. Open the Process Designer.

  3. Fill out the business process properties.

    • Set Name to "Add New External Contact."
    • Set Code to "UsrAddNewExternalContact."

    Use default values for other properties.

  4. Add business process parameters.

    The process uses parameters to pass the data of the new contact: name and phone number.

    View the process parameter values to fill out in the table below.

    Process parameter values

    Title

    Cod

    Data type

    ContactName parameter

    "Contact Name"

    "ContactName"

    "Text (50 characters)"

    ContactPhone parameter

    "Contact Phone"

    "ContactPhone"

    "Text (50 characters)"

  5. Add the ScriptTask element.

    1. Fill out the element properties.

      • Set Name to "Add contact."
      • Set Code to "ScriptTaskAddContact."
    2. Implement the mechanism that adds a new contact.

      To edit the script code, double-click a diagram element. This opens a window on the element setup area. Enter and edit the source code in the window.

      ScriptTaskAddContact
      /* Creates an instance of the [Contact] object schema. */ 
      var schema = UserConnection.EntitySchemaManager.GetInstanceByName("Contact");
      /* Creates an instance of the new object. */
      var entity = schema.CreateEntity(UserConnection);
      /* Sets the default values for the object columns. */
      entity.SetDefColumnValues();
      string contactName = Get<string>("ContactName");
      string contactPhone = Get<string>("ContactPhone");
      /* Sets the [Name] column values from the process parameter. */
      entity.SetColumnValue("Name", contactName);
      /* Sets the [Phone] column values from the process parameter. */
      entity.SetColumnValue("Phone", contactPhone);
      /* Saves the new contact. */
      entity.Save();
      return true;
  6. Click Save.

2. Create a process that reads all Creatio contacts

The business process that creates the index of all Creatio contacts includes a single ScriptTask element as well.

To create a process that reads all Creatio contacts:

  1. Open the Configuration section and select a custom package to add the business process.

  2. Open the Process Designer.

  3. Fill out the business process properties.

    • Set Name to "Get All Contacts."
    • Set Code to "UsrGetAllContacts."

    Use default values for other properties.

  4. Add the business process parameter.

    Fill out the values of the business process parameter.

    • Set Title to "Contact List."
    • Set Code to "ContactList."
    • Set Data type to "Unlimited length text."

    The parameter returns the JSON index of all Creatio contacts.

  5. Add the ScriptTask element.

    1. Fill out the element properties.

      • Set Name to "Get all contacts."
      • Set Code to "ScriptTaskGetAllContacts."
    2. Implement the mechanism that reads the Creatio contacts.

      To edit the script code, double-click the diagram. This opens a window on the element setup area. Enter and edit the source code in the window.

      ScriptTaskGetAllContacts
      /* Creates an EntitySchemaQuery instance. */ 
      EntitySchemaQuery query = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "Contact");
      /* Flag the [Id] primary column as required to select. */
      query.PrimaryQueryColumn.IsAlwaysSelect = true;
      /* Adds columns to the query. */
      query.AddColumn("Name");
      query.AddColumn("Phone");
      /* Retrieve the resulting collection. */
      var entities = query.GetEntityCollection(UserConnection);
      /* Creates the contact list to serialize in JSON. */
      List<object> contacts = new List<object>();
      foreach (var item in entities) {
      var contact = new {
      Id = item.GetTypedColumnValue<Guid>("Id"),
      Name = item.GetTypedColumnValue<string>("Name"),
      Phone = item.GetTypedColumnValue<string>("Phone")
      };
      contacts.Add(contact);
      }
      /* Saves the contact collection that is serialized in JSON to the ContactList parameter. */
      string contactList = JsonConvert.SerializeObject(contacts);
      Set<string>("ContactList", contactList);
      return true;
  6. Click Save.

Outcome of the example

  1. Run business processes from the browser address bar.

    1. Run the process that adds a contact. To do this, enter the following URL in the browser address bar.

      URL that runs the process that adds a contact
      http[s]://[CreatioURL]/0/ServiceModel/ProcessEngineService.svc/UsrAddNewExternalContact/Execute?ContactName=John Johanson&ContactPhone=1 111 111 1111

      As a result, the process will add a contact to Creatio.

      Important

      A contact is added every time the service receives a request successfully. If you run multiple queries that contain the same parameters, the service adds multiple duplicate contacts.

    2. Run the process that reads all Creatio contacts. To do this, enter the following URL in the browser address bar.

      URL that runs the process that reads all Creatio contacts
      http[s]://[Creatio URL]/0/ServiceModel/ProcessEngineService.svc/UsrGetAllContacts/Execute?ResultParameterName=ContactList

      As a result, the browser window will display the JSON object that contains the contact collection.

  2. Run business processes from the console app.

    View the complete source code of the custom console app that runs business processes via the ProcessEngineService.svc service on GitHub.

    1. Perform the authentication. To do this, use the AuthService.svc authentication service. Use the example available in a separate article: Implement authentication using C#.

    2. Add a string field that contains the base service URL to the source code of the Program class.

      URL that creates requests to the ProcessEngineService.svc service
      private const string processServiceUri = baseUri + @"/0/ServiceModel/ProcessEngineService.svc/";
    3. Add a GET method that runs the business process that creates a contact to the source code of the Program class.

      GET method that runs the business process
      public static void AddContact(string contactName, string contactPhone) { 
      /* Generates a request URL. */
      string requestString = string.Format(processServiceUri + "UsrAddNewExternalContact/Execute?ContactName={0}&ContactPhone={1}", contactName, contactPhone);
      /* Creates an HTTP request. */
      HttpWebRequest request = HttpWebRequest.Create(requestString) as HttpWebRequest;
      request.Method = "GET";
      request.CookieContainer = AuthCookie;
      /* Executes the request and analyzes the HTTP response. */
      using (var response = request.GetResponse()) {
      /* Displays the properties of the HTTP response if the service returns an empty string. */
      Console.WriteLine(response.ContentLength);
      Console.WriteLine(response.Headers.Count);
      }
      }
    4. Add GET method that runs the business process that reads all Creatio contacts to the Program class source code.

      GET method that runs the business process
      public static void GetAllContacts() { 
      /* Generates a request URL. */
      string requestString = processServiceUri + "UsrGetAllContacts/Execute?ResultParameterName=ContactList";
      HttpWebRequest request = HttpWebRequest.Create(requestString) as HttpWebRequest;
      request.Method = "GET";
      request.CookieContainer = AuthCookie;
      /* Creates an HTTP request. */
      using (var response = request.GetResponse()) {
      /* Executes the request and displays the result. */
      using (var reader = new StreamReader(response.GetResponseStream())) {
      string responseText = reader.ReadToEnd();
      Console.WriteLine(responseText);
      }
      }
      }
    5. Call the added methods in the main app method after a successful authentication.

      Call the methods
      static void Main(string[] args) { 
      if (!TryLogin("Supervisor", "Supervisor")) {
      Console.WriteLine("Wrong login or password. Application will be terminated.");
      }
      else {
      try {
      /* Calls methods that runs business processes. */
      AddContact("John Johanson", "+1 111 111 1111");
      GetAllContacts();
      }
      catch (Exception) {
      /* Handle exceptions. */
      throw;
      }
      };
      Console.WriteLine("Press ENTER to exit...");
      Console.ReadLine();
      }

Resources

GitHub (example of a console application that runs business processes)