Vivek
Vivek

Reputation: 43

Service Now Geneva Rest API Java Code To attach file to incident

Can you please help with some sample java code to perform file (multiple content type like csv,jpg,xls,xlsx) attachment using REST API of ServiceNow geneva?

Thanks in advance for your help.

Upvotes: 0

Views: 1222

Answers (1)

Aryan
Aryan

Reputation: 61

I created following html code in ContentPage to upload file in ServiceNow Geneva version. I tested it, it's working well for TXT, DOCX,PDF, JPEG, GIF, ICONS etc. This can be used in UI pages as well. I would suggest you to make some modifications for security reasons.

  1. Remove new XMLHttpRequest() code block from UI script/ Content page and upload in Script Include.
  2. Create REST message and make a call from Script Include, such that code is not exposed to end user.


    File Upload test






        <script>
         function uploadFileTest()
         {
                  var obj=document.getElementById('fileUpload');
                  var file=obj.files[0];
                  var fileName=obj.files[0].name;
                  var encodedFile='';
                //Encode file
                 var reader = new FileReader();
                reader.onload = function(readerEvt) 
                {
                    var binaryString = readerEvt.target.result;
                    encodedFile= btoa(binaryString);
                   var xhr = new XMLHttpRequest();
                    xhr.withCredentials = true;
                    xhr.addEventListener("readystatechange", function () 
                    {
                      if (this.readyState === 4) {
                        console.log(this.responseText);
                      }
                    });
    
                    // xhr.open("POST","https://XXXXX.service-now.com/api/now/table/ecc_queue");
                    xhr.open("POST","https://XXXXX.service-now.com/ecc_queue.do?JSONv2");
    var data='{"agent":"AttachmentCreator","topic":"AttachmentCreator","name":"'+fileName+'","source":"u_itinerary_attchements:"'+sysid+'","payload":"'+encodedFile+'"}';
                    xhr.setRequestHeader("authorization", "Basic anBhdmFuOmpwYXZhbg==");
                    xhr.setRequestHeader("Content-Type", "application/json");       
                    xhr.setRequestHeader("Accept", "application/json");
                    xhr.setRequestHeader("cache-control", "no-cache");
                    xhr.send(data);
                };
                reader.readAsBinaryString(file);
    
    
         }
    
    
    
        </script>
    
        <input type='text' id='response' />
        </form>
    </body> 
    </html> 
    

Upvotes: 2

Related Questions