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
}

}

 

  • 0
  • 0
  • 130
  • 5
  • 2

Answers

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)) {
        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...)

Comments

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?