Skip to main content
1-Visitor
Jul 13, 2018 10:32 AM UTC
Question

Copy a long text field

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??

7 replies

5-Regular Member
Jul 16, 2018 2:49 PM UTC

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);
}
}

 

 

Translate
1-Visitor
Jul 17, 2018 11:03 AM UTC
tmpDir What to assign in this?
As I am getting error cannot identified it

 

Translate
5-Regular Member
Jul 17, 2018 6:30 PM UTC

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.

Translate
1-Visitor
Jul 18, 2018 6:09 AM UTC

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

Translate
5-Regular Member
Jul 18, 2018 1:21 PM UTC

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

Translate