Skip to main content
1-Visitor
July 13, 2018
Question

Copy a long text field

  • July 13, 2018
  • 2 replies
  • 3835 views

I am trying to copy a long text field from 1 type to another through a trigger. The text field is rich content enabled and it has a image. Whjle copying I pictures are coming broken. The reason could be that in first type in the attachment field there is that image. While in second there is not. I tried to use getAttachment and addAttachment function But in addAttachment function I am getting error File does not exist in the disk. Any idea how to solve this??

2 replies

5-Regular Member
July 16, 2018

You are right that the problem in the second item is that the images are not in the attachments.

 

To copy an attachment from one item to the other, you need to put the attachment contents into a file, then add the file using AddAttachment.

 

For example, the following code copies all attachments from the Attachments field:

 

 

var attachmentField = "Attachments"
var attachmentBeans = rb.getAttachmentBeans(attachmentField);
if( null != attachmentBeans && attachmentBeans.length > 0 )
{
var attachDir = new java.io.File(tmpDir.getAbsolutePath() + fs + attachmentField);
// Create the temporary directory, if it doesn't exist
if( ! attachDir.isDirectory() )
{
attachDir.mkdirs();
}

// Extract each attachment and add it to the new item
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(), attachmentField);
}
}

 

 

1-Visitor
July 17, 2018
tmpDir What to assign in this?
As I am getting error cannot identified it

 

5-Regular Member
July 17, 2018

Apologies. I've attached my entire script now. there were some definitions outside of the code snippet I pasted.

 

note: I tested this with 11.2. I set it up as a rule trigger and copied the long text and attachment field from the item being edited to an item I set in the parameters. Obviously, you would determine the source and target items differently in a real world situation.

1-Visitor
July 18, 2018

Thanks Awalsh it worked. Can u tell me is there a way by which we can know the filename which is attached to the long text field as attachment. Since it is copying all the attachments present in that particular type which I dont want

5-Regular Member
July 18, 2018

You can get the filename with getName() from the attachment bean.

 

FYI: you can see all the methods and beans in the java docs. From your Integrity Server homepage, click on "Event Trigger Java Documentation", then select the bean you want from the left hand side:

 

homepage.PNG

AttachmentBeanJavaDocumentation