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

6 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)

  4. Hi Bruno,

    You are a savior. The script saved me a lot of time and I had no clue if this was possible.

    I am trying to print a BIRT report when my inspection form is complete and attaching the PDF report back to the work order. All of these are working fine except that I cannot pass the inspection result number from the script to the report. I am writing the script on the “Inspection Result” object on save.

    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

    #raise TypeError(“inside script”)

    logger = MXLoggerFactory.getLogger(“maximo.dev”)

    reportName = “Inspection_result.rptdesign” # name of the report to be lauched
    appName = “APAWOTN” # application
    reportFolder = “Attachments” # folder where the report will be stored

    resultnum = mbo.getString(“RESULTNUM”);
    status = mbo.getString(“STATUS”);
    siteId = mbo.getString(“SITEID”);
    orgId = mbo.getString(“ORGID”);
    woId =resultnum;
    workorder =’Test’;
    #raise TypeError(status)

    logger.debug(“Retrieving destination file and folder”)
    if status == ‘COMPLETED’:
    #raise TypeError(status)
    doctypesMboSet = MXServer.getMXServer().getMboSet(‘DOCTYPES’, mbo.getUserInfo())
    doctypesMboSet.setWhere(“DOCTYPE='” + reportFolder + “‘”)
    outputFilePath=doctypesMboSet.getMbo(0).getString(‘DEFAULTFILEPATH’)

    outputFileName = resultnum + “-details.pdf”
    outputFile = outputFilePath + “/” + outputFileName

    # get the handler to BIRT report engine
    reportAdminService = MXServer.getMXServer().lookup(“BIRTREPORT”)
    # pass WONUM as report parameter
    parameterData = ReportParameterData()
    parameterData.addParameter(“where”, “(INSPECTIONRESULT.resultnum='”+resultnum+”‘)”)
    parameterData.addParameter(“where”, “(w.wonum=’M15841204′)”)
    #parameterData.addParameter(“where”, “(wokrorder.wonum=’M15841204’)”)
    #raise TypeError (str(“where”, “(ISR.RESULTNUM='”+resultnum+”‘)”))
    #parameterData.addParameter(“where”, “(INSPECTIONRESULT.RESULTNUM='”+resultnum+”‘)”)
    #parameterData.addParameter(“where”, “(INSPRESULTFORM.RESULTNUM='”+resultnum+”‘ )”)

    if (len (resultnum) > 0):
    query = “siteid ='”+siteId+”‘ and resultnum ='”+resultnum+”‘ and referenceobject =’MULTIASSETLOCCI’ ”
    workorderSet = mbo.getMboSet(“$INSPECTIONRESULT”,”INSPECTIONRESULT”,query)
    if ( workorderSet is not None):
    workorderMbo=workorderSet.getMbo(0);
    if (workorderMbo is not None):
    workorder =workorderMbo.getString(“PARENT”);
    if (len(workorder)>0):
    query1 =”siteid ='”+siteId+”‘ and wonum ='”+workorder+”‘ ”
    woIdSet = mbo.getMboSet(“$WORKORDER”,”WORKORDER”,query1)
    if (woIdSet is not None):
    #raise TypeError (str(woIdSet.getMbo(0).getInt(“WORKORDERID”)))
    woId =woIdSet.getMbo(0).getString(“WORKORDERID”);

    reportBytes = reportAdminService.runReport(MXServer.getMXServer().getSystemUserInfo(), reportName, appName, parameterData, outputFileName, ReportAdminServiceRemote.OUTPUT_FORMAT_PDF)
    # writes the binary data to the output file
    #raise TypeError (str(reportBytes))
    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”, resultnum)
    doclinksMbo.setValue(“OWNERTABLE”,”WORKORDER”, mbo.NOACCESSCHECK|mbo.NOVALIDATION)
    doclinksMbo.setValue(“OWNERID”,woId, mbo.NOACCESSCHECK|mbo.NOVALIDATION)

    #raise TypeError(str(doclinksMbo.getString(“DESCRIPTION”)))

    1. Changing the whereclause like below helped fix my issue.

      parameterData.addParameter(“resultnum”, resultnum )

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top