//Copy the specified fields from the item being edited to the given item. // //@param Field:longtext CopyField //The long text field to copy from one item to the other // //@param Field:attachment CopyFieldAttachments //The Attachmnet field associated with the long text field to copy from one item to the other // //@param String Item //The item to copy to // function print(s) { eb.setMessageCategory("DEBUG"); eb.print(s); } function cleanDir(dir) { if (dir.isDirectory()) { var children = dir.listFiles(); for( var i = 0; i < children.length; i++ ) { cleanDir(children[i]) } } // Delete the top level dir and low level files var deleted = (new java.io.File(dir.getAbsolutePath()))["delete"](); } // copies a text field from source to target items function copyField(source,target,field) { var sourceFieldContents = source.getNewFieldValue(field); target.setFieldValue(textField,sourceFieldContents); } function copyAttachments(source, target, attachField) { // Setup a temporary directory to extract the attachments var tmpDir = new java.io.File(integrityDir + fs + "data" + fs + "tmp" + fs + source.getID()); var attachmentBeans = source.getAttachmentBeans(attachField); if( null != attachmentBeans && attachmentBeans.length > 0 ) { var attachDir = new java.io.File(tmpDir.getAbsolutePath() + fs + attachField); // Create the temporary directory (per item/per field), if it doesn't exist if( ! attachDir.isDirectory() ) { attachDir.mkdirs(); } // Extract each attachment and add it to the new clone for( var i = 0; i < attachmentBeans.length; i++ ) { var attachBean = attachmentBeans[i]; var outputFile = new java.io.File(attachDir.getAbsolutePath() + fs + attachBean.getName()); var outputStream = new java.io.FileOutputStream(outputFile); attachBean.getContent(outputStream) ; outputStream.close(); target.addAttachment(outputFile.getAbsolutePath(), attachBean.getContentType(), attachField); } } // Clean up the temporary directory created cleanDir(tmpDir); } var eb = bsf.lookupBean("siEnvironmentBean"); var fs = java.lang.System.getProperty( "file.separator" ); var params = bsf.lookupBean("parametersBean"); var integrityDir=eb.getInstallDirectory(); var sb = bsf.lookupBean("imServerBean"); var textField = params.getParameter("CopyField"); var attachmentField = params.getParameter("CopyFieldAttachments"); var targetItem = params.getParameter("Item"); var source = bsf.lookupBean("imIssueDeltaBean"); var target = sb.getIssueDeltaBean(targetItem); print("Copy fields " + textField + " and " + attachmentField + " from " + source.getID() + " to " + targetItem); copyField(source, target, textField); copyAttachments(source, target, attachmentField);