Message Boards

Liferay 7.2 - How to create a ddmFormValues to add a DDLRecord?

Fabio Carvalho, modified 4 Years ago.

Liferay 7.2 - How to create a ddmFormValues to add a DDLRecord?

Junior Member Posts: 81 Join Date: 6/25/19 Recent Posts
Hi,

I am trying to add DDLRecords programmatically. I am trying to add with the following method:
DDLRecord ddlRecord = DDLRecordLocalServiceUtil.addRecord(
    theme.getUserId(), 
    theme.getScopeGroupId(), 
    ddlRecordSet.getRecordSetId(), 
    DDLRecordLocalServiceUtil.getDDLRecordsCount(),
    ddmFormValues, 
    context
);

But I don't now how to create the ddmFormValues param. I know that I could get the ddmFormValues like this:
DDMForm ddmForm = DDMUtil.getDDMForm(serializedJSONDDMForm);
DDMFormValues ddmFormValues = DDMUtil.getDDMFormValues(ddmForm, serializedJSONDDMFormValues)

But I can't seem to find any example of serializedJSONDDMFormValues to mimic it. How should I create my DDLRecord?

EDIT: I have created a Service Wrapper to Override the addRecord method from DDLRecordLocalServiceWrapper and print the DDMUtil.getDDMFormValuesJSONString(ddmFormValues). I have got the following:
{
    "availableLanguageIds":[
        "en_US"
    ],
    "defaultLanguageId":"en_US",
    "fieldValues":[
        {
            "instanceId":"joro",
            "name":"TextExample1",
            "value":{
                "en_US":"teste"
            }
        },
        {
            "instanceId":"yofm",
            "name":"IntegerExample1",
            "value":{
                "en_US":"1"
            }
        },
        {
            "instanceId":"wwgf",
            "name":"BooleanExample1",
            "value":{
                "en_US":"true"
            }
        }
    ]
}

That's something. Now, what is this instanceId in the JSON? Could I use a GUID and hardcode my instanceId with this GUID?
Serafeim Kourlos, modified 4 Years ago.

RE: Liferay 7.2 - How to create a ddmFormValues to add a DDLRecord?

New Member Posts: 2 Join Date: 11/15/19 Recent Posts
Same issue here. Any help ?
Shruti Mishra, modified 4 Years ago.

RE: Liferay 7.2 - How to create a ddmFormValues to add a DDLRecord?

New Member Posts: 22 Join Date: 4/24/15 Recent Posts
Hi,
Any luck with this ? I'm trying something similar.
Gaurav SIngh, modified 3 Years ago.

RE: Liferay 7.2 - How to create a ddmFormValues to add a DDLRecord?

New Member Post: 1 Join Date: 5/8/20 Recent Posts
I was facing the same issue, but then after going through the liferay source code and some brainstorming i was able to save record programmatically , Lets start from the beginning for full understanding as there is no tutorial available for this.

1.  How DDL works is, you add Data Definition in which you design the form (its a sort of template) and then publish it.

2. So once you have a template/Data Definition ready, you create a dynamic data list and select the same data definition.

3. From the UI its pretty easy you just have to add dynamic data list display portlet (Liferay OOTB feature) on any page and then fill up the form.

4. But now if you need to add same records which you did in dynamic data list display portlet programmatically, You need to follow this steps



Code for adding any record

DDLRecordLocalServiceUtil.addRecord(userId, groupId, recordSetId, displayIndex, ddmFormValues, serviceContext)

Now lets understand these parameters,

a. userId and groupId is simple

b. serviceContext :- ServiceContext serviceContext = ServiceContextFactory.getInstance(DDLRecord.class.getName(), actionrequest);

RECORDSET_NAME will be the name of the Dynamic Data List you created. Hardcode this or save this in configuration file

DynamicQuery DDLRecordSetDQ = DDLRecordSetLocalServiceUtil.dynamicQuery();
DDLRecordSetDQ.add(RestrictionsFactoryUtil.like("name","%"+RECORDSET_NAME+"%"));
DDLRecordSet recordSet= (DDLRecordSet) DDLRecordSetLocalServiceUtil.dynamicQuery(DDLRecordSetDQ).get(0);


c. recordSetId:- recordSet.getRecordSetId()

d. displayIndex:- DDLRecordLocalServiceUtil.getDDLRecordsCount()

e. ddmFormValues:- This is the hardest part

DDMFormValues ddmFormValues = new DDMFormValues(recordSet.getDDMStructure().getDDMForm());

Here I fetched DDMForm from the recordset, and now you basically have the structure of the data definition you created in the first step. next step would be setting values to this DDMForm.

Set<Locale> localeSet = new HashSet<Locale>();
localeSet.add(Locale.forLanguageTag("en-US"));

ddmFormValues .setAvailableLocales(localeSet);
ddmFormValues .setDefaultLocale(Locale.forLanguageTag("en-US"));
ddmFormValues .setDDMFormFieldValues(ddmFormFieldValues);
Now we need to add ddmFormFieldValues its a list

List<DDMFormFieldValue> ddmFormFieldValues = new ArrayList<DDMFormFieldValue>();
DDMFormFieldValue ddmFormFieldValue = new DDMFormFieldValue();

ddmFormFieldValue .setName("name of the variable");// Set the name of value you need to set

Value value = new LocalizedValue();//Implementation class of interface value
value.addString(Locale.forLanguageTag("en-US"), "value of the variable");
ddmFormFieldValue .setValue(value); //Set the value but its an interface so we need its implementation class
ddmFormFieldValues.add(DDMFormFieldValue);

and then pass this ddmFormFieldValues to the  add record function

DDLRecordLocalServiceUtil.addRecord(userId, groupId, recordSetId, displayIndex, ddmFormValues, serviceContext)

I hope this helps, If you have any doubts you can ping me on gaurav.bajatha@ugamsolutions.com

Regards,
Gaurav Singh Bajatha