Rodolfo Moreira · 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)



0 7 190 2


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

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.

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

Would it help?

It's not really clear what you expect by "ID of the task"

You get the actual process id by system variable  $JOB.

If you look for the Task Name as seen in Taskmanager then  $this.GetTaskName() as %String
Returns the localized name of this task.
see docs of  %SYS.Task.Definition ​​​​​​​

Hello @Rodolfo Moreira,

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)) {
        if (rs.Get("TaskClass")=className){
            Set id = rs.Get("ID")
    Quit id

Hope this help you.


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

That assumes only one task of the specified type exists.

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)