Entity storage - Custom objects

Freshworks customers have real-world data modelling requirements, all of which cannot be met by a product’s native objects. The products offer custom fields to extend the existing native objects. At times, customers need entities that are related to native objects but do not exist within the product. Custom objects help meet this requirement. Through the use of an app, custom objects, and their relationships to native objects (associations) customers can create and use entities that act as part of the product itself, to meet specific functional requirements.

Apps that consume the default native objects or custom fields that the Freshworks products offer, can use the developer platform’s data storage feature. For a long time, the developer platform supported only key-value storage. This poses certain limitations in terms of key size, value field size, reverse look-ups, and capabilities to query, aggregate, report, and relate data. As part of the custom objects functionality, the developer platform offers entity storage in addition to the existing key-value storage. Entity storage helps provide capabilities such as reporting and aggregation of stored data, look up, retrieval, and usage of relational information, querying on stored data, and so on.

To leverage the entity storage - custom objects feature, when you build an app, ensure to use FDK 8.5.0 or a later version.

Note:Custom objects created through an app (as specified in this section) cannot be accessed by another app.

To create and use custom objects,

  1. Define entities.
  2. As part of the app code, use the custom objects interface to create entity records.
  3. As part of the app code, define the necessary actions or operations on the entity records.

Sample use case - KYC record creation and updation for all contacts in the Freshsales Suite system. On initialization, the app leverages the custom objects feature to create KYC records for contacts in the Freshsales system, if such a record does not exist for a contact. On record creation the KYC status is Pending. The app is deployed as a full page app and also as a Contact Details page app. The app displays all KYC records in the Pending status. Also, through entity-storage retrievals, the app provides a provision to view the KYC documents. Based on the submitted approval documents, an agent can approve or reject the KYC document. The app updates the entity-storage records and displays the updated status on the app interface.

Notes:
  • Currently, custom objects does not support defining associations between custom and native objects.
  • You can build apps that use custom objects and submit the apps as a Freshworks app or Custom app. After review and approval, the Freshworks app is published to the marketplace The published app or the custom app is available for installation. If you are unable to install a published app or publish a custom app, contact us.

Define entities

In Freshworks products, business data is modelled as objects. The objects are created out of specific object types. An entity is an object type. Entity definition is the schema definition of the object type. Schema definition includes the definition of all the object.attributes. Contacts, Deals, Sales Accounts, Tasks and so on are some default native objects. tasks.description, tasks.status and tasks.due_date are some of the attributes of the task entity.

You can use the custom objects feature to define new entities (object types). An object belonging to an entity (object type) is a record. Entity records (objects) of the defined object types can be created and an app can process these records to provide meaningful results.

To define and use entities:

  1. Define the custom object schema - Entity definition specification. The schema includes all object.attribute definitions.
  2. Initiate entity storage.
  3. Obtain a reference to the entity to facilitate schema and record operations.

Note: After an app is published, you can update or modify the entity definition and submit a new version of the app.

Entity definition specification

  1. From the app’s root directory, navigate to the config folder and create an entities.json file.
  2. In the entities.json file, use the syntax shown on the right pane to configure all custom objects that the app uses, as JSON objects.
Notes:
  • You can define a maximum of five entities for each business account.
  • You can define a maximum of 20 fields (attributes) for each entity.

Attributes description

  • <entity-name>object

    Schema definition of the custom object. An app can create records of type <entity-name> and process the records.

    For example, for an app to create a custom object kyc_status , <entity-name> in entities.json is kyc_status.

    The app and its users can create multiple records or objects of type kyc_status. Each record is a collection of attributes. In entities.json, specify the attributes’ definition through the fields array.

Initiate entity storage

The developer platform’s data storage feature offers a client.db or $db interface to store and retrieve data. For custom objects, the interface is enhanced beyond its lightweight key-value storage capabilities to support entity storage. This facilitates custom object schema creation and deletion and CRUD operations on the records. Currently, the entity storage is accessible through a versioned interface that specifies that the app uses the v1 version of the entity storage feature.

To initiate entity storage and start using the custom objects interface, use the following constructor:

Sample frontend or client-side file
const entity = client.db.entity({ version: "v1" });
Sample server.js
const $entity = $db.entity({ version: "v1" });

Response
The constructor returns a versioned wrapper interface that can be used for custom objects operations.

Obtain a reference to the entity

To obtain a reference to the entity, use one of the following interface calls:

front-end or client-side file
const <entReference> = entity.get("<entity-name>");
server.js file
const <entReference> = $entity.get("<entity-name>");

Response
A successful call returns a class that represents the entity and contains the static methods that can be used to perform operations on the entity records.

Sample response
{
  schema: async function() {},
  create: async function() {},
  get: async function() {},
  getAll: async function() {},
  update: async function() {},
  delete: async function() {}
}

Create entities

