Attach Files to Defects

Applies to QAComplete 14.5, last modified on August 06, 2024

The code below uses the Attachments_Add operation to attach a file to a defect in QAComplete. In a similar way, you can attach files to releases, requirements and other items. Just specify the appropriate values for the EntityCode (item type) and FKId (item ID) properties of the Attachment object.

C#

string login = "[email protected]";
string password = "p@ssword";

int projID = 10372;
string filePath = "C:\\Work\\doc.pdf";
string entityCode = "Bugs";
int defectID = 17;
string fileName = System.IO.Path.GetFileName(filePath);
string title = "Sample PDF file";

ServiceSoapClient service = new ServiceSoapClient();

// Preparing AuthenticationData
LoginInfo loginInfo = service.GetLoginInfo("", login, password);
AuthenticationData authData = new AuthenticationData();
authData.AppCode = loginInfo.AppCode;
authData.UserId = loginInfo.UserId;
authData.PassCode = password;
authData.DeptId = loginInfo.DeptId;
authData.ProjId = projID;

// Preparing the Attachment object
Attachment attachment = new Attachment();
attachment.EntityCode = entityCode;
attachment.FKId = defectID;
attachment.FileName = fileName;
attachment.Title = title;

// Reading a file to be attached
byte[] attachFile = System.IO.File.ReadAllBytes(filePath);

// Adding an attachment with the file
int attachId = service.Attachments_Add(authData, attachment, attachFile, false);
Console.WriteLine("Attachment ID:" + attachId);

Java

import java.io.File;
import java.io.RandomAccessFile;

String login = "[email protected]";
String password = "p@ssword";

int projId = 10372;
File sourceFile = new File("C://Work//doc.pdf");
String fileName = sourceFile.getName();
String filePath = sourceFile.getPath();
String entityCode = "Bugs";
int defectID = 17;
String title = "Sample PDF file";

ServiceSoap service = new Service().getServiceSoap12();

// Preparing AuthenticationData
LoginInfo loginInfo = service.getLoginInfo("", login, password);
AuthenticationData authData = new AuthenticationData();
authData.setAppCode(loginInfo.getAppCode());
authData.setUserId(loginInfo.getUserId());
authData.setPassCode(password);
authData.setDeptId(loginInfo.getDeptId());
authData.setProjId(projId);

// Preparing the attachment object
Attachment attachment = new Attachment();
attachment.setEntityCode(entityCode);
attachment.setFKId(defectID);
attachment.setFileName(fileName);
attachment.setTitle(title);

try {
  // Reading a file to attach
  RandomAccessFile file = new RandomAccessFile(filePath, "r");
  byte[] bytes = new byte[(int)file.length()];
  file.read(bytes);

  // Adding an attachment with the file
  int attachId = service.attachmentsAdd(authData, attachment, bytes, false);
  System.out.println("Attachment ID: " + attachId);
}
catch (Exception e) {
  e.printStackTrace();
}

See Also

SOAP API Introduction
How To
Attachments_Add Operation

Highlight search results