Method to return value of object element
I am trying to write a class method to return the value of a object element. Input to method is the object and element. I am not getting a value returned. What am I missing?
{
quit:pField="" "" set tResponse=""
set tResponse = pObject.%Get(pField)
return tResponse
}
Here is my input data,
{
"priority": "2",
"lastmodifieduser": "PORTAL",
"assignedto": "jdoe01 STAFF",
"status": "REVIEW",
"documentclass": "PATIENTCASE",
"versiontoken": "937486",
"departmentid": "999",
"patientcaseid": "105555424",
"providerusername": "jdoe01",
"internalnote": "firstMessage",
"subject": "first123",
"patientid": 9877654321,
"createduser": "PORTAL",
"description": "patient case - first123",
"documentroute": "MDP",
"documentsubclass": "PATIENTCASE_CLINICALQUESTION",
"documentsource": "PORTAL",
"createddate": "07/17/2023",
"lastmodifieddate": "07/17/2023"
}
It looks to me like you might want to use %DynamicObject instead of %DynamicAbstractObject. %DynamicAbstractObject doesn't have a %Get method.
Tried that. Still did not get a response with a value.
Nice, now we know nearly everything, except the most important info: what is the content of pField?
I assume, pObject contains the above JSON, i.e.
set pObject = {"priority": "2", "lastmodifieduser": "PORTAL", "assignedto": "jdoe01 STAFF", ... }
pField = "patientid"
Did a little test:
Result:
It works also for me... maybe it depends on how Michael Wood is passing the json object to the method...
If he is passing a string, there is a conversion needed from string to %DynamicAbstractObject like:
Set str="{""prio"":""2"",""code"":""A001""}" Write str {"prio":"2","code":"A001"} Set obj=##Class(%DynamicAbstractObject).%FromJSON(str) Write obj 1@%Library.DynamicObject ZW obj obj={"prio":"2","code":"A001"} ; <DYNAMIC OBJECT>
That worked. Thanks.
{
quit:pObject="" ""
quit:pField="" ""
$$$TRACE("pObject="_pObject)
$$$TRACE("pField="_pField)
set dynObj=##Class(%DynamicAbstractObject).%FromJSON(pObject)
set tValue=$PROPERTY(dynObj,pField)
return tValue
}
$property works with all objects, dynamic or not:
>s a={"b":2} >w $property(a, "b") 2