Search code examples
liferayliferay-6liferay-service-builder

Delete record on sepecific field value in liferay service builder


I want to delete specific record using Field Name

Table : Dummy Entity

  • Field Id
  • Field Name

    public void deleteLocation(req, res){
    String getLocationName = request.getParameter("locationName");
    Location locationToDelete = new LocationImpl();
    locationToDelete.setLocationName(getLocationName);
    LocationLocalServiceUtil.deleteLocation(locationToDelete);
    }
    

It's not showing me any error but the record doesn't get deleted. Please hep me out.


Solution

  • The simplest way to achieve this is to add <finder> node for that specific field in service.xml, as following (saying Location is your entity name, name is your field name and Name is the name of finder entry in service.xml) and build service:

    <column name="name" type="String" />
    
    <finder name="Name" return-type="Collection">
        <finder-column name="name" />
    </finder>
    

    On successful build, it will create CRUD operations in your service based on that column. Now you can find following methods in your LocationUtil.java:

    findByName,
    removeByName,
    countByName,
    

    Create following (new) method in LocationLocalServiceImpl.java:

    public void deleteLocationsByName(String name){
        try{
            LocationUtil.removeByName(name);
        }catch(Exception ex){
            // log your exception
        }
    }
    

    Again, on building service, this method will be available for use in your action class from LocationLocalServiceUtil.java, where you can call it like:

    public void deleteLocation(req, res){
        String locationName = request.getParameter("locationName");
        LocationLocalServiceUtil.deleteLocationsByName(locationName);
    }
    

    That's it, you have added custom finder method to your service.