Written by

Developer at Shift Consultoria e Sistemas Ltda
Question Rodolfo Moreira dos Santos · Aug 21, 2019

How to know the ID of the task that called my class?

Hello, I have a task created in my task manager, calling my Business Service class, and it's working fine.
But I need to know the ID of the task, I searched within the class  %SYS.Task.Definition and found nothing related.

Class Example.Tasks Extends %SYS.Task.Definition{Property BusinessService As %String [ InitialExpression = "Example" ];Method OnTask() As %Status{
  SET tSC = $$$OK
  TRY {
  
  SET tSC = ##Class(Ens.Director).CreateBusinessService(..BusinessService,.tBS)
  SET tRequest = ##class(Ens.Request).%New()
  SET tSC = tBS.OnProcessInput(tRequest,.tResponse)
  KILL tBS  
  
  
  CATCH  {
   SET tSC = $System.Status.GetErrorText(e)
  }
  QUIT tSC}}

Comments

Eduard Lebedyuk · Aug 21, 2019

Please elaborate on what do you want to do with ID.

0
Rodolfo Moreira dos Santos  Aug 21, 2019 to Eduard Lebedyuk

We use a REST service to generate the tasks, the ID generated after creating the task is used to link the task to another table with additional parameters.
When executing the task the ID will be used to fetch information from this table.

0
Eduard Lebedyuk  Aug 21, 2019 to Rodolfo Moreira dos Santos

You can add property to the task class which references this table.

Would it help?

0
Lorenzo Scalese · Aug 23, 2019

Hello @Rodolfo Moreira dos Santos,

For retrieve the task ID, I wrote this : 

ClassMethod getTaskId(
    ByRef sc As %Status = {$$$OK},
    className As %String = {..%ClassName(1)}) As %String
{
    Set id = ""
    Set rs = ##class(%Library.ResultSet).%New("%SYS.Task:TaskListDetail")
    Set sc = rs.Execute()
    Quit:$$$ISERR(sc) ""
    While (rs.Next(.sc)) {
        Quit:$$$ISERR(sc)
        if (rs.Get("TaskClass")=className){
            Set id = rs.Get("ID")
            Quit
        }
    }
    Quit id
} 

Hope this help you.

Regards.

Edit : modify this code for your needs (ex : return a list of Id, namespace filter...)

0
Eduard Lebedyuk  Aug 24, 2019 to Lorenzo Scalese

That assumes only one task of the specified type exists.

0
Lorenzo Scalese  Aug 24, 2019 to Eduard Lebedyuk

Yes Exactly! @Eduard Lebedyuk .

It's just a way.

The code can be easily improved depending your needs (ex : return a list of id, namespace filter)

0