Need to send Attachement via REST message along with other data to Azure Devops

Bhuvanesh Babu
Giga Expert

Hi All and @Ankur Bawiskar , 

I have refered and gone through many posts in community but was not successful, 

Requirement is I have a BR that creates a ticket in Azure Devops if a checkbox is enabled, this works fine but I also need to send attachments (be it any type / multiple attachments) if it is present in that particular Incident. 

Please let me know if its possible and how to achive this.

Note: Below is the code snippet (replaced data/fields with 'x')

Regards,

Babu

var hasattachments = current.hasAttachments();
gs.addErrorMessage(hasattachments);
	

    var incUrl = gs.getProperty('glide.servlet.uri') + 'incident.do?sys_id=' + current.sys_id;

    if (plmGroup)
        incDesc += 'Group: ' + plmGroup + '<br/>';
    if (plmRole)
        incDesc += 'Role: ' + plmRole + '<br/>';
    incDesc += current.description + '<br/>' + '<div><a href=' + '\'' + incUrl + '\'' + '>Link to ServiceNow</a> Incident</div>';

    try {
        var r = new sn_ws.RESTMessageV2('xxx', 'POST');
        if (current.assignment_group == 'xxxxxxxxxxx') { 
        
        r.setStringParameterNoEscape('x', incShortDesc.replaceAll('"', "'"));
        r.setStringParameterNoEscape('x', reqEmail);
        r.setStringParameterNoEscape('x', incNumber);
        r.setStringParameterNoEscape('x', incPriority);
        r.setStringParameterNoEscape('x', path);
        r.setStringParameterNoEscape('x', 'bug');

		if (hasattachments == true){


		}
        var response = r.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();
        var subject = "Integration " + httpStatus;
        if (httpStatus != "200") {
            gs.eventQueue('eventname', current, subject, responseBody);
        }
        current.work_notes = 'requestBody' + r.getRequestBody() + '\n\nhttpStatus: ' + httpStatus + '\n\nresponseBody: ' + responseBody;
    } catch (ex) {
        var message = ex.message;
    }
9 REPLIES 9

panda1
Kilo Guru

Do you want to upload the file to azure Devops's WorkItem ?

 First, you need to upload the attachment to the azure Devops server

View this API

Attachments - Create - REST API (Azure DevOps Work Item Tracking) | Microsoft Docs

get the URL from the response

 

second

Update with other data through the update WorkItem API,Add - relation to request body

Work Items - Update - REST API (Azure DevOps Work Item Tracking) | Microsoft Docs

request's json like this

  [
      {
        "op": "add",
        "path": "/relations/-",
        "value": {
          "rel": "AttachedFile",
          "url": "response'url",
          "attributes": {
            "comment": "Spec for the work"
            "name":"your attachment name"
          }
        }
      }
   ]

it creates a new record in Azure Devops with the fieldvalues from this incident, along with the values the attachment should also be copied/sent/attached (of any type) to the new Azure Devops ticket.

Regards,

Babu

Yes, in azure Devops, ticket is called WorkItem.

As I said above, uploading an attachment first needs to be uploaded to the azure Devops server

Attachments - Create - REST API (Azure DevOps Work Item Tracking) | Microsoft Docs

Using this API, it will upload the attachment to the server. Then return an attachment URL.

Then, when you create a ticket ticket, you can add the URI to the requestbody,The format of JSON is like what I said above.

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can get base64encoded data of that attachment present on record and include in some json key of the request body

in which json key 3rd party expects that file content?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader