VMware Cloud Community
carl1
Expert
Expert
Jump to solution

Read/Write Resource element not using a file

Page 18 of the vRO Coding Guide (ver 1.0) says

-----

Possibly use a CompositeType, or if it is really meant for internal use then you can save the Properties object as a Resource Element

-----

I cannot find any way to write a JS object into a Resource Element.  Nor for that matter, to read it back as an object.  I find lots of ways to read/write files to a Resource Element, just not objects as the coding guide states.  Can anyone help me?

Thanks,

Carl L.

Oh, and if anyone is interested, the coding guide is here:

https://docs.vmware.com/en/vRealize-Automation/7.3/vrealize_orchestrator_coding_design_guide.pdf

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
eoinbyrne
Expert
Expert
Jump to solution

A resource element stores a MimeAttachment which contains the content. You can access the actual content value of a ResourceElement like this

resourceElement.getContentAsMimeAttachment().content

You can write to the resource element like this

var newResourceContent = "my new resource content";
var mime = resourceElement.getContentAsMimeAttachment();
mime.content = newResourceContent;
resourceElement.setContentFromMimeAttachment(mime);

To store a custom object into the resource element you can convert it to a JSON string using something like the following

var myObject = {}; 
myObject.name = "myObject";
var child = {};
child.name = "child";
myObject.childrenArray= []; 
myObject.childrenArray.push(child); 
var myJSONStringRepresentation = JSON.stringify(myObject); 
//Note that JSON.stringify may not always produce useful results - it depends on the object type. I generally avoid using it for any built-ins
//or plugin types as you will often get only the string [object Object] as the result

I use this mechanism a lot to store small amounts of cached data in vRO for different purposes. The benefit of using a JSON string here is that if you set the MimeType to text/plain then it will be readable in the vRO Client + you can edit it externally if you need to.

Hope this helps

View solution in original post

Reply
0 Kudos
1 Reply
eoinbyrne
Expert
Expert
Jump to solution

A resource element stores a MimeAttachment which contains the content. You can access the actual content value of a ResourceElement like this

resourceElement.getContentAsMimeAttachment().content

You can write to the resource element like this

var newResourceContent = "my new resource content";
var mime = resourceElement.getContentAsMimeAttachment();
mime.content = newResourceContent;
resourceElement.setContentFromMimeAttachment(mime);

To store a custom object into the resource element you can convert it to a JSON string using something like the following

var myObject = {}; 
myObject.name = "myObject";
var child = {};
child.name = "child";
myObject.childrenArray= []; 
myObject.childrenArray.push(child); 
var myJSONStringRepresentation = JSON.stringify(myObject); 
//Note that JSON.stringify may not always produce useful results - it depends on the object type. I generally avoid using it for any built-ins
//or plugin types as you will often get only the string [object Object] as the result

I use this mechanism a lot to store small amounts of cached data in vRO for different purposes. The benefit of using a JSON string here is that if you set the MimeType to text/plain then it will be readable in the vRO Client + you can edit it externally if you need to.

Hope this helps

Reply
0 Kudos