Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

If you have already learned how to add a property or a sub-property to a resource object, you already know the basics of how these GetObject companion services work. As one might expect, there is an AddSubResource service available to add a sub-resource to a resource. In fact, there are two principle services available to the developer. The other service is named AddSubResourceObject. Both of these are explained below.

Table of Contents

AddSubResource Service

The calling signature for the AddSubResource service is virtually identical to the AddSubProperties service (i.e., the plural version of the AddSubProperty service). The key difference is that when the AddSubProperties service is called more than once for the same parent property name and the same sub-property names, the new sub-property values replace the old ones. The AddSubResource service, on the other hand, adds (or appends) new sub-resource objects to the existing array of sub-resources associated with the parent property. Here is an example of what the code might look like:

...

Admittedly, the AddSubResourceObject service appears to require more code to implement than the AddSubResource service. For simple sub-resource objects this is true but . But as already noted, the AddSubResourceObject service is intended for more complex sub-resource objects. It is also quite useful when another service routine is responsible for creating the object handles handle and these are this is returned conveniently to be a calling routine and used in the AddSubResourceObject service.

At this time we will introduce the AddSubResourceObjects service. This is a wrapper around the AddSubResourceObject service and it accepts an @FM delimited list of object handles. This is quite useful when a separate routine returns multiple objects handles at once. The below code demonstrates this in action using the GetObjects service and the AddSubResourcesObjects service:

Code Block
languagebp
objResource     = HTTP_Resource_Services('GetObject')
If Error_Services('NoError') then
    objSubResources = HTTP_Resource_Services('GetObjects', 'CONTACTS', 'SELECT CONTACTS WITH FIRST_NAME STARTING "H"', 'FIRST_NAME' : @FM : 'LAST_NAME', 'firstName' : @FM : 'lastName')
    HTTP_Resource_Services('AddSubResourceObjects', objResource, 'contacts', objSubResources)
    // Serialize the JSON object.
    jsonResource    = HTTP_Resource_Services('GetSerializedResource', objResource)
end

When this code is tested against the default CONTACTS table that ships with the SRP HTTP Framework, the following resource object is produced:

Code Block
languagejs
{
   "contacts":[
      {
         "firstName":"Harrison",
         "lastName":"Haufler"
      },
      {
         "firstName":"Haydee",
         "lastName":"Denooyer"
      },
      {
         "firstName":"Heike",
         "lastName":"Berganza"
      },
      {
         "firstName":"Helga",
         "lastName":"Fredicks"
      },
      {
         "firstName":"Herman",
         "lastName":"Demesa"
      },
      {
         "firstName":"Herminia",
         "lastName":"Nicolozakes"
      },
      {
         "firstName":"Hillary",
         "lastName":"Skulski"
      },
      {
         "firstName":"Howard",
         "lastName":"Paulas"
      }
   ]
}