Question
· Aug 4, 2023

Calling Second Method inside the Main Method

Hy,

I have a question how to call another methode inside the main method ?

like 

this_MainMethod

{

    calling SecondMethod

}

Method SecondMethod

{

}

When I try to call the data without second method, it works, but when I try to call with SecondMethod, it show empty

Thank you for your help

Product version: IRIS 2023.2
$ZV: IRIS for UNIX (Red Hat Enterprise Linux 8 for x86-64) 2022.1 (Build 209_0_21965U) Thu Oct 20 2022 22:15:19 EDT [Health:3.5.0]
Discussion (17)3
Log in or sign up to continue

I assume, we are talking about ObjectScript? Then you got in the main method a <SYNTAX> error because "calling" is not a command keyword. So what are you really doing? We don't need the whole main method, the line with the call would be enough. By the way, a method name like "this_method" is not a valid name except if you place it in (double)quotes.  Something, which is formal correct, looks like this:

Method "this_MainMethod"()
{
    do ..SecondMethod()
}

Method SecondMethod()
{
    // ...
}

I've no idea how to call this method

ClassMethod GetSphereJauh(Id As %String) As %String   ----this  is the second method
{
 set Id=$zstrip(Id,"*C")
 set (ID,OBSATParRef,OBSATValue,ATTRCode,ATTRDesc)=""
 
 // Query Data Encounter
  &sql(declare adm cursor for
     select 
OBS_ParRef,
OBSAT_ParRef,
OBSAT_Value,
OBSAT_ItemAttribute_DR ->ATTR_Code,
OBSAT_ItemAttribute_DR ->ATTR_DESC
into :ID,:OBSATParRef,:OBSATValue,:ATTRCode,:ATTRDesc
from SQLUser.MR_ObservationsAttribute 
Join SQLUser.MR_Observations on OBS_RowId = OBSAT_ParRef
Join SQLUser.MRC_ObservationItemAttribute on ATTR_RowId = OBSAT_ItemAttribute_DR
where OBSAT_ParRef =:Id and ATTR_Code ='EKA.Sphere.OD' ) &sql(open adm)
for {
  &sql(fetch adm)
  quit:SQLCODE'=0
    set ind=ind+1
  set ^||IRISTemp("TRAK",repid,ind)=$lb(ind,ID,OBSATParRef,OBSATValue,ATTRCode,ATTRDesc)
} &sql(close adm)
  // Build QHandle (AtEnd,ReportID,Index)
set QHandle=$listbuild(0,repid,0)
set QHandle(1)=mon
quit $$$OK
} ClassMethod GetSphereJauh(Id As %String) As %String
{
 set Id=$zstrip(Id,"*C")
 set (ID,OBSATParRef,OBSATValue,ATTRCode,ATTRDesc)=""
 
 // Query Data Encounter
  &sql(declare adm cursor for
     select 
OBS_ParRef,
OBSAT_ParRef,
OBSAT_Value,
OBSAT_ItemAttribute_DR ->ATTR_Code,
OBSAT_ItemAttribute_DR ->ATTR_DESC
into :ID,:OBSATParRef,:OBSATValue,:ATTRCode,:ATTRDesc
from SQLUser.MR_ObservationsAttribute 
Join SQLUser.MR_Observations on OBS_RowId = OBSAT_ParRef
Join SQLUser.MRC_ObservationItemAttribute on ATTR_RowId = OBSAT_ItemAttribute_DR
where OBSAT_ParRef =:Id and ATTR_Code ='EKA.Sphere.OD' ) &sql(open adm)
for {
  &sql(fetch adm)
  quit:SQLCODE'=0
    set ind=ind+1
  set ^||IRISTemp("TRAK",repid,ind)=$lb(ind,ID,OBSATParRef,OBSATValue,ATTRCode,ATTRDesc)
} &sql(close adm)
  // Build QHandle (AtEnd,ReportID,Index)
set QHandle=$listbuild(0,repid,0)
set QHandle(1)=mon
quit $$$OK
}

And I want to call this method Inside this main method

