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.
Example:
import com.contentstack.sdk.*;
Stack stack = Contentstack.stack(context, apiKey, deliveryToken, environment);
GlobalField globalField = stack.globalField();
globalField.findAll(new GlobalFieldsResultCallback() {
@Override
public void onCompletion(GlobalFieldsModel globalFieldsModel, Error error) {
if(error == null){
JSONArray result = globalFieldsModel.getResultArray();
} else {
System.out.println("❌ Error: " + error.getErrorMessage());
}
}
});fetch
The fetch method retrieves the details of the specified global field.
| Name | Type | Description |
|---|---|---|
| globalFieldUid (required) | String | UID of the global field |
Example:
import com.contentstack.sdk.*;
Stack stack = Contentstack.stack(context, apiKey, deliveryToken, environment);
GlobalField globalField = stack.globalField("globalFieldUid");
globalField.fetch(new GlobalFieldsResultCallback() {
@Override
public void onCompletion(GlobalFieldsModel globalFieldsModel, Error error) {
if (error == null) {
JSONArray result = globalFieldsModel.getResultArray();
System.out.println("Global Fields Response: " + result);
} else {
System.out.println("Error: " + error.getErrorMessage());
}
}
});findAll
The findAll method retrieves the details of all the global fields of the stack.
Example:
import com.contentstack.sdk.*;
Stack stack = Contentstack.stack(context, apiKey, deliveryToken, environment);
GlobalField globalField = stack.globalField();
globalField.findAll(new GlobalFieldsResultCallback() {
@Override
public void onCompletion(GlobalFieldsModel globalFieldsModel, Error error) {
if(error == null){
JSONArray result = globalFieldsModel.getResultArray();
} else {
System.out.println("Error: " + error.getErrorMessage());
}
}
});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(context, apiKey, deliveryToken, environment);
GlobalField globalField = stack.globalField("globalFieldUid").includeBranch();
globalField.fetch(new GlobalFieldsResultCallback() {
@Override
public void onCompletion(GlobalFieldsModel globalFieldsModel, Error error) {
if (error == null) {
JSONArray result = globalFieldsModel.getResultArray();
System.out.println("Global Fields Response: " + result);
} else {
System.out.println("Error: " + error.getErrorMessage());
}
}
});Note
- Information about Global fields can be retrieved by all users, regardless of their role or access level.
- If your Global field contains nested Global fields, they will appear as part of the schema in the API response.