<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>How to programmatically create a form</title>
  <link rel="self" href="https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=117188535" />
  <subtitle>How to programmatically create a form</subtitle>
  <id>https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=117188535</id>
  <updated>2026-04-05T11:11:50Z</updated>
  <dc:date>2026-04-05T11:11:50Z</dc:date>
  <entry>
    <title>How to programmatically create a form</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=117188534" />
    <author>
      <name>Anupam Shrivastava</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=117188534</id>
    <updated>2019-09-17T13:09:08Z</updated>
    <published>2019-09-17T13:09:08Z</published>
    <summary type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;It is possible to programmatically create a form based on a specific element set.&lt;br&gt;This is done through the "DDMFormInstanceLocalServiceUtil.addFormInstance" method.&lt;br&gt;&lt;pre&gt;&lt;code&gt;// Here goes your existing element set Id
long ddmStructureId = 33869;

// Initialize form instance
long ddmInstanceId = CounterLocalServiceUtil.increment(DDMFormInstance.class.getName());
DDMFormInstance ddmFormInstance = DDMFormInstanceLocalServiceUtil.createDDMFormInstance(ddmInstanceId);
ddmFormInstance.setStructureId(ddmStructureId);

// Retrieve the element set
DDMStructure ddmStructure = null;
try {
	ddmStructure = DDMStructureLocalServiceUtil.getDDMStructure(ddmStructureId);
} catch (PortalException e) {
	e.printStackTrace();
}

// Convert it into formValues needed for addFormInstance
DDMForm ddmForm = ddmStructure.getDDMForm();
DDMFormValues ddmFormValues = _ddmFormValuesFactory.create(actionRequest, ddmForm);

// Set name and description for the new form
Map&amp;lt;locale, string&amp;gt; name = new HashMap&amp;lt;locale, string&amp;gt;();
name.put(LocaleUtil.US, "New Form");
Map&amp;lt;locale, string&amp;gt; description = new HashMap&amp;lt;locale, string&amp;gt;();
description.put(LocaleUtil.US, "New Form Description");

// Create the form
try {
	DDMFormInstanceLocalServiceUtil.addFormInstance(themeDisplay.getUserId(), themeDisplay.getScopeGroupId(), ddmStructureId, name, description, ddmFormValues, serviceContext);
} catch (PortalException e) {
	e.printStackTrace();
}&amp;lt;/locale,&amp;gt;&amp;lt;/locale,&amp;gt;&amp;lt;/locale,&amp;gt;&amp;lt;/locale,&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;br&gt;See attached sample code for more details.&lt;br&gt;&lt;strong&gt;&lt;strong&gt;Disclaimer:&amp;nbsp;&lt;/strong&gt;&lt;/strong&gt;Please note that this is a sample of how to extend your portlet and should only be used as a reference.&lt;/body&gt;&lt;/html&gt;</summary>
    <dc:creator>Anupam Shrivastava</dc:creator>
    <dc:date>2019-09-17T13:09:08Z</dc:date>
  </entry>
</feed>
