Maximo

Maximo

Come for answers, stay for best practices. All we're missing is you.

 View Only
Expand all | Collapse all

Automation Script on New PR Line creation

  • 1.  Automation Script on New PR Line creation

    Posted Wed April 12, 2023 12:01 PM

    Hello All , 

    Need help creating an automation script. 
    There are 2 requirement both of which need to be executed when a user in the PR application tries to create PR Line . 
    1. If the Vendor at the PR level is not selected before creating the PR line then on creating a PR Line the user should get an error "Please provide the value for PR Vendor field before creating PR lines "
    2. Once the user has entered the value for Vendor for PR then on creation of the PR line there is a custom field "Custom_Vendor" which should default to the value selected for the Vendor field of the PR. 


    I am new to automation scripting and this is what I have so far. 
    I am trying to follow this link OBJECTNAME.NEW Script

    Based on that I have created a script PRLINE.NEW without a launch point as stated in the link above and have the below script added .

    owner = mbo.getOwner()
     
    if owner.getString("VENDOR") is None:
        service.error("vendor","Please provide value of Vendor for the PR")
     
    if owner and owner.getName()=="PR":
        mbo.setValue("CUSTOM_VENDOR",owner.getString("VENDOR"))

    But this does not seem to be working for both the scenarios . Any pointers on how to make this work or if there is an alternate way of doing this is highly appreciated.


    ------------------------------
    Naveen Yadav
    ------------------------------


  • 2.  RE: Automation Script on New PR Line creation
    Best Answer

    Posted Wed April 12, 2023 01:09 PM

    Here is a Python script for doing what you want to do. Note there are two launch points one that is on the Allow Object Creation and the other on Initialize Value. I have named them PRLINE.ADD.VALIDATE and PRLINE.ADD.COPY respectively and those names do matter as they are used in the script to determine the correct behavior.

    I have also included the Maximo Deployment Tools scriptConfig so you should be able to directly deploy the script using the VS Code extension https://marketplace.visualstudio.com/items?itemName=sharptree.maximo-script-deploy
    and the launch points will be created for you correctly.

    Let me know if you have any questions.

    Jason

    # As good practice define a main entry point to the script.
    # Focused on JavaScript but the principle remains.
    #
    # https://www.sharptree.io/blog/2021/2021-11-03-js-functions/
    
    def main():
        # There are two launch points, one to check if the record can be added
        # the other to copy the value.
        # In the Allow Object Creation launch point the global / implicit variable "mbo" does
        # not exist and in the Initialize Value launch point the "mboset" variable does not exist.
        # Define a common named variable called "check" and set it to the appropriate global variable.
        check = None
    
        if 'mboset' in globals():
            check = mboset
        elif 'mbo' in globals():
            check = mbo
    
        # Check that the mbo or mboset variable is available and that it is the expected PRLINE type.
        if check is not None and check.isBasedOn("PRLINE"):
            # Make sure we can get a reference to the owner and that it is a PR
            if check.getOwner() is not None and check.getOwner().isBasedOn("PR"):
                # If we are in the validate launch point then if the owner (PR) vendor is null then throw an error.
                if launchPoint == "PRLINE.ADD.VALIDATE":
                    if check.getOwner().isNull("VENDOR"):
                        service.error("msggroup","msgkey")
                elif launchPoint == "PRLINE.ADD.COPY":
                    # if in the initialize if the Mbo is to be added then copy the vendor from the parent PR
                    if mbo.toBeAdded():
                        check.setValue("CUSTOM_VENDOR", check.getOwner().getString("VENDOR"))
    
    # Call the main function
    main()
    
    # Configuration for the script and launch points to be deployed using the VS Code Maximo development tool extension
    # https://marketplace.visualstudio.com/items?itemName=sharptree.maximo-script-deploy
    scriptConfig="""{
        "autoscript": "PRLINE.ADD",
        "description": "Automation script to validate the PR has a vendor and copy it if it does",
        "version": "",
        "active": true,
        "logLevel": "ERROR",
        "scriptLaunchPoints": [
            {
                "launchPointName": "PRLINE.ADD.VALIDATE",
                "launchPointType": "OBJECT",
                "active": true,
                "description": "Automation script to validate the PR has a vendor",
                "objectName": "PRLINE",
                "allowObjectCreation": true
            },
            {
                "launchPointName": "PRLINE.ADD.COPY",
                "launchPointType": "OBJECT",
                "active": true,
                "description": "Automation script to copy the PR vendor to the line",
                "objectName": "PRLINE",
                "initializeValue": true
            }        
        ]
    }"""


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



  • 3.  RE: Automation Script on New PR Line creation

    Posted Thu April 13, 2023 02:39 AM

    Hello Jason, Thanks for the response . I will try this .



    ------------------------------
    Naveen Yadav
    ------------------------------



  • 4.  RE: Automation Script on New PR Line creation

    Posted Thu April 13, 2023 09:00 AM

    Hello Jason, I tried this and was able to successfully execute the automation script. Thanks a lot for this. 



    ------------------------------
    Naveen Yadav
    ------------------------------



  • 5.  RE: Automation Script on New PR Line creation

    Posted Thu April 13, 2023 05:49 PM
    A very well thoughtful code Jason! Thanks for sharing. 





  • 6.  RE: Automation Script on New PR Line creation

    Posted Wed April 12, 2023 01:11 PM
      |   view attached

    I have uploaded the file as well for in case the site mangles the formatting like it did in the email response.



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

    Attachment(s)

    py
    prline.add.py   2 KB 1 version


  • 7.  RE: Automation Script on New PR Line creation

    Posted Thu April 13, 2023 07:33 AM

    What version of Maximo are you on? I don't remember what version this was added in but if you're on an older 7.6.0.X version you may not have this event. Using .NEW is advantageous though because it fires once only when a record is being added and is done immediately. Jason's example will work on more versions but depends on initialize of the object which happens often. For example, when you copy the PRLINE to a POLINE we need to initialize the PRLINE object. He has the proper checks in place to avoid doing anything in that scenario but the script still will fire for each line.

    The first check "if owner.getString("VENDOR") is None:" is something to be careful of with automation scripts in general. Sometimes getString() returns an empty string which is not the same as None. What I tell customers to do on python/jython scripts is keep it simple. 

    if not owner.getString("VENDOR"):

    That will handle empty strings as well. Or use our MBO method like Jason did:

    if owner.isNull("VENDOR"):

    That should fix the first issue. The second piece should work so if you're still having issues, I'd try to set a different field (like remark) to see if it's some logic on your custom field that's causing issues.



    ------------------------------
    Steven Shull
    ------------------------------



  • 8.  RE: Automation Script on New PR Line creation

    Posted Thu April 13, 2023 09:09 AM

    One extra thought here: You may want to add near the top of the script an 

    if interactive()

    line, so that if you have automatic inventory replenishment active and creating PRs in the background, these don't error out.



    ------------------------------
    Travis Herron
    ------------------------------



  • 9.  RE: Automation Script on New PR Line creation

    Posted Thu April 13, 2023 09:19 AM
    Thanks Steven I didn't realize my mistake with the "getString() is None", so I learned something.  I really appreciate the feedback.  

    Jason






  • 10.  RE: Automation Script on New PR Line creation

    Posted Thu April 13, 2023 11:09 AM

    Hello Steven, The maximo version is the latest one ( 7.6.1.X). After correcting the first check the script worked fine . Thank You for the help here .



    ------------------------------
    Naveen Yadav
    ------------------------------