Question
· Sep 15, 2016

How to retrive the output of sample program

I got a input to user . How to I print the input in another method

ClassMethod sample() As %String
{
Set Name=##class(Eam.Empdetails).%New()
"enter your name :",NameStr
Name.EmpName=NameStr
"enter your address:",NameStr1
Name.Address=NameStr1
"Enter your Phone number:",NameStr2
Name.PhoneNo =NameStr2
"Enter Your Id:",NameStr3
Name.id = NameStr3
set status = Name.%Save()
}

Discussion (3)0
Log in or sign up to continue

I'm guessing that you mean another class method that would be call to output your saved information. 

Once you have use .%Save() Caché generates an id. I see that you have a field named id, but as you didn't post the class definition I'm not sure it was set up to be the unique key for the class. If you were looking to do that then I might recommend this documentation.

http://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=...

So by default Caché uses an integer for the ID, incremented by 1 from the last saved object. You can get that id right after .%Save() by calling .%Id(). Once you have that value you can use it to open the object in a different method. There are other ways to call up the %Id() information such as using a SQL query to find the object and retrieving the object id from the ID field.  Either way once you have the Id you can open a saved object with like this. 

Set myObj = ##class(Eam.Empdetails).%OpenId(yourid) 

This page goes into more detail about using %Save and retrieving an object. 

http://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=...

I hope this helps. 

Lots of ways to get the object instance properties back (queries, ID, Cache Object Script).  Start with the doc and examples:

http://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=GORIENT_ch_persistence#GORIENT_persistence_data_programmatically​

One simple example:

SAMPLES>set person=##class(Sample.Person).%OpenId(1)
 
SAMPLES>w person.Name
Newton,Dave R.

Tom Fitzgibbon | 347-464-8531

Another response with similar info...

So, your ClassMethod sample() is included in (or at least references) the class Eam.Empdetails, which, presumably, is as %Persistant class.  Therefore, the method %Save() adds the %New object you just defined as a record in the the database.

Your method returns a status from the %Save, and you should check that because, if it is not 1 ($$$OK), then there will be not data to read.

Assuming the record was successfully saved, it has an ID that you can use to read it back. You did not include your class definition, and so I don't know how the ID is specified.  The ID might be, for example, a system-generated sequential integer, the Name.id property you specified, or something else.  You might want to see what was saved by temporaliry adding the following line to the end of your method, compiling, and running it again.  It will display the contents of the object you just %Saved.  Near the top of the display is %%OID which is a Caché list consisting of the object's ID and the class to which it belongs.  You can retrieve this list with object method %Oid().  The ID is the first item in the list.  Alternatively, your ID might have been specified as Name.id.

To print out a record from the database, you would need to know its ID.  If you have a main program that calls your sample() method, you might want that method to also return the ID so the main program could read and display it.  One way to do this would be to add an output argument to sample(), changing its header to:
   ClassMethod sample(ID As %String) As %String
and adding after the %Save():
   s ID=$lg(Name.%Oid(),1)

Then the main program could look something like (the leading dot passes ID by reference so the value can be returned):
   s ID=""
   s status=##class(Eam.Empdetails).sample(.ID)
   if (status=1) {
      s Name=##class(Eam.Empdetails).%OpenId(ID)
      w !
      w "EmpName: "_Name.EmpName,!
      w "Address: "_Name.Address,!
      w "Phone: "_Name.PhoneNo,!
      w "Id: "_Name.id,!
   }

If you want to check the status with $$$OK (or other predefined values), add the following line to the top of the main program:
   #include %occStatus
and change the test to:
   if (status=$$$OK) {

There are other ways to see your data, such as with SQL (add a WHERE clause if desired):
   SELECT * FROM Eam.Empdetails