Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X
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??
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);
}
}
tmpDir What to assign in this?
As I am getting error cannot identified it
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.
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
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:
But i want filename of the attachment which is linked with long text field. Will it work?
No, it won't give that. If an image is pasted into the rich text field, then the attachment name will begin with MKS. But a user could add an attachment manually, then add the image in the field.
I think the only way for sure would be to parse the rich text field looking for filename in the img tags, and then compare those to the attachment names. I'm sorry, that's beyond my javascripting skills.
Edited to Add:
Actually, another way to do this would be to have an attachment field strictly for the long text field, and a different attachment field for other attachments.