Page 1 of 1

Setting user id of an attachment

PostPosted: Mon Sep 21, 2015 4:54 pm
by grahamb
We have a script that generates a task of a new category from an existing task, and it moves file attachments from the original task to the new task, however since the upgrade from 4.0.19 to 5.0.5, the user associated with the task seems to randomly change. The random user is then added to the participants list of the new task and gets mailouts for each attachment and any future changes on the new task,

We are still using the older .bsh scripts, this is an excerpt fro the script that handles the moving of the attachments:

Code: Select all
/* Move any attachments */
if (task.hasAttachments()) {
  int notCopied = 0;
  Map ma = task.getAttachsTaskOrMessage();
  Set s=ma.entrySet();
  Iterator it=s.iterator();
  while (it.hasNext()) {
    Map.Entry m = (Map.Entry);
    SecuredTaskAttachmentBean key = (SecuredTaskAttachmentBean)m.getKey();
    boolean value = (boolean)m.getValue(); // True for Task attachment, False for Message attachment
    int maxSize = Integer.parseInt(UDFManager.getTaskUDFValue(sc, configTaskId, "maxAttachmentCopySize"));
    if (key.getSize() < maxSize*1024*1024) {

      ByteArrayInputStream data = new ByteArrayInputStream(AttachmentManager.getAttachment(sc, key.getId()));
      AttachmentManager.createAttachment( sc,
      AttachmentManager.deleteAttachment( sc, key.getId());
    } else {

  if (notCopied > 0) {
    newMsgDesc = "There is one large file";
    if (notCopied > 1) {
      newMsgDesc = "There are " + Integer.toString(notCopied) + " large files";
    newMsgDesc = newMsgDesc + " attached to the original report (#" + MyTaskNumber + ").";
    MessageManager.createMessage(sc, newTaskId, newNoteId, newMsgDesc,
                               null, null, null, null, null, null, null, false);

Even if the attachment user isn't randomly set, it seems to default to the user running the script, I'd like it to be the original user that uploaded the attachment. How can I do that?

Re: Setting user id of an attachment

PostPosted: Wed Sep 23, 2015 1:05 pm
by dpochtarev
You should create attachments for every user, who uploaded files.
Code: Select all
                taskId,  /* task id*/
                null,      /*message id*/
                att.getUserId(),    /* attachment owner id*/

You can get attachment owner id from SecuredAttachmentBean.

Re: Setting user id of an attachment

PostPosted: Thu Sep 24, 2015 3:23 pm
by grahamb
We are using the user id from the attachment though. key is the SecuredTaskAttachmentBean instance, and we're passing key.getUserId() into createAttachment().

Your example uses TriggerManager to create the attachment, whereas we're using an instance of SecuredAttachmentAdapterManager (AttachmentManager). Are we doing that wrong?

Re: Setting user id of an attachment

PostPosted: Thu Sep 24, 2015 9:06 pm
by Petr
Hello. The user is taken from SessionContext sc in your scripts, for this reason you have such problem.
So you can create the SessionContext for each user who upload the file or you need to upload the file directly through kernel by AttachmentManager how the Dmitry shows you.

Re: Setting user id of an attachment

PostPosted: Thu Jan 28, 2016 7:19 pm
by grahamb
Thank you for the info, I've now got this working, belatedly.