How can we add attachment in email task from script task ?
I have created an excel file in script task and trying to attach same in email task as attachment but its not working
def metamodelReport = {
exportFileSuffix = “.xlsx”
baseURL = applicationApi.getInfo().getBaseUrl()
exportUser = userApi.getCurrentUser().get().getUserName()
//Creating the export file
def createExportFile = {->
isoFormat = new SimpleDateFormat("yyyy-MM-dd_HH:mm")
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"))
nowDate = isoFormat.format(new Date().getTime())
loggerApi.info("nowDate: ${nowDate}")
exportFileName = "MetaModel_Usage_Report_${nowDate}${exportFileSuffix}"
exportTempFile = File.createTempFile(exportUser,exportFileSuffix)
workbook = new XSSFWorkbook()
execution.setVariable('nowDate', nowDate)
}
def addFile = { ->
def fos = new FileOutputStream(exportTempFile)
workbook.write(fos)
fos.close()
def exportFileId = fileApi.addFile(AddFileRequest.builder().name(exportFileName).fileStream(new FileInputStream(exportTempFile)).build()).getId()
def exportFileUrl = "../rest/2.0/files/" + exportFileId
loggerApi.info("***File ${exportFileUrl} ***")
execution.setVariable('reportUrl', exportFileUrl)
execution.setVariable('exportFileName', exportFileName)
}
def populateSheet = { newSheet, header, rows ->
headerRow = newSheet.createRow(0)
XSSFFont boldFont= workbook.createFont();
boldFont.setBold(true);
XSSFCellStyle boldStyle = workbook.createCellStyle();
boldStyle.setFont(boldFont)
header.keySet().eachWithIndex{ col, i ->
headerCell = headerRow.createCell(i)
headerCell.setCellStyle(boldStyle)
headerCell.setCellValue(header[col].toString())
}
def rowNum = 1
rows.each { row ->
def reportRow = newSheet.createRow(rowNum)
header.keySet().eachWithIndex{ col, i ->
if(row.containsKey(col)){
val = row.get(col)
}else{
val = null
}
reportRow.createCell(i).setCellValue(val)
}
rowNum++
}
}
def createSheet = { sheetName, header, rows ->
def newSheet = workbook.createSheet(sheetName)
populateSheet(newSheet, header, rows)
//populateSheet(newSheet, header, rows.flatten())
}
def exportAssetTypes = { ->
def rows = []
def header = [:]
header.assetTypeId = "Resource Id"
header.assetTypeName = "Asset Type Name"
header.assetTypeDesc = "Asset Type Description"
header.assetCount = "Count of Assets"
findAssetTypesRequest = FindAssetTypesRequest.builder().topLevel(true).build()
allAssetTypes = assetTypeApi.findAssetTypes(findAssetTypesRequest).getResults()
createSheet("Asset Types", header, rows.flatten())
}
createExportFile()
exportAssetTypes()
addFile()
}
metamodelReport()
sourabhgiri1
Posted 3 years ago · Edited 1 year ago
0 comments