Best Practices for Custom Business Services
Tags: beginners, business, tutorial, programming
Published: 2022-03-27 17:40:02

Business services are powerful components that pull data in from external sources. In most cases, pre-built components do the job, but sometimes you need to code custom business services. There are a few best practices to keep in mind when doing this:

<div style="margin-left: 40px;">
   
</div>

  1. Lean and Mean - Business services should be coded to have minimal processing. This is due to the fact that if any errors occur within the business service, no message will be sent, and thus no trace will be created. This makes it very difficult to troubleshoot. Instead, populate an Ensemble message as quickly as possible and pass it on to the appropriate target. Some would argue that if a stream comes in, the Ensemble message should include one stream property where a business process would then parse that stream. (See [Example 3 in docs](http://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=EFIL_inbound#EFIL_busserv_examples)) Others would say that minimal processing is OK as long as good error checking is included. In either case, keep in mind that data transformations exist and can be used within a business process to do further translation of this data. These transformations should NOT be called from a business service. (See Example 1 in docs)
  2. Configurable Target - In most cases, business services should be coded to have a configurable target instead of hardcoding this into the business service. To do this, you can create a property within the business service class called TargetConfigNames with the type Ens.DataType.ConfigName and use the SETTINGS parameter and place this setting on the Configuration Page.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1dt88h6kfsxrpjx5dwc1.png)
  While the property does not need to be named TargetConfigNames - using this name is in concert with the built-in setting used in HL7 components and doing so, maintains consistency within your components. Using the code above only allows you to select one target. If you need to send the message to multiple components, there are options to make this setting multi-select. Refer to <a href="http://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=EGDV_prog#EGDV_prog_settings">Adding and Removing Settings in documentation </a>for more information on how to do this.
</p>

<p style="margin-left: 40px;">
  You can code the method to handle multiple targets using the following code:
</p>

<p style="margin-left: 40px;">
  <basefont name="Courier New"> <font color="#0000ff">For </font><font color="#808000">iTarget</font><font color="#000000">=1:1:</font><font color="#0000ff">$L</font><font color="#000000">(..</font><font color="#0000ff">TargetConfigNames</font><font color="#000000">, </font><font color="#008000">","</font><font color="#000000">) </font><font color="#800080">{</font><br />     <font color="#0000ff">Set </font><font color="#808000">tOneTarget</font><font color="#000000">=</font><font color="#0000ff">$ZStrip</font><font color="#000000">(</font><font color="#0000ff">$P</font><font color="#000000">(..</font><font color="#0000ff">TargetConfigNames</font><font color="#000000">,</font><font color="#008000">","</font><font color="#000000">,</font><font color="#808000">iTarget</font><font color="#000000">),</font><font color="#008000">"&lt;>W"</font><font color="#000000">)</font><br />      <font color="#0000ff">Continue</font><font color="#000000">:</font><font color="#008000">""</font><font color="#000000">=</font><font color="#808000">tOneTarget</font><br />     <font color="#0000ff">$$$sysTRACE</font><font color="#000000">(</font><font color="#008000">"Sending input Stream ..."</font><font color="#000000">)</font><br />     <font color="#0000ff">set </font><font color="#808000">tSC </font><font color="#000000">= ..</font><font color="#0000ff">SendRequestAsync</font><font color="#000000">(</font><font color="#808000">tOneTarget</font><font color="#000000">, </font><font color="#808000">pRequest</font><font color="#000000">)</font><br /><font color="#800080">}</font>&lt;/p> 
  
  <p style="margin-left: 40px;">
     
  </p>
  
  <p>
    Do you have any other best practices you'd add?
  </p>&lt;/body>&lt;/html>