Get all fields of a given type

How can I get all fields of a given type?
I need the fields name to use them as header in an export to CSV file.

You can use the Type#fieldTypes function to retrieve all fields defined on a type.

In JavaScript, it would look like this:

MyType.fieldTypes().map(function(fieldType) { return fieldType.name(); });

Where C3.typesys.Type#fieldTypes returns an array of C3.typesys.FieldType instances. Keep in mind that a field can be a method as well.

1 Like

Thanks @matt, but how I can check if a field is a method or not? because I only want fields!

Check out the documentation for C3.typesys.FieldType:
https://<environment_url>/static/jsduck/index.html#!/api/C3.typesys.FieldType

Hint: At minimum, a field type consists of a name (the field name) and a value type. You’ll have to figure out how to determine if a value type is a method.

2 Likes

There is FunctionType, how I can get these instances? because I can then filter out these functions from my fields list.

The solution is as described bellow

MyType.fieldTypes().filter(function(fieldType) {
  var fn = fieldType.name();
  var vt = fieldType.valueType();
  return !vt.isAnyFunction();
}).map(function(fieldType) {
  return fieldType.name();
});

Following up on this with a twist, what if I wanted all fields on my Type that are writable?

define “writable” please

If I did a MyType.upsert(), the fields I am allowed to define. e.g. not stored calcs, not functions, not foreign key lookups, etc.

What if I’d like to filter for all required fields for the type? (In the documentation/c3showtype it lists whether its required-- how can I modify this query to pull only those marked required?)

MyType.fieldTypes().filter(function (fieldType) {
  return fieldType.hasAnnotation('constraint') && fieldType.extensions().constraint.required === true;
});

The length it returns is correct, but the values were
(38) [constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr, constr]

Yes, those are instances of C3.typesys.FieldType. Use the browser developer tools’ auto-complete feature for the available APIs on those field type instances.

As another follow up - is there a way to do this with python using the remote-types connection? Server version 7.8.8.

EDIT: This works, but still open to cleaner ways to do this :slight_smile:
[x['name'] for x in c3.TagMetadataStore.readType(typeName="MyType").fields.toJson()]

yea that makes a remote api call… there should be an api like:

C3.myType.fieldTypes() or equivalent… can you try that?

I was unable to get an API of that form to work in python… typically returned an error around the lines of

"function metadata was not found for action Target [myTenant/myTag/MyType?action=fieldTypes]"
JSON: {}

Also, worth mentioning that fieldTypes is only available client side - I was not able to get it to work on python code provisioned to my environment. See this post for discussion on this: