Maximo

 View Only
Expand all | Collapse all

Using DOCLINKS DOCUMENTDATA attribute in Automation Scripts (or any other way to include a base64 string for the attached doc)

  • 1.  Using DOCLINKS DOCUMENTDATA attribute in Automation Scripts (or any other way to include a base64 string for the attached doc)

    Posted Thu June 20, 2024 04:54 PM

    I'm setting up a synchronous integration to an external system and (since I've never been down this road before) the path of least resistance seemed to be to create a json message via an automation script and send it to my endpoint.  It worked great until I got to the part where I needed to include attached documents.  I had previously setup an asynchronous integration via a Publish channel and it had no problem providing the attached documents using the non-persistent DOCUMENTDATA attribute.  That same attribute returns nothing when called from an automation script.

    I don't know if there is any kind of workaround to get that DOCUMENTDATA data into an automation script or if I need to find a different path to meeting this objective.  I'm assuming whatever is populating that non-persistent attribute is happening at the time the object structure is called and if I don't go through the os, I'm out of luck.  I'm checking with support, but figured I'd see if anyone else has encountered the same issue.



    ------------------------------
    Brandon Fisher
    ------------------------------


  • 2.  RE: Using DOCLINKS DOCUMENTDATA attribute in Automation Scripts (or any other way to include a base64 string for the attached doc)

    IBM Champion
    Posted Thu June 20, 2024 08:32 PM

    Here you go, let me know if you have any questions.

    MXServer = Java.type("psdi.server.MXServer");
    HTTPHandler = Java.type("psdi.iface.router.HTTPHandler");
    XMLUtils = Java.type("psdi.iface.util.XMLUtils");   
    
    JavaString = Java.type("java.lang.String");
    HashMap = Java.type("java.util.HashMap");
    Base64 = Java.type("java.util.Base64");
    
    var ENDPOINT_URL = "YOUR_ENDPOINT_URL_HERE";
    
    main();
    
    /**
     * Main function that is called when the script is run. This provides an entry point for the script.
     */
    function main() {
        if (typeof mbo !== "undefined" && mboIsOrHasDocLinks(mbo)) {        
            if(mbo.isBasedOn("DOCLINKS")){
                postDocLinks(mbo);
            }else{
                
                var docLinksSet = mbo.getMboSet("DOCLINKS");
                var docLinks = docLinksSet.moveFirst();
    
                while(docLinks){
                    postDocLinks(docLinks);
                    docLinks = docLinksSet.moveNext();
                }
            }
        }
    }
    
    /**
     * Gets the provided DOCLINKS Mbo's document and posts it to the provided endpoint.
     * @param {*} docLinks the DOCLINKS Mbo to get the document from and post to the endpoint.
     * @returns the response from the endpoint as a string, this can be parsed to a JSON object with JSON.parse().
     */
    function postDocLinks(docLinks) {
        if(docLinks != null){                
            var internalUrlType = MXServer.getMXServer().getMaximoDD().getTranslator().toInternalString("URLTYPE", docLinks.getString("URLTYPE"));
            if (internalUrlType.equals("FILE")) {            
                
                // The XMLUtils.readXMLFileToBytes utility function reads the file from the URLNAME attribute and returns it as a byte array.
                // The XML part is a bit of a misnomer as it was originally designed to read XML files, but it can be used to read any file.
                var data = {
                    "name":docLinks.getString("DOCINFO.DOCUMENT"),
                    "description":docLinks.getString("DOCINFO.DESCRIPTION"),
                    "data":Base64.getEncoder().encodeToString(XMLUtils.readXMLFileToBytes(docLinks.getString("DOCINFO.URLNAME")))
                }        
                
                return post(ENDPOINT_URL ,data);
            }
        }   
    }
    
    /**
     * Posts the provided data object to the provided URL, with optional basic authentication with a username and password.
     * @param {String} url the URL to post to the data to.
     * @param {String} data the data to post as a JSON object that will be stringified.
     * @param {String} userName optional username for basic authentication.
     * @param {String} password optional password for basic authentication.
     * @returns the response from the URL as a string, this can be parsed to a JSON object with JSON.parse().
     */
    function post(url,data, userName, password){
    
        var handler = new HTTPHandler();
        var metaData = new HashMap();
        metaData.put("URL", url);
        metaData.put("HTTPMETHOD", "POST");
        metaData.put("CONTENTTYPE", "application/json");
    
        if (userName != null) {
          metaData.put("USERNAME", userName);
          metaData.put("PASSWORD", password);
        } 
        // this converts the response to a String, assuming it is JSON you can parse it to an object with JSON.parse()
        return new JavaString(handler.invoke(metaData, JSON.stringify(data,null,4).getBytes()));
    }
    
    /**
     * Checks if the provided Mbo is based on DOCLINKS or has a relation to DOCLINKS.
     * @param {psdi.mbo.Mbo} checkMbo the Mbo to check if it is based on DOCLINKS or has a relation to DOCLINKS.
     * @returns true if the provided Mbo is based on DOCLINKS or has a relation to DOCLINKS, false otherwise.
     */
    function mboIsOrHasDocLinks(checkMbo) {
        if (checkMbo != null) {
            return checkMbo.isBasedOn("DOCLINKS") || checkMbo.getThisMboSet().getMboSetInfo().getRelationInfo("DOCLINKS")!=null;
        } else {
            return false;
        }
    }
    
    // Script Config for use with the Sharptree Maximo Developer Tools VS Code Extension
    // https://marketplace.visualstudio.com/items?itemName=sharptree.maximo-script-deploy
    var scriptConfig={
        "autoscript": "UPLOAD.DOCLINKS",
        "description": "Upload Work Order Doclinks to End Point",
        "version": "",
        "active": true,
        "logLevel": "ERROR",
        "scriptLaunchPoints": [
            {
                "launchPointName": "UPLOAD.DOCLINKS",
                "launchPointType": "OBJECT",
                "active": true,
                "description": "Upload Work order Doclinks to End Point on every save",
                "objectName": "WORKORDER",
                "save": true,
                "add": true,
                "update": true,
                "delete": false,
                "beforeSave": true
            }
        ]
    };


    ------------------------------
    Jason VenHuizen
    https://sharptree.io
    https://opqo.io
    ------------------------------



  • 3.  RE: Using DOCLINKS DOCUMENTDATA attribute in Automation Scripts (or any other way to include a base64 string for the attached doc)

    IBM Champion
    Posted Thu June 20, 2024 08:36 PM
      |   view attached

    When TechXchange sends out the email it mangles the script formatting so I have also included it as a file attachment.



    ------------------------------
    Jason VenHuizen
    https://sharptree.io
    https://opqo.io
    ------------------------------

    Attachment(s)

    js
    upload.doclinks.js   4 KB 1 version


  • 4.  RE: Using DOCLINKS DOCUMENTDATA attribute in Automation Scripts (or any other way to include a base64 string for the attached doc)

    Posted Fri June 21, 2024 09:07 AM

    Thanks so much!  It works great.



    ------------------------------
    Brandon Fisher
    ------------------------------