Loading
Ongoing maintenance for Salesforce HelpRead More
Context Service
Apex Classes for a Context Definition

Apex Classes for a Context Definition

Apex classes provide the framework for the structure and data of a context definition. You can use these classes to create and manage the context data in your Salesforce org.

Required Editions

Available in: Lightning Experience
Available in: Developer, Enterprise, Professional, and Unlimited editions for Industries clouds where Context Service is enabled

You can’t create an Apex class from a context definition that has any of the following:

  • Attributes with the data type set to SelfReference.

  • Nodes that indirectly reference themselves or child nodes that are shared across multiple parent nodes.

  • Node with the name DefRoot.

  • Attributes with name of meta attributes: id, dmlStatus, dataPath, businessObjectType

Considerations for generating an Apex class

  • The generated Apex classes will not contain dynamic attributes which are part of the context dictionary (such as DYN_COLOR)

  • The generated Apex class names will not contain the underscore character, even if the context definition has an underscore. The Apex class names skip the underscore character.

  • If a context definition includes an attribute with name ‘type’, the generated Apex automatically converts the name to ‘z0type’ to make it Apex compliant. See Apex Reserved Keywords for more information.

  • If a context definition includes attributes of DateTime or Date data type, they are treated as strings having data type as Text in the generated Apex and it will appear in the local date format in the Flow. For example, a Last Modified Date value of 2026-01-21T18:33:26.000Z in the generated Apex appears as 22 January 2026, 00:03 in the flow. Map these values directly to text fields in your flow, or use formula fields to convert the strings to Date or DateTime fields.

  • The generated Apex classes of extended custom definitions must be manually refreshed after the extended custom definition is synced. Apex classes of standard definitions are automatically updated.

  • The generated Apex classes will not contain tag names. Reading query tag data, refer to the related Apex node and attribute names.

  • All system limits of External Service OpenAPI apply to generating Apex classes. See System Limits.

  • For each context definition, one Definition Root class with the name <DefinitionName>_DefRoot is created, and one Apex class is created for each node in the definition.

Structure of the Apex Class

The structure of the generated Apex classes follows the structure of the context definition. Each node in the context definition is represented as a separate class, and the attributes within a node are represented as fields in that class.

For example, consider a context definition named SalesContextDef with the following structure:

Sample context definition for generating an Apex class

When you generate the Apex classes, one main definition root class with the name <DefinitionName>_DefRoot is created. Four more apex classes are created, one for each node.

In this example, Definition root class: SalesContextDef_DefRoot.apex.

Node classes:

  • SalesContextDef_Order.apex

  • SalesContextDef_Account.apex

  • SalesContextDef_Contact.apex

  • SalesContextDef_Opportunity.apex

The SalesContextDef_DefRoot class is the entry point for the context definition. It contains fields for each top-level node in the context definition. Each node class contains fields for its attributes and any nested nodes.

Here is an example of the SalesContextDef_DefRoot class:


global class SalesContextDef_DefRoot {
// [PROPERTY] Order
@AuraEnabled
global List<SalesContextDef_Order> Order {get; set{ this.Order = value; this.Order_set = true; }}
global Boolean Order_set {get; set;}

// [PROPERTY] Account
@AuraEnabled
global List<SalesContextDef_Account> Account {get; set{ this.Account = value; this.Account_set = true; }}
global Boolean Account_set {get; set;}

// [PROPERTY] Opportunity
@AuraEnabled
global List<SalesContextDef_Opportunity> Opportunity {get; set{ this.Opportunity = value; this.Opportunity_set = true; }}
global Boolean Opportunity_set {get; set;}

// [PROPERTY] Contact
@AuraEnabled
global List<SalesContextDef_Contact> Contact {get; set{ this.Contact = value; this.Contact_set = true; }}
global Boolean Contact_set {get; set;}
}

      

Each attribute in a node is represented as a field in the node class with the field name as the attribute's name and the field type as the attribute's data type. Each node Apex class contains its attributes and these fixed meta attributes : id, dmlStatus, dataPath, businessObjectType.

The Account node has two attributes: BillingCity and Name. The Apex class for the Account node also contains one variable for its child node Contact. This variable is used while reading or assigning contact records that come under Account.


global class SalesContextDef_Account {
// [PROPERTY] BillingCity
@AuraEnabled
global String BillingCity {get; set{ this.BillingCity = value; this.BillingCity_set = true; }}
global Boolean BillingCity_set {get; set;}

// [PROPERTY] businessObjectType
@AuraEnabled
global String businessObjectType {get; set{ this.businessObjectType = value; this.businessObjectType_set = true; }}
global Boolean businessObjectType_set {get; set;}

// [PROPERTY] dmlStatus
@AuraEnabled
global String dmlStatus {get; set{ this.dmlStatus = value; this.dmlStatus_set = true; }}
global Boolean dmlStatus_set {get; set;}

// [PROPERTY] id
@AuraEnabled
global String id {get; set{ this.id = value; this.id_set = true; }}
global Boolean id_set {get; set;}

// [PROPERTY] Name
@AuraEnabled
global String Name {get; set{ this.Name = value; this.Name_set = true; }}
global Boolean Name_set {get; set;}

// [PROPERTY] dataPath
@AuraEnabled
global String dataPath {get; set{ this.dataPath = value; this.dataPath_set = true; }
global Boolean dataPath_set {get; set;}

// [PROPERTY] Contact
@AuraEnabled
global List<SalesContextDef_Contact> Contact {get; set{ this.Contact = value; this.Contact_set = true; }}
global Boolean Contact_set {get; set;}
}

      
 
Loading
Salesforce Help | Article