# Creating documents using Groovy script

**URL:** <https://community.bloomreach.com/t/creating-documents-using-groovy-script/2906>\
**Category:** Experience Manager (PaaS/OnPrem)\
**Created:** [December 7, 2021, 9:56am UTC](https://community.bloomreach.com/t/creating-documents-using-groovy-script/2906 "2021-12-07T09:56:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yeshwanth\_Lagala](https://yyz1.discourse-cdn.com/flex027/user_avatar/community.bloomreach.com/yeshwanth_lagala/32/777_2.png) [@Yeshwanth\_Lagala](https://community.bloomreach.com/u/Yeshwanth_Lagala)\
**Post date:** [December 7, 2021, 9:56am UTC](https://community.bloomreach.com/t/creating-documents-using-groovy-script/2906/1 "2021-12-07T09:56:38Z")

</div>

Hi All,

I am trying to create a document using groovy script. Below is the code. when i tried executing the script, it throws constraint violation exception. Could you please suggest me what went wrong ?

**Error details:**  
Failed to save session - javax.jcr.nodetype.ConstraintViolationException: /content/documents/riversource/experience-pages/onecolumn/xpage/xpage[2]: mandatory property {[http://www.onehippo.org/jcr/hippostdpubwf/nt/1.0](http://www.onehippo.org/jcr/hippostdpubwf/nt/1.0)}lastModificationDate does not exist

**Groovy Script**  
package org.hippoecm.frontend.plugins.cms.admin.updater

import org.onehippo.repository.update.BaseNodeUpdateVisitor  
import javax.jcr.Node  
import javax.jcr.RepositoryException  
import javax.jcr.Session

class UpdaterTemplate extends BaseNodeUpdateVisitor {

boolean logSkippedNodePaths() {  
return false // don’t log skipped node paths  
}

boolean skipCheckoutNodes() {  
return false // return true for readonly visitors and/or updates unrelated to versioned content  
}

Node firstNode(final Session session) throws RepositoryException {  
return null // implement when using custom node selection/navigation  
}

Node nextNode() throws RepositoryException {  
return null // implement when using custom node selection/navigation  
}

boolean doUpdate(Node node) {

```
log.debug "Updating node ${node.path}"   

Node draft = node.addNode("xpage","riversource:SimpleDocument")

```

if(hasMixin(draft, “hippostd:publishableSummary”)) {  
log.debug “hippostd:publishableSummary Already added”  
} else {  
draft.addMixin(“hippostd:publishableSummary”)  
}  
if(hasMixin(draft, “hippostd:publishable”)) {  
log.debug “hippostd:publishable Already added”  
} else {  
draft.addMixin(“hippostd:publishable”)  
}  
if(hasMixin(draft, “hippostd:container”)) {  
log.debug “hippostd:container Already added”  
} else {  
draft.addMixin(“hippostd:container”)  
}  
if(hasMixin(draft, “hippo:derived”)) {  
log.debug “hippo:derived Already added”  
} else {  
draft.addMixin(“hippo:derived”)  
}  
if(hasMixin(draft, “hippo:container”)) {  
log.debug “hippo:container Already added”  
} else {  
draft.addMixin(“hippo:container”)  
}

```
if(hasMixin(draft, "hippostdpubwf:audittrace")) {
  log.debug "hippostdpubwf:audittrace Already added"
} else {
  draft.addMixin("hippostdpubwf:audittrace")
}

```

if(hasMixin(draft, “hippotranslation:translated”)) {  
log.debug “hippotranslation:translated Already added”  
} else {  
draft.addMixin(“hippotranslation:translated”)  
}

if(hasMixin(draft, “mix:referenceable”)) {  
log.debug “mix:referenceable Already added”  
} else {  
draft.addMixin(“mix:referenceable”)  
}

```
if(hasMixin(draft, "hippostdpubwf:document")) {
  log.debug "document Already added"
} else {
  draft.addMixin("hippostdpubwf:document")
}

if(hasMixin(draft, "hst:xpagemixin")) {
  log.debug "hst:xpagemixin Already added"
} else {
  draft.addMixin("hst:xpagemixin")
}

if(hasMixin(draft, "hippostd:relaxed")) {
  log.debug "hippostd:relaxed Already added"
} else {
  draft.addMixin("hippostd:relaxed")
}

if(hasMixin(draft, "hippo:named")) {
  log.debug "hippo:named Already added"
} else {
  draft.addMixin("hippo:named")
}

draft.setProperty("hippostd:state","draft")
draft.setProperty("hippostd:stateSummary","live")  

draft.setProperty("hippostdpubwf:createdBy","admin")
draft.setProperty("hippostdpubwf:creationDate","2021-12-06T06:51:21.626Z")

draft.setProperty("hippostdpubwf:lastModificationDate","2021-12-06T06:51:21.626Z")
draft.setProperty("hippostdpubwf:lastModifiedBy","admin")

draft.setProperty("hippotranslation:locale","en")
draft.setProperty("hippotranslation:id","")

return false

```

}

boolean undoUpdate(Node node) {  
throw new UnsupportedOperationException(‘Updater does not implement undoUpdate method’)  
}

boolean hasMixin(Node node, String type) throws RepositoryException {

```
if (!node.hasProperty("jcr:mixinTypes")) {
  return false;
}
 NodeTypeManager ntMgr = node.getSession().getWorkspace().getNodeTypeManager();

for (Value value : node.getProperty("jcr:mixinTypes").getValues()) {
NodeType nt = ntMgr.getNodeType(value.getString());
if (nt.isNodeType(type)) {
  return true;
}
}
return false;
}

```

}

Thanks,  
Yeshwanth.

---

<div class="post-metadata">

**Author:** ![machak](https://yyz1.discourse-cdn.com/flex027/user_avatar/community.bloomreach.com/machak/32/1298_2.png) [@machak](https://community.bloomreach.com/u/machak)\
**Post date:** [December 7, 2021, 12:30pm UTC](https://community.bloomreach.com/t/creating-documents-using-groovy-script/2906/2 "2021-12-07T12:30:37Z")

</div>

> [@Yeshwanth\_Lagala](#):
>
> `draft.setProperty("hippostdpubwf:lastModificationDate","2021-12-06T06:51:21.626Z")`

I don’t think this will work, it should be a Calendar type and not a string…
