How to use/replace GlideSysAttachment for scoped app?

aklimenko
Mega Expert

We use GlideSysAttachment in these cases. Is it supported in scoped app or how can we rewrite it?

1. GlideSysAttachment.copy() to copy an attachment from one object to another

2. .getBytes() to get binary stream to be later sent to external server

13 REPLIES 13

john_roberts
Mega Guru

Copying attachments can be tackled with GlideRecords directly (this is basically what's happening in the java class). Of course it does require changes to the application access on system tables.


Not sure of the best workaround for getBytes().


Bobby Edmonds got any tricks up your sleeve?



/*


Requires read and create application access to all scopes on sys_attachment and sys_attachment_doc tables


*/


function copyAttachments(sourceTable, sourceID, targetTable, targetID) {


      var att = new GlideRecord("sys_attachment");


      var userImagePrefix = "ZZ_YY";


      var tables = [sourceTable, userImagePrefix + sourceTable];


      att.addQuery("table_name", tables);


      att.addQuery("table_sys_id", sourceID);


      att.query();


      gs.info(att.getEncodedQuery());


      gs.info("copying " + att.getRowCount());



      while (att.next()) {


              //copy attachment record


              var oldSysID = att.getUniqueValue();


              var targetName = targetTable;


              var sourceName = att.getValue("table_name");


              if (sourceName.startsWith(userImagePrefix))


                      targetName = userImagePrefix + targetTable;


   


              att.setValue("table_name", targetName);


              att.setValue("table_sys_id", targetID);


              var newSysID = att.insert();


   


              //copy attachment doc records


              var doc = new GlideRecord("sys_attachment_doc");


              doc.addQuery("sys_attachment", oldSysID);


              doc.query();


              while (doc.next()) {


                      doc.setValue("sys_attachment", newSysID);


                      doc.insert();


              }


      }


}


Thank you. The key phrase here "Of course it does require changes to the application access on system tables."


This is another example when default table restrictions would get in the way. I wonder if ServiceNow guys would be able to provide an easy way for apps to request extra permissions for special scenarios and still be published on App Store...


+1


Follow standards of Facebook, Android and iOS apps.


This application is requesting access to:


* Write Attachment


* Write and Delete Users


* Configure Incident


aklimenko
Mega Expert

To ServiceNow team: any additional information or non-hack solution for the problem?