How to get Liferay Dynamic Data Lists RecordId (ddlRecordId) in workflow script

3.6k Views Asked by At

I'm trying to integrate Liferay Dynamic Data Lists into Kaleo Workflow (Liferay 6.1 CE GA2), but how to get the ddlRecordId in workflow? I did some homework, I checked all Attributes in serviceContext, but there is no "ddlRecordId" in the serviceContext Attribute, only a key named "recordId" and its value always is 0. Also I can get some field value in the serviceContext Attributes, such as select and textarea. But what I want is the upload file field. Thanks.

long ddlRecordId = GetterUtil.getLong(serviceContext.getAttribute("ddlRecordId"));
DDLRecord ddlRecord = DDLRecordLocalServiceUtil.getRecord(ddlRecordId);
2

There are 2 best solutions below

0
On BEST ANSWER

In Liferay 6.1 the DDLRecordId is equivalent to the entryClassPK in the Workflow Context Variables. This could be a helpful documentation (read section about Workflow Context Variables)

So, you can get the upload file field by this way :

import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
import com.liferay.portlet.dynamicdatamapping.storage.Field;
import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.util.GetterUtil;

import java.io.File;
import java.io.Serializable

DDLRecord ddlRecord = DDLRecordLocalServiceUtil.getDDLRecord(GetterUtil.getLong(entryClassPK));
// get the upload field
Field field = ddlRecord.getField("field_attachment");
if (field != null){
    DDMStructure structure = field.getDDMStructure();
    Serializable fieldValue = field.getValue();
    String value = String.valueOf(fieldValue);
    if (!value.isEmpty()){
        JSONObject fileJSONObject = JSONFactoryUtil.createJSONObject(value);
        String fileName = fileJSONObject.getString("name");
        String filePath = fileJSONObject.getString("path");
        File file = DLStoreUtil.getFile(structure.getCompanyId(), 0L, filePath);
    }
}

I hope this will help more than one...

0
On

i had the same problem. I´ve been like a week trying to solve it and finally I get it. I hope it will solve yours to.

I had to recover all the DDLRecords in a list and find the one is ussing my workflow with the "recordSetId" attribute compared with the "recordSetId" of DDLRecord.

The final code is like this:

import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portlet.dynamicdatamapping.storage.Field;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
import com.liferay.portlet.dynamicdatalists.model.impl.DDLRecordImpl;
import com.liferay.portlet.dynamicdatalists.service.*;

long companyId = GetterUtil.getLong((String) workflowContext.get(WorkflowConstants.CONTEXT_COMPANY_ID));
String uuid = (String) workflowContext.get(WorkflowConstants.CONTEXT_USER_ID);

ServiceContext serviceContext = (ServiceContext) workflowContext.get(WorkflowConstants.CONTEXT_SERVICE_CONTEXT);

long ddlRecordId = GetterUtil.getLong(serviceContext.getAttribute("recordSetId"));
List ddlRecordList = DDLRecordLocalServiceUtil.getDDLRecords(0,DDLRecordLocalServiceUtil.getDDLRecordsCount());
for(DDLRecord o : ddlRecordList){
    if(o.getRecordSetId()==ddlRecordId){
        Field field = o.getField("status");

        String status = GetterUtil.getString(field.getValue());

        if (status.contains("not")) {
        returnValue = "No"
        }
        else {
        returnValue = "Yes"
        }
    }

}