The fdk validate and fdk pack commands validate the entities.json file, to ensure that the Entity Definition Specifications are appropriate. You can test entities creation and record operations before submitting the app.

App installation implicitly creates the entities specified in entities.json.

Retrieve entity schema

To verify the entity creation and view the schema of the created entity, use one of the following interface calls:

Note:<entReference> is a reference to the actual entity.

front-end or client-side file
<entReference>.schema();
server.js file
$entity.get("<entity-name>").schema();

Response
A successful call returns the entity object created based on entities.json specification, along with the following meta-data:

  • id: Unique numeric identifier of the entity, auto-generated by the developer platform when the entity is created and stored.
  • name: <entity-name> as specified in entities.json.
  • prefix: Prefix associated with the entity and used in internal interface calls to uniquely identify entities, auto-generated by the developer platform when the entity is created.

For information on the fields array, see Attributes of <entity-name>.field object.

Delete entities

App uninstallation clean slates data and implicitly deletes the entities definition.

Modify entity definition specification

After an app is published to the marketplace, you can modify the entity definition specification, test the changes, and resubmit a new version of the app.

In entities.json,

  • You can add new entities.
  • You can delete existing entities. All records associated with the deleted entity are deleted.
  • If you modify the <entity-name> of an existing entity, the entity with the previous <entity-name> is deleted and a new entity is created. All records associated with the previous <entity-name> are deleted.

In entities.json > <entity-name>.fields,

  • You can add a new field object.
  • You can delete an existing field object.
  • If you modify the fields.name value, the attribute with the previous name is deleted and a new attribute is created.
  • For an existing field, you cannot modify the fields.type value.
  • For an existing field, you cannot modify the fields.filterable value.
  • For an existing field, you can modify the fields.required value. If the fields.required value is modified to be true, for all existing entity records with no values for the field that is marked as required, the field value is stored as null.
  • For an attribute of the type enum, you cannot modify the fields.choices array.
  • For an attribute of the type section, you can add or delete new fields.

Define record operations

Through the app and its components, multiple records (objects) of a specific entity type can be created. These records provide data that can be processed by the app. As part of processing the data, the app can programmatically perform the following operations on the records:

The operations are performed through the custom objects interface, which returns promises. Successful calls return responses with requisite data and the following:

Response meta-data

  • display_id: Unique identifier of a record, auto-generated when the record is created. It is a combination of the prefix used to identify the entity and an incremental numeric value that uniquely identifies the record.
  • created_at: Timestamp of when the record is created, specified in the ISO-8601 format.
  • updated_at: Timestamp of when the record is last updated, specified in the ISO-8601 format.

Create a record

To create a record belonging to a specific entity, use one of the following interface calls:

Notes:
  • A maximum of 10k records can be created for each entity. Currently, batch operations/bulk record creation is not supported.
  • Each record can be of maximum 100 KB.
  • <entReference> is a reference to the actual entity.
<entReference>.create({
<fields.name1>: <valid value for fields.name1>,
<fields.name2>: "<valid value for fields.name2>"
});

Response: A successful create operation, returns the record object created based on the input and the corresponding meta-data. record.data contains the JSON object that is stored as the record text.

Update a record

To update a record belonging to a specific entity, use one of the following interface calls:

Notes:
  • <entReference> is a reference to the actual entity. <display-id> is the unique identifier of a record, auto-generated when the record is created. <display-id> is returned as part of the response to a successful create record operation.
  • Ensure that all attributes specified as required in entities.json are passed as part of the request call payload.
<entReference>.update("<display-id>", {
  <fields.name1>: "<valid value for fields.name1>",
  <fields.name2>: <valid value for fields.name2>
});

Response: A successful update operation, returns the record object updated based on the input and the corresponding meta-data. record.data contains the JSON object that is stored as the updated record text.

Retrieve a record

To retrieve a specific record belonging to a specific entity, use one of the following interface calls:

Note:<entReference> is a reference to the actual entity. <display-id> is the unique identifier of a record, auto-generated when the record is created. <display-id> is returned as part of the response to a successful create record operation.

Syntax
<entReference>.get("<display-id>");
Sample server.js
const record = await $entity.get("<entity-name>").get("<display-id>");
Sample front-end file
contact_entity.get("kycs-13");

Response: A successful retrieve by display-id operation, returns the retrieved record object and the corresponding meta-data. record.data contains the JSON object that is stored as the record text.

Retrieve all records

To retrieve all records belonging to a specific entity, use one of the following interface calls:

Note: <entReference> is a reference to the actual entity.

Syntax
<entReference>.getAll();
Sample server.js
const records = await <entReference>.getAll({});
Sample front-end file
function loadContacts() {
  contact_entity.getAll()
    .then(function (data) {
      //render details of all contacts as a list
  })
  .catch(function (error) {
    //error message
  })
}

