Liferay Object API from Custom Module

Accessing Liferay Object Data through Liferay Module

Liferay Objects has been one of the powerful features of Liferay. It provides codeless approach of building application. It provides headless api which can be used for accessing object entry data. These endpoints provide us options for searching, filtering, sorting and aggregating the data (Refer).

In this blog, we will see how we can make use of this Filter, Sort and Search functionality from inside Liferay module.

Liferay Provides a Service - ObjectEntryLocalService which can be used for accessing object entry along with features of Filter, Sort and Search.

For this example I have created an Object Employee which has three columns : 
1. Name - Text
2. Designation - Picklist
3. Experience - Integer

We will create filter to fetch records with Designation as Manager and Experience more than 10 years and sort it with name.

1. Fetch the ObjectDefinition, You can get the objectdefinition id from Object details screens :

  ObjectDefinition objectDefinition =  ObjectDefinitionLocalServiceUtil.fetchObjectDefinition(39604)

2. We will create a filter with the required query :

String filterString  = "designation eq 'manager' and experience gt 10 ";

3. Create Predicate for the filter :

@Reference(
            target = "(filter.factory.key=" + ObjectDefinitionConstants.STORAGE_TYPE_DEFAULT + ")"
        )
private FilterFactory<Predicate> _filterFactory;

Predicate predicate = _filterFactory.create(filterString , objectDefinition);

4. Create Sort for the field name :

  Sort[] sorts = new Sort[] {SortFactoryUtil.create("name", false)};

5. Create Array for providing fields that need to be returned, if all fields are required we can keep it null :

String[] selectedObjectFieldNames = new String[] {"name"};

6. Now we will call getValueList method provided by ObjectEntryLocalService Service :

List<Map<String, Serializable>> list = _objectEntryLocalService.getValuesList(groupId, _portal.getDefaultCompanyId(), _portal.getUserId(renderRequest), objectDefinition.getObjectDefinitionId(),

selectedObjectFieldNames , predicate, searchKeyword, QueryUtil.ALL_POS, QueryUtil.ALL_POS, sorts);

Note :

1. groupId  can be 0 if Object is Company scoped.

2. Search Keyword can be used if searching with some search terms or else it can be null.

The above service will return records with only field name and matching filter provided along with sorted by name in asc order.

Note : Above example is done on Liferay 7.4