Question
· 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
}

}
Discussion (7)1
Log in or sign up to continue

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