A very well thoughtful code Jason! Thanks for sharing.
Original Message:
Sent: 4/12/2023 1:09:00 PM
From: Jason VenHuizen
Subject: RE: Automation Script on New PR Line creation
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
------------------------------
Original Message:
Sent: Wed April 12, 2023 06:46 AM
From: Naveen Yadav
Subject: Automation Script on New PR Line creation
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
------------------------------