Maximo Open Forum

 DOCLINKS Automation Script

  • Administration
Jason Johnson's profile image
Jason Johnson posted 10-16-2024 12:43

We have a simple little script to add a URL attachment to an asset. However, it results in an error that it requires the Document field. When we add that to the script, it errors again. Is there something we're missing?

startUrl = "start URL here"
mFileName = mbo.getString("ASSETNUM")
finishUrl = "finish URL here"
compUrl = startUrl + mFileName + finishUrl

doclinksMboSet = mbo.getMboSet("DOCLINKS")
doclinksMbo = doclinksMboSet.add()

doclinksMbo.setValue("URLTYPE", "URL")
doclinksMbo.setValue("URLNAME", compUrl)
doclinksMbo.setValue("NEWURLNAME", compUrl)
doclinksMbo.setValue("DOCTYPE", "Attachments")
doclinksMbo.setValue("ADDINFO", False)
doclinksMbo.setValue("DESCRIPTION", mFileName)

Error message with script as is

Error message adding line 9

doclinksMbo.setValue("DOCUMENT", "ABC")

Mahadevan Ramakrishnan's profile image
Mahadevan Ramakrishnan

Hi There,

You are getting null pointer exception because there might be some white space or something on line 9,can you check if there is some special character or white space on the field that you set  with name as "document",could have come up when you copy pasted from editor.

mark robbins's profile image
mark robbins

A null pointer exception occurs because the code is trying to call a method on an object that hasn't been set. 

So using your code we can tell :

doclinksMbo.setValue("DOCUMENT", "ABC")

Doclinksmbo is the object that hasn't been set.

Setvalue is the method

So the question is why hasn't the Doclinksmbo been set? 

The object will be set when the code to get the doclinks mboset gets an mboset and when the add operation works 

So it looks like one of those has failed 

You should check the systemerr log file for errors eg unable to ADD record or missing relationship 

If you want to add code to handle this situation then add code like this:

If doclinksMbo is not None:

#Your code here 

Else:

#Log a warning about null mbo

Jason VenHuizen's profile image
Jason VenHuizen

Jason,


Here is an example of a script that will create a new document with a URL link to our website on save of an asset.  If the document with URL already exists it just reuses that DOCINFO record and adds the doc link.

I have included the scriptConfig variable so you can use our Maximo development extension for VS Code to deploy it directly to Maximo for testing. https://marketplace.visualstudio.com/items?itemName=sharptree.maximo-script-deploy

var MXServer = Java.type("psdi.server.MXServer");

var SqlFormat = Java.type("psdi.mbo.SqlFormat");

var DOCUMENT_NAME = "COMPURL";

main();

function main() {
    if(typeof mbo !== 'undefined' && mbo.isBasedOn("ASSET")) {
        var docLinksSet = mbo.getMboSet("DOCLINKS");

        var doclink = docLinksSet.moveFirst();
        var found = false;

        while(doclink){
            if(doclink.getString("DOCUMENT") === DOCUMENT_NAME){
                found = true;
                break;
            }
            doclink = docLinksSet.moveNext();
        }

        if(!found){
            var docInfoId = getOrCreateDocumentId();
            var doclink = docLinksSet.add();
            doclink.setValue("DOCTYPE", "Attachments");
            doclink.setValue("DOCINFOID", docInfoId);    
            doclink.setValue("DOCUMENT", DOCUMENT_NAME);         
        }
    }
}

function getOrCreateDocumentId(){
    // create the document if it does not exist.
    var docInfoSet = MXServer.getMXServer().getMboSet("DOCINFO", mbo.getUserInfo());
    try{
        var sqlf = new SqlFormat("document=:1");
        sqlf.setObject(1, "DOCINFO", "DOCUMENT", DOCUMENT_NAME);

        docInfoSet.setWhere(sqlf.format());
        var doc = docInfoSet.moveFirst();
        if(!doc){
            doc = docInfoSet.add();
            doc.setValue("DOCTYPE", "Attachments");
            doc.setValue("DOCUMENT", DOCUMENT_NAME);
            doc.setValue("DESCRIPTION", "Company URL");            
            doc.setValue("URLTYPE", "URL");
            doc.setValue("URLNAME", "https://sharptree.io");

            docInfoSet.save();
        }

        return doc.getUniqueIDValue();
    }finally{
        try{
            docInfoSet.close();
            docInfoSet.cleanup();
        }catch(ignore){}
    }

}

var scriptConfig = {
    "autoscript": "ASSET.DOCLINKS",
    "description": "Adds a doclink to an asset on save.",
    "version": "1.0.0",
    "active": true,
    "logLevel": "ERROR",
    "scriptLaunchPoints": [
        {
            "launchPointName": "ASSET.DOCLINKS",
            "launchPointType": "OBJECT",
            "description": "Adds a doclink to an asset on save.",
            "active": true,
            "objectName": "ASSET",            
            "save": true,
            "add":true,
            "update":true,
            "beforeSave":true
        }
    ]
};