CreateBusinessService() from Trigger
I'm trying to send a message into a production on change a persistent object.
And when I change an object using SQL - I get an error,
when change the object using objectscript - I get a message (but not an error or exception)
But the most interesting - everything works fine (the object has been changed, and business service receives data)
Persistent class
Class Demo.Person Extends%Persistent
{
Property Name As%String;Property Status As%Integer;
Trigger UpdateTrigger [ Event = UPDATE, Foreach = row/object, Time = AFTER ]
{
if ( ({Status*O}=0)&&({Status*N}=1) ) {
do##class(Demo.Person).SendToProduction({Name})
}
}
ClassMethod SendToProduction(name As%String) As%Status
{
set sc = ##class(Ens.Director).CreateBusinessService("DemoService", .service)
return:$$$ISERR(sc) sc
set sc = service.ProcessInput(##class(Ens.StringContainer).%New(name))
return sc
}
}Business Service
Spoiler
Class Demo.DemoService Extends Ens.BusinessService
{
Method OnProcessInput(pInput As Ens.StringContainer, pOutput As%RegisteredObject) As%Status
{
$$$TRACE(pInput.StringValue)
return$$$OK
}
}Production
Spoiler
Class Demo.DemoProduction Extends Ens.Production
{
XData ProductionDefinition
{
<Production Name="Demo.DemoProduction" TestingEnabled="true" LogGeneralTraceEvents="false">
<Description></Description>
<ActorPoolSize>2</ActorPoolSize>
<Item Name="DemoService" Category="" ClassName="Demo.DemoService" PoolSize="0" Enabled="true" Foreground="false" Comment="" LogTraceEvents="true" Schedule="">
</Item>
</Production>
}
}
SQL Query
UPDATE Demo.Person
SETStatus = 1WHEREID = 1An error executing this query
[SQLCODE: <-400>:<Fatal error occurred>] [%msg: <Unexpected error occurred: <COMMAND>%0Ac+1^%sqlcq.TEST.cls3.1 *NoTransaction>]
In terminal
TEST>set pers = ##class(Demo.Person).%OpenId(1)
TEST>write pers.Status
0
TEST>set pers.Status = 1
TEST>set sc = pers.%Save()
07:02:44.836:Demo.DemoService: aaa
07:02:44.840:Demo.DemoService: 1open user transaction found after OnProcessInput(); committing before proceeding
TEST>write sc
11. Why I see these messages?
"07:02:44.836:Demo.DemoService: aaa" - it's a $$$TRACE from BusinessService
"07:02:44.840:Demo.DemoService: 1 open user transaction found after OnProcessInput(); committing before proceeding" - It's a warning in Production Log.
2. How to work correctly with Interoperability from Triggers?
Now, I use job command from trigger, as a workaround
job ##class(Demo.Person).SendToProduction({Name})Comments
I miss this in your Trigger code:
%ok: A variable used only in trigger code.
If trigger code succeeds, it sets %ok=1.
If trigger code fails, it sets %ok=0.
Robert,
%ok sets by IRIS
the developer only need to set %ok =0 to abort execution of the trigger and roll back the operation