Guest User

Untitled

a guest
Oct 16th, 2017
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1.  
  2. //Convert attachment to base 64 string
  3.  
  4. var gsa = GlideSysAttachmentInputStream('e1d8bd45dbb103006cc25990cf96197b');
  5. var baos = new Packages.java.io.ByteArrayOutputStream();
  6. gsa.writeTo(baos);
  7. var base64Data = GlideStringUtil.base64Encode(baos.toByteArray());
  8.  
  9. //Convert the string to array chunks with 3736 chars length
  10. function splitString (string, size) {
  11. var re = new RegExp('.{1,' + size + '}', 'g');
  12. return base64Data.match(re);
  13. }
  14. var myarr = splitString(base64Data,3736);
  15.  
  16. //Send the array as Json via Rest
  17. var request = new sn_ws.RESTMessageV2();
  18. request.setEndpoint('https://dev24007.service-now.com/api/164669/valerianche');
  19. request.setHttpMethod('POST');
  20.  
  21. //Eg. UserName="admin", Password="admin" for this code sample.
  22. var user = 'admin';
  23. var password = 'Prohor@73';
  24. request.setBasicAuth(user,password);
  25.  
  26. request.setRequestHeader("Accept","application/json");
  27.  
  28. var obj = {};
  29. obj.payload = myarr;
  30. var attachment = {
  31. table_name: 'incident',
  32. table_sys_id: '46ee851edb3103001c917befbf96195c',
  33. file_size: '3636',
  34. table: 'incident',
  35. file_name: 'KOP'
  36. };
  37. obj.attachment = attachment;
  38.  
  39.  
  40. request.setRequestBody(JSON.stringify(obj));
  41.  
  42. request.execute();
  43. var response = request.execute();
  44. gs.log(response.getBody());
Add Comment
Please, Sign In to add comment