Global Fields
Global Fields
A Global field is a reusable field (or group of fields) that you can define once and reuse in any content type within your stack. This eliminates the need (and thereby time and efforts) to create the same set of fields repeatedly in multiple content types.
fetch
The fetch method retrieves the global field data of the specified global field.
Example:
import com.contentstack.sdk.*;
Stack stack = Contentstack.stack(apiKey, deliveryToken, environment);
GlobalField globalField = stack.globalField("globalfields_uid");
globalField.fetch(new GlobalFieldsCallback() {
@Override
public void onCompletion(GlobalFieldsModel model, Error error) {
JSONArray resp = model.getResultArray();
Assertions.assertTrue(resp.isEmpty());
}
});findAll
The findAll method retrieves all the global fields of the stack.
Example:
GlobalField globalField = stack.globalField();
globalField.findAll(new GlobalFieldsCallback() {
@Override
public void onCompletion(GlobalFieldsModel globalFieldsModel, Error error) {
assertTrue(globalFieldsModel.getResultArray() instanceof JSONArray);
assertNotNull(((JSONArray) globalFieldsModel.getResponse()).length());
}
});includeBranch
The includeBranch method includes the branch details in the result for single or multiple global fields.
Example:
import com.contentstack.sdk.*; Stack stack = Contentstack.stack(apiKey, deliveryToken, environment); GlobalField globalField = stack.globalField().includeBranch();
includeGlobalFieldSchema
The includeGlobalFieldSchema method includes the schema of the global field in the response.
Example:
import com.contentstack.sdk.*; Stack stack = Contentstack.stack(apiKey, deliveryToken, environment); GlobalField globalField = stack.globalField().includeGlobalFieldSchema();
Note
- All users can retrieve information about Global fields, regardless of their role or access level.
- If a Global field includes nested Global fields, the API response will return them as part of the overall schema.