ClassMethod GetDataExecute(ByRef QHandle As %Library.Binary, Id As %String) As %Library.Status
{
 //Start Monitor (if configured to capture stats)
 set mon=..MonitorBegin()
   
 // Get reportid i.e. use $INCREMENT to add another node to ^IRISTemp global.
 // We use ^IRISTemp global because it will always use memory before disk
 // Use $Increment to get the next node
 set repid=$increment(^||IRISTemp("TRAK"))
 if $data(^||IRISTemp("TRAK",repid)) {
 kill ^||IRISTemp("TRAK",repid)
 }
 set ind=0
 
 // Build data into ^IRISTemp("TRAK",ReportID)
 set Id=$zstrip(Id,"*C")
 //set EpisodeId=$zstrip(EpisodeId,"*C")
 //set PatientId=$zstrip(PatientId,"*C")
 set (ID,EpisodeID,PatientID,PAPMINAME2,PAPMINAME,Age,Address)=""  // Query Data Encounter
  &sql(declare adm cursor for 
   select 
DISTINCT(OBS_ParRef),
MRADM_ADM_DR->PAADM_RowID EpisodeId,
PAADM_PAPMI_DR->PAPMI_RowId PatientId,
PAADM_PAPMI_DR->PAPMI_Name2,
PAADM_PAPMI_DR->PAPMI_Name,
PAADM_PAPMI_DR->PAPMI_PAPER_DR->PAPER_AgeYr Age,
PAADM_PAPMI_DR->PAPMI_PAPER_DR->PAPER_StName
into :ID,:PatientID,:PAPMIRowID,:PAPMINAME2,:PAPMINAME,:Age,:Address
from SQLUser.MR_Observations 
join SQLUser.PA_PatMas on PAPMI_RowId=PAADM_PAPMI_DR
join SQLUser.PA_Person on PAPER_RowId=PAPMI_PAPER_DR
where OBS_ParRef =:Id)
 
 &sql(open adm)
 for {
 &sql(fetch adm)
 quit:SQLCODE'=0
 
 set ind=ind+1
 
 //set SphereJauh=..GetSphereJauh(ATTRDesc)  ----Here I try to call this method

 set ^||IRISTemp("TRAK",repid,ind)=$lb(ind,ID,PatientID,PAPMIRowID,PAPMINAME2,PAPMINAME,Age,Address,SphereJauh)  }
 &sql(close adm)
 // Build QHandle (AtEnd,ReportID,Index)
 set QHandle=$listbuild(0,repid,0)
 set QHandle(1)=mon
 quit $$$OK
}

It will give me an empty row, there's no data when I try to call the second method, but if I comment the calling for second method, it will display the data

You have 2 options to return a value from a Method, one is to send by reference an object and instantiate that object inside the method called:

ClassMethod MyMethod(myInput As String, Output myOutput As MyPackage.MyClass) As %Library.Status
{
    set myOuput = ##class(MyPackage.MyClass).%New()
    set myOutput.name = "This is a name"
    
    return $$$OK
}

And invoke it like:

// Invoke method with .. if it belong to the same class or with ##class(classname).method if it doesn't
set tSC = ..MyMethod("One Input", .output)
// Now output has been created and output.name has the value "This is a name"
write output.name

You can see here in the documentation how to pass variables by reference:

https://docs.intersystems.com/irislatest/csp/docbook/DocBook.UI.Page.cls...

You need to change your cursors names, use two different names in your declare statements, try it.

Some considerations about embedded SQL cursors:

1) I think using embedded SQL Cursors is a practice to be avoided in favor of %SQL.Statement class usage: 

https://docs.intersystems.com/iris20232/csp/docbook/Doc.View.cls?KEY=GSQ...

2) Genarally is not  a good idea to have the same name in embedded SQL cursors, even if they are in different Methods, this because of the behaviour of the Stack separations used by the embedded SQL cursors.

3) Use a close statement before every open statement of embedded SQL cursors, this prevent problems if your process crash and in the same context you re execute it.

After reading through this thread, it seems like there are two questions:

  1. What is the correct syntax for calling a method? That question has been answered by several posts. It's either using ..method2() if method2() is in the same class as method1(), or ##class(package.class).method2() if method2() is in a different package.class. Use "do" to call the method, or "set retval = " to call the method and capture the returned value.
  2. Why when I run method1() I get data, but when I add a call to method2() from inside method1() I get no data? As Davide suggested, using "adm" as an embedded SQL cursor in the GetDataExecute() method and the GetSphereJauh() method is probably causing the problem, so try changing the cursor name to "juah" in the declare, open, fetch and close statements in GetSphereJauh(). If that doesn't help, you should probably contact TrakCare Support and get their help.