intersystems-cacheobjectscript

Calling method from console: "No current object"


I have a method that I need to test. I would like to do so from the console. Here is the method, as well as some metadata from the class:

Include HS.Common

Class Custom.class Extends Ens.BusinessOperation
{
    Parameter ADAPTER = "EnsLib.EMail.OutboundAdapter";

    Property Adapter As EnsLib.EMail.OutboundAdapter;

    Method SendMessage(pSubject As %String, pMessage As %String, pEmailAddresses) As %Status
    {
        set tSC=$$$OK
        set tMailMessage=##class(%Net.MailMessage).%New()
        do tMailMessage.To.Insert($PIECE(pEmailAddresses,",",1))
        for tI=2:1:$LENGTH(pEmailAddresses,",") {
            do tMailMessage.Cc.Insert($PIECE(pEmailAddresses,",",tI))
        }

        set tMailMessage.Subject=pSubject
        set tMailMessage.Charset="iso-8859-1"
        set tSC=tMailMessage.TextData.Write(pMessage)
        quit:'tSC
        Set tSC1=..Adapter.SendMail(tMailMessage)
        if 'tSC1 {
            //Log warning about being unable to send mail.
            do $SYSTEM.Status.DecomposeStatus(tSC1,.err)
            $$$LOGWARNING("Could not send email: "_err(err))
            kill err
        }

        quit tSC
    }

    ...other methods here...

}

but when I perform this command:

set tResult = ##class(Custom.class).SendMessage("Test Subject","Test Message","my@email.com")

I get this error:

 Set tSC1=..Adapter.SendMail(tMailMessage)
 ^
<NO CURRENT OBJECT>zSendMessage+11^Custom.class.1

I tried instantiating adapter, much like the property definition, before calling the method but that did not work. How can I call this method from a console session?


Solution

  • this method is an instance method, and you can't call it directly just for some class. Before, you should create an object, and then for that object, you can call any instance methods. But you still trying to call Ensemble classes, it is not so easy, because you should prepare environment, such as configured and started Ensemble Production, your class should be added as an Operation, configured and activated.

    set oper=##class(Custom.class).%New("configName")
    

    where configName - name for that operation in your production, by default it is same as class name (e.g. "Custom.class"). And now you can call your method.

    write oper.SendMessage("testSubj","test body","my@mail.com")
    

    But I would not recommend such way. It would be better if you test it through production, just sent test messages to this operation.