Save form data into CMS

I’ve created a form with a date field, and I want to save the selected date in the CMS upon form submission. However, I’m encountering an issue with the code — the workflow is returning null.

@Override
public void doAction(HstRequest request, HstResponse response) throws HstComponentException {
super.doAction(request, response);

	String time = request.getParameter("dateTimeInput");
	if (time == null) {
		response.setRenderParameter("error", "Missing form fields");
		return;
	}

	Session session = null;
	try {
		// Get the authenticated session
		session = request.getRequestContext().getSession();

		// Get or create submissions folder
		Node submissionsFolder = getOrCreateSubmissionsFolder(session);

		// Create new node with proper type
		Node newEntry = submissionsFolder.addNode("appointment-" + System.currentTimeMillis(),
				"myproject:appointment");

		// Set mandatory Hippo properties
		setRequiredHippoProperties(newEntry, "Appointment Submission");

		// Set your custom property
		newEntry.setProperty("myproject:time", time);

		// Save through workflow for proper document handling
		saveThroughWorkflow(session, newEntry);

		// Redirect after successful submission
		response.sendRedirect("/thankyou"); // Use relative
															// URL

	} catch (RepositoryException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (session != null && session.isLive()) {
			session.logout();
		}
	}
}

// Helper methods
private Node getOrCreateSubmissionsFolder(Session session) throws RepositoryException {
	String folderPath = "/content/documents/myproject/appointment";
	if (session.nodeExists(folderPath)) {
		return session.getNode(folderPath);
	}

	// Create folder structure if it doesn't exist
	Node parent = session.getNode("/content/documents/myproject");
	Node folder = parent.addNode("appointment", "hippostd:folder");
	folder.setProperty("hippostd:foldertype", new String[] { "appointments" });
	session.save();
	return folder;
}

private void setRequiredHippoProperties(Node node, String title) throws RepositoryException {
	// Required for all Hippo documents
	node.setProperty("hippotranslation:locale", "en");
	node.setProperty("hippo:availability", new String[] { "live", "preview" });
	node.setProperty("hippostd:foldertype", new String[] { "appointment" });
	node.setProperty("hippostd:state", "draft");
	node.setProperty("hippostd:title", title);

	// Recommended properties
	node.setProperty("hippo:docbase", node.getIdentifier());
}

private void saveThroughWorkflow(Session session, Node node) throws RepositoryException, WorkflowException {
    try {
        // Verify we have a proper document node
        if (!node.getPath().startsWith("/content/documents")) {
            throw new RepositoryException("Node must be under /content/documents");
        }

        // Ensure proper mixin exists (ADDED)
        Node handleNode = node.isNodeType("hippo:handle") ? node : node.getParent();
        if (!handleNode.isNodeType("hippostd:folder")) {
            handleNode.addMixin("hippostd:folder");
            session.save();
        }

        // Get workflow - now should work
        HippoWorkspace hippoWorkspace = (HippoWorkspace) session.getWorkspace();
        DocumentWorkflow workflow = (DocumentWorkflow) hippoWorkspace.getWorkflowManager()
            .getWorkflow("default", handleNode);

        if (workflow == null) {
            throw new RepositoryException("Workflow still null after mixin addition");
        }

        // Process workflow
        workflow.obtainEditableInstance();
        workflow.commitEditableInstance();
        if(!session.getUserID().equals("admin")){
			workflow.publish();
		}

    } catch (Exception e) {
        throw new RepositoryException("Workflow processing failed", e);
    }
}

Thanks
K Prasad

Hi,
You need a persistable session to write to the repository, see at Hst Component Persistable annotation and workflow - Bloomreach Experience Manager (PaaS/Self-Hosted) - The Fast and Flexible Headless CMS

HTH
Jeroen

Thanks, Jeroen.

I’m now able to create the document in CMS and save the data.

Hi Prasad,

We are happy you are part of Bloomreach Developer’s Community.
Thank you for reaching out to us.
We hope we have answered your queries and therefore marking this thread for Closure.
Looking forward to your continuous contribution towards our vibrant Bloomreach Community.

Thanks and Regards,
Rajesh