· Aug 28, 2019

Run code on Business Process start

I want to execute initialization code on the start of my business process.

I thought OnInit is a way to go, but apparently not - ^dbg global remains empty:

Class Test.BP Extends Ens.BusinessProcess
Method OnInit() As %Status
    Set ^dbg = 1
    Quit $$$OK


Discussion (2)1
Log in or sign up to continue


I'm not 100% sure I understand _exactly_ what you're trying to accomplish -- do you want to execute code when the Production starts, or do you want to execute code when the individual Business Process is fired? Either way, I'll explain both in my example below...

Did you associate your class with a particular production or business process? In Studio, I created a New >> Production >> Business Process, and it will ask you for a Package Name (I used TEST) and Class Name (I used StartItUp) and a description (Testorama) - and if you click Finish it will take you right to the graphical Business Process editor. You then need to add an activity to the process, I used "code" and entered

  set ^gfxdbg(+$o(^gfxdbg("z"),-1)+1)=$h

under the 'Code' section. Then I connected the <start> to the <code> and the <code> to the <end> nodes (graphically). I then saved & compiled the .bpl.  After that's saved & compiled, you can press <SHIFT><CTRL><V> to view the code that Studio created, and you'll see this:

/// Testorama
Class TEST.StartItUp Extends Ens.BusinessProcessBPL

/// BPL Definition
XData BPL [ XMLNamespace = "" ]
<process language='objectscript' request='Ens.Request' response='Ens.Response' height='2000' width='2000' >
<sequence xend='324' yend='440' >
<code xpos='204' ypos='244' >
<![CDATA[ set ^gfxdbg(+$o(^gfxdbg("z"),-1)+1)=$h]]>
/// except this comment - I added this manually. Insert extra methods here.

Except... one line near the bottom; I added this manually to show you where to add the next segment of code. In that spot, I added this:

Method OnInit() As %Status
    set ^initdbg(+$o(^initdbg("z"),-1)+1)=$h
    quit $$$OK

ClassMethod OnProductionStart() As %Status
    set ^proddbg(+$o(^proddbg("z"),-1)+1)=$h
    quit $$$OK

I used different globals to highlight the different functions of the methods. Every time that the OnInit() is run, it'll add a new node to ^initdbg, and every time the OnProductionStart() is run, it'll add a new node to ^proddbg.

If you've made it this far... you're still not quite done. :-) You still need to go back to the Management Portal in your production, and click the (+) next to Processes to add a new Business Process.

Under the Business Process Class, click the dropdown and you should now see TEST.StartItUp (scroll to the bottom to see it):

Select that for the BPClass. Give the Process a Name (Below, I used "Fizzle2_Stuff" -- yea, it's a dumb name, but I didn't want to reconfigure _everything_ again just to give it something different. Sorry 'bout that.), and click "Enable Now."  If the process is created and the dot stays green, you're.... still not quite there. But almost! In your command prompt, if you type this:

zw ^proddbg

you _should_ see an entry. This is set every time the production starts -- or more accurately, when the production starts the Business Process. If you type this:

zw ^initdbg

you will not get any output? Why? Because the OnInit() isn't executed on production start - it's executed on "subjob" start. When the Business Process gets some input from a Business Service that uses an InboundAdapter, then it'll populate. I have a test Service that scans a directory on my hard drive (based on the EnsLib.File.InboundAdapter) and can send it to a Business Process. That's set under the "Target Config Names" setting. My example has a _lot_ of testing things that are named weird... so be warned. :-)

Adding Target Config Name to Service.

Hopefully that part's not too confusing... Anyway, once you have the Service pointing to the Process, every time new input goes through the queue on the Service and is sent to the Process, then the OnInit() method fires, and you'll see a new entry in:

zw ^initdbg

Anyway, If this doesn't quite make sense, I apologize and maybe I'll rework the tutorial to make it clearer and highlight every single step it took to get this tested fully... please respond with feedback on how this could be better understood.

If you have any questions, please feel free to ask!

[[ Edited initial paragraph for clarity. ]]