Response: A successful retrieve all records operation, returns the retrieved records as an array of objects. The retrieved records are not paginated, retrieved in sets of 100 records, and ordered by created_time chronologically. The links attribute in the response provides a token to retrieve the next set of records.

Response attributes

  • linksobject

    Link to the next set of records.

  • recordsarray of objects

    All retrieved records belonging to a specific entity, specified as an array of objects.

Sample call to retrieve a succeeding set of records
function loadContacts() {
    contact_entity.getAll({
        next: {
            marker: "Lbr2zerj3WHNDsZ1NsdFj7NiigDlittVkGc7RmPjKF3"
    }})
    .then(function (data) {
        //render details of all contacts as a list
    })
    .catch(function (error) {
        //error message
    })
}

Response: The marker value, in the call, is an encoded token of a record id. A successful call retrieves the next set of records, starting from the record identified by the marker value. The marker value of the last set of records is null.

Apply filters and retrieve specific records

You can include queries as part of the retrieve all records call and thereby specify filter criteria for the filterable fields. On successful processing of the call only specific records that satisfy the criteria are retrieved.

To specify queries as part of the interface call, use the following format:

Notes:
  • When filtering by a datetime field, ensure that the filter criteria is specified in the ISO-8601 format. Range querying on datetime fields is currently not supported.
  • <entReference> is a reference to the actual entity.
Syntax
<entReference>.getAll({
  query: {
    <filterable field-name>: "<filter criteria value>"
  }
});
Sample server.js
const record = await <entReference>.getAll({
  query: {
    <filterable field-name>: "<filter criteria value>"
  }
});

To use the and or or operations to construct a query with multiple query parameters, use the following samples:

Notes:
  • When constructing a query with multiple query parameters, a minimum of two query parameters and a maximum of three filterable fields or query parameters should be used.
  • In an and operation, ensure that the attributes (filterable fields) used are different.
  • In an or operation, ensure that the attributes (filterable fields) used are the same.
  • Nested queries are not supported.

Sample 1 - $or query construct

const record = contact_entity.getAll({
  query: {
    $or:[{
       status: "Pending"
    },
    {
       status: "Rejected"
     }
   ]}
   }).then(function(data){
    // access to filtered contacts
  }).catch(function(data){
   // Handle errors
})

Response: A successful retrieve all records operation with filters, returns the records that meet the filter criteria as an array of objects.

Response attributes

  • linksobject

    Link to the next set of records.

  • recordsarray of objects

    All records belonging to a specific entity and meeting the filter criteria, specified as an array of objects.

Sample 2- retrieve subsequent sets of filtered records

const record = await contact_entity.getAll({
  query: {
    $or: [{
      status: "Pending"
    },
    {
      status: "Rejected"
    }
  ]},
  next: {
    marker: "Lbr2zerj3WHNDsZ1NsdFj7NiigDlittVkGc7RmPjKF3"
  }
});

Sample 3 - $and query construct

const record = await $entity.get(‘kyc_status’).getAll({
    query: {
        $and: [{
            status: "Pending"
        },
        {
            document_type: "Voter ID"
        }
    ]},
    next: {
        marker: "Lbr2zerj3WHNDsZ1NsdFj7NiigDlittVkGc7RmPjKF3"
    }
});

Delete a record

To delete a specific record belonging to a specific entity, use one of the following interface calls:

Note: <entReference> is reference to the actual entity. <display-id> is the unique identifier of a record, auto-generated when the record is created. <display-id> is returned as part of the response to a successful create record operation.

<entReference>.delete("<display-id>");

Response: A successful delete operation returns an empty object.

Error responses

If a call fails, the custom objects interface returns an error response with the following attributes.

Error response attributes

  • messagestring

    Generic message specifying the reason for the error.

  • statusinteger

    HTTP status code.

  • errorsarray of objects

    All errors that caused the call to fail, specified as an array.

  • errorSourcestring

    Specifies whether the error is app or platform related.
    Possible values: APP, PLATFORM

Test

Note:To test your app, use the latest version of the Chrome browser

  1. To test the configured custom objects, from the command prompt navigate to the app project folder and run the following command:

    fdk run

    The command validates the entities.json file and displays the validation errors, if any. Fix the validation errors and run the command. The command creates a .sqlite file and the entities that are defined in entity.json. .sqlite mimics the platform’s entity storage, in the local setting.

    Note:If the entity definition specification in entities.json is modified, for the modification to reflect in the local .sqlite file, you will have to rerun the fdk run command. A prompt to resync is displayed and a resync deletes all existing entities, associated data, and records and creates a clean instance.

  2. Log in to your Freshsales Suite account.

    If the app is successfully created, it is rendered in the app location specified in manifest.json.

  3. To the Freshsales Suite account URL, append ?dev=true.
    Example URL: https://domain.myfreshworks.com/crm/sales?dev=true

  4. To test the custom objects interface calls, from the app, simulate record operations. If the calls fail, appropriate error responses are displayed.