Is it Possible to fetch the comments on a field on a type for use in the UI?

I would like to display a help-modal to the user in the UI the description of a field on a type to give them more context for what some of the fields represent.

The content already exists in the c3typ file as comments on the field, so I would like to leverage them instead of copying them and hardcoding them into a modal.

Is there a way to do this?

(Remember, this will be done with the permissions of the end user, as opposed to someone with Developer Rights)

Try this function:

function getTypeFieldDescription(typeName, fieldName) {
    var typeMetadata = MetadataStore.tag().readTypeJson(typeName)
    var fieldMetadata = typeMetadata.fields.filter(fieldMeta => fieldMeta.name == fieldName);
    // Check that field exists.
    if (fieldMetadata.length == 1) {
        var fieldDescription = fieldMetadata[0].extensions.doc.details;
        return fieldDescription;
    }
    // Field did not exist
    return "";
}

Edit: Actually, I realized I have not checked that this can be done with the permissions of the end user. Please confirm this works without developer rights.

2 Likes

Thank you, Yushi.

This can definitely work.

I found that this will be sufficient in 7.8, as readTypeJson() is not a function, yet:

function getTypeFieldDescription(typeName, fieldName) {
    var typeMetadata = MetadataStore.tag().readType(typeName)
    var fieldMetadata = typeMetadata.fields.filter(fieldMeta => fieldMeta.name == fieldName);
    // Check that field exists.
    if (fieldMetadata.length == 1) {
        var fieldDescription = fieldMetadata[0].extensions.doc.details;
        return fieldDescription;
    }
    // Field did not exist
    return "";
}

This should be possible to put this in a method on a c3typ and then generate the proper response, as long as the end user is granted permissions to access the method.

1 Like

All of the below is done with “C3.Group.Developer” Access:

When I execute this action from static/console:

TagMetadataStore.readType('Unit')

I see all of the type.

However, if I have this type:

type RandomType {
  returnFieldsOnType: function(typeName: string) : [Obj] js server
}

with this function:

function returnFieldsOnType(typeName) {
  var typeMeta = TagMetadataStore.readType(typeName);
  if (logger.isDebugEnabled()) {
   logger.debug("initial typeMeta: " + JSON.stringify(typeMeta));
  }
  var fields = typeMeta.fields;
  return fields;
}

and the splunk logger shows when it is run in js on the server that the full metaData is not returned for the type:

initial typeMeta: {"type":"Type","module":"-type-Unit","name":"Unit"}

however, if I run the method with a breakpoint (now it will be run in js on the client), I get the expected full metadata of the unit type.

In neither case does the code error out, but it seems like the action from the client gets executed with different permissions than the actions when run on the server.

Is this expected? Is there a workaround to achieve what we are looking for?