Script to run report and save as attachment

One of my customers asked me to take a kind of snapshot of work orders at a specific point of the workflow to keep track of special approvals and exact situation of the approved record.

I decided to implement such requirement through the implementation of an automation script to generate a BIRT report automatically and attach it the work order. I my scenario I have triggered it from the workflow but it can be also triggered from an escalation.

#-------------------------------------------------------------------------------
#
# Script: WOREPORTGENSAVE
#
# Launch Point: WORKORDER - Action
#
# Generate a BIRT report and save it as an attachment.
#
#-------------------------------------------------------------------------------

from psdi.mbo import Mbo, MboConstants
from psdi.util.logging import MXLoggerFactory
from psdi.server import MXServer

from com.ibm.tivoli.maximo.report.birt.admin import ReportAdminServiceRemote
from com.ibm.tivoli.maximo.report.birt.runtime import ReportParameterData

from java.io import FileOutputStream

#-------------------------------------------------------------------------------

logger = MXLoggerFactory.getLogger("maximo.dev")

reportName = "wodetail.rptdesign"    # name of the report to be lauched
appName = "WOTRACK"                  # application
reportFolder = "Attachments"         # folder where the report will be stored

#-------------------------------------------------------------------------------

logger.debug("Entering WOREPORTGENSAVE")

wonum = mbo.getString("WONUM")

logger.debug("Retrieving destination file and folder")

doctypesMboSet = MXServer.getMXServer().getMboSet('DOCTYPES', mbo.getUserInfo())
doctypesMboSet.setWhere("DOCTYPE='" + reportFolder + "'")

outputFilePath=doctypesMboSet.getMbo(0).getString('DEFAULTFILEPATH')

logger.debug("Output folder: " + outputFilePath)

outputFileName = wonum + "-details.pdf"
outputFile = outputFilePath + "/" + outputFileName

logger.debug("Output file: " + outputFile)
logger.debug("Generating report" + reportName)

# get the handler to BIRT report engine
reportAdminService = MXServer.getMXServer().lookup("BIRTREPORT")

# pass WONUM as report parameter
parameterData = ReportParameterData()
parameterData.addParameter("where", "(WORKORDER.wonum='"+wonum+"')")

reportBytes = reportAdminService.runReport(MXServer.getMXServer().getSystemUserInfo(), reportName, appName, parameterData, outputFileName, ReportAdminServiceRemote.OUTPUT_FORMAT_PDF)

# writes the binary data to the output file
fos = FileOutputStream(outputFile)
fos.write(reportBytes)
fos.close()

logger.debug("Creating DOCLINKS record")

doclinksMboSet = mbo.getMboSet("DOCLINKS")

doclinksMbo = doclinksMboSet.add()

doclinksMbo.setValue("URLTYPE", "FILE")
doclinksMbo.setValue("URLNAME", outputFile)
doclinksMbo.setValue("NEWURLNAME", outputFile)
doclinksMbo.setValue("DOCTYPE", reportFolder)
doclinksMbo.setValue("ADDINFO", True)
doclinksMbo.setValue("DESCRIPTION", "Test Results")
Script to run report and save as attachment

4 thoughts on “Script to run report and save as attachment

  1. Thanks for posting this Bruno… solved a specific business requirement.
    I found I had to modify the parameters line to use the where parameter in the report.
    parameterData.addParameter(“where”, “(WORKORDER.wonum = ‘” + wonum + “‘)”)

    Also had to remove the testtype field (undefined).
    I set the description as the outputFileName (instead of Test Results).

    Overall having your script to work from saved me a ton of time… thank you.

  2. Hi,
    If I want to upload the attachement to network path for example \\192.100.1.0\d$\folder\ with username and pssowrd to access the server. How I can do that.

    Please help.

  3. Thanks Bruno, this was very helpful. I added code below to send an email with the generated report as well as any other attachments on the application record.

    #Created report and attaching all documents————————————
    clMbo = mbo.createComm()
    clMbo.setValue(‘TEMPLATEID’, emailTemplateName)

    docLinksSet=mbo.getMboSet(“DOCLINKS”)
    docLink=docLinksSet.moveFirst()
    while docLink is not None:
    docInfoSet=docLink.getMboSet(“DOCINFO”)
    docInfo=docInfoSet.moveFirst()

    commLogDocsSet=clMbo.getMboSet(“COMMLOGDOCS”)
    commLogDoc=commLogDocsSet.add()
    commLogDoc.setValue(“docinfoid”,docInfo.getInt(“docinfoid”), mbo.NOACCESSCHECK|mbo.NOVALIDATION)
    commLogDoc.setValue(“urlname”,docInfo.getString(“urlname”), mbo.NOACCESSCHECK|mbo.NOVALIDATION)
    commLogDoc.setValue(“commlogid”,clMbo.getInt(“commlogid”), mbo.NOACCESSCHECK|mbo.NOVALIDATION)
    commLogDocLinksSet=clMbo.getMboSet(“DOCLINKS”)
    commLogDocLink=commLogDocLinksSet.add()
    commLogDocLink.setValue(“ownertable”,’COMMLOG’, mbo.NOACCESSCHECK|mbo.NOVALIDATION)
    commLogDocLink.setValue(“ownerid”,clMbo.getInt(“commlogid”), mbo.NOACCESSCHECK|mbo.NOVALIDATION)
    commLogDocLink.setValue(“doctype”,’Attachments’, mbo.NOACCESSCHECK|mbo.NOVALIDATION)
    commLogDocLink.setValue(“docinfoid”,docInfo.getInt(“docinfoid”), mbo.NOACCESSCHECK|mbo.NOVALIDATION)
    commLogDocLink.setValue(“document”,docLink.getString(“document”))

    docLink=docLinksSet.moveNext()
    try:
    clMbo.sendMessage()
    except:
    clMbo.setValue(“ISSENDFAIL”, True, MboConstants.NOACCESSCHECK)

Leave a Reply

Your email address will not be published.


Scroll to top