Use form to create a page and also change class properties

I’m trying to use form to create a page based on defined template and at the same time set value for class properties of the new page.

Here’s my code:

{{velocity}}
#if("$!request.docName" != '')
  ## Request for creating a new instance
  #set($uin1 = $services.uin.getNext('InternalAuditReporting'))
  #set($docName = "${request.docName}$uin1")
  #set($targetDocName = "${request.spaceName}.${request.Project}.${docName}")
  #set($reference = $services.model.createDocumentReference('Wiki', 'QualityManagement', 'InternalAuditReporting', ${request.Project}, ${docName}))
  #set($document  = $xwiki.getDocument($reference))
  $document.set('Project', ${request.Project})
  $document.set('ID', $uin1)
  $document.save()
  #if(!$xwiki.exists($targetDocName) && $xwiki.hasAccessLevel('edit', $xcontext.user, $targetDocName))
    $response.sendRedirect($xwiki.getURL($targetDocName, 'edit', "template=${escapetool.url($request.template)}&parent=${escapetool.url($request.parent)}"))
    ## Stop processing, since we already sent a redirect.
    #stop
  #end
#end

= Create Report =

#if("$!targetDocName" != '' && $xwiki.exists($targetDocName))
  {{warning}}The target document already exists. Please choose a different name, or [[view the existing document>>$targetDocName]]{{/warning}}
#elseif("$!targetDocName" != '')
  {{warning}}You don't have permission to create that document{{/warning}}
#end

{{html}}
  <form action="" id="newdoc" method="post">
    <div>
      <input type="hidden" name="parent" value="${doc.fullName}"/>
      <input type="hidden" name="sheet" value="1"/>
      <input type="hidden" name="template" value="QualityManagement.InternalAuditReporting.InternalAuditReportingTemplate"/>
      <input type="hidden" name="spaceName" value="QualityManagement.InternalAuditReporting"/>
      <input type="hidden" name="docName" value="InternalAuditChecklist"/>
      <p>Title: <input type="text" name="Title" size="100" column="3"/></p>
      <p>Type: <input type="text" name="Type" size="100"/></p>
      <p>Project: <select name="Project">
        <option value="2018-2019">2018-2019</option>
        <option value="2017-2018">2017-2018</option>
      </select></p>
      <span class="buttonwrapper"><input type="submit" value="Create" class="button"/></span>
    </div>
  </form>
{{/html}}
{{/velocity}}

So far, When I click the button, it was able to go to the edit view of the new page. However, the properties are not set. Please help.

Hi @samsun387!

I see the issue in $reference creation as the list of spaces is wrongly used (https://github.com/xwiki/xwiki-platform/blob/master/xwiki-platform-core/xwiki-platform-model/src/main/java/org/xwiki/model/script/ModelScriptService.java#L110) . Then, in the main wiki, the wiki parameter could be empty ''.
In this case, it would be $services.model.createDocumentReference('', ['QualityManagement', 'InternalAuditReporting', ${request.Project}], ${docName}) the right format (notice the [] and the empty first parameter).
More details about references you can find here: https://extensions.xwiki.org/xwiki/bin/view/Extension/Model%20Module.

Hope it helps,
Alex