Skip to main content
deleted 11 characters in body
Source Link
User1974
  • 198
  • 1
  • 19

Part 1 is (herePart 1 (it focused on general cleanup).)

Part 1 is here (it focused on general cleanup).

(Part 1 focused on general cleanup.)

If we're obfuscating the URL, let's do so in a standards-compliant way.
Source Link
mdfst13
  • 21.7k
  • 6
  • 33
  • 68
#What the script does:
#     1. Takes the X&Y coordinates of a work order in Maximo
#     2. Generates a URL from the coordinates
#     3. Executes the URL via a separate script/library (LIB_HTTPCLIENT)
#     4. Performs a spatial query in an ESRI REST feature service (a separate GIS system)
#     5. JSON text is returned to Maximo with the attributes of the zone that the work 
#        order intersected
#     6. The zone number is parsed from the JSON text via a separate script/library 
#        (LIB_PARSE_JSON)
#     7. Inserts the zone number into the work order record
#
#Notes about libraries:
#     - Unfortunately, I'm unable to add external Python libraries (like urllib or 
#       urlparse) to my Maximo/Jython implementation.
#     - Furthermore, some libraries (exampple: JSON) that are normally included in Python
#       and Jython have been excluded from my Maximo implementation. I don't have control
#       over this.
#     - Instead, if there is functionality missing from my Jython implementation, I need 
#       to use Java classes to fill in the gap: 
#       https://www.ibm.com/support/knowledgecenter/ja/SSEUEX_2.0.2/com.ibm.javaeuc.doc/com/ibm/json/java/package-summary.html
#
#Notes about creating the URL:
#     - Previous code reviewers have strongly suggested "...splitting the URL creation 
#       into different variables and adding a looping function that adds query parameters 
#       to the URL."
#     - I've tried doing this, but I found that it made the url parts harder for me to 
#       manage, not easier. Maybe I'm nuts, but I've tried it, and figured it was overly 
#       complicated, so I think we can skip this idea for now. Thanks all the same.

from psdi.mbo import MboConstants
from java.util import HashMap

field_to_update = "ZONE"

def get_coords():
    """
    Get the y and x coordinates(UTM projection) from the WOSERVICEADDRESS table
    via the SERVICEADDRESS system relationship.
    The datatype of the LatitdeY and LongitudeX fields is decimal.
    """
    laty  = mbo.getDouble("SERVICEADDRESS.LatitudeY")
    longx = mbo.getDouble("SERVICEADDRESS.LongitudeX")

    return laty, longx


def is_latlong_valid(laty, longx):
    #Verify if the numbers are legitimate UTM coordinates
    return (4000000 <= laty <= 5000000 and
            600000 <= longx <= 700000)


def make_url(laty, longx):
    """
    Assemble the URL (including the longx and the laty).
    Note: The coordinates are flipped in the url.
    Consider replacing the field wildcard(*) with the specific field name (zone)
    """
    url="http://somethingexample.com/arcgis/rest/services/Something/Zones/MapServer/15/query?geometry={0}%2C{1}&geometryType=esriGeometryPoint&spatialRel=esriSpatialRelIntersects&outFields=*&returnGeometry=false&f=pjson".format(str(longx),str(laty))

    return url


def fetch_zone(url):
    # Get the JSON text from the feature service (the JSON text contains the zone value).
    ctx = HashMap()
    ctx.put("url", url)
    service.invokeScript("LIB_HTTPCLIENT", ctx)
    json_text = str(ctx.get("response"))

    # Parse the zone value from the JSON text
    ctx = HashMap()
    ctx.put("json_text", json_text)
    service.invokeScript("LIB_PARSE_JSON", ctx)
    parsed_val = str(ctx.get("parsed_val"))

    return parsed_val


#Is this sort of generic error checking acceptable (try/except)?
try:
    laty, longx = get_coords()
    if not is_latlong_valid(laty, longx):
        service.log('Invalid coordinates')
    else:
        url = make_url(laty, longx)
        zone = fetch_zone(url)
    
        #Insert the zone value into the zone field in the work order
        mbo.setValue(field_to_update, zone, MboConstants.NOACCESSCHECK)
except:
    """
    If the script fails, then set the field vaule to null.
    Reason: If the work order coordinates have changed (thereby triggering this script), 
    then setting the zone's field value to null is better than leaving it as the wrong 
    zone number (if the coordinates of the work order changed, that means
    that the zone number likely changed too).
    Furthermore, if there is an error in the script, then an error message will pop up 
    and prevent users from creating/saving work orders. 
    We don't want that! (error messages would render the system unusable until fixed)
    So, I figure, I'll set the field value to null if there is an error.
    """
    mbo.setValue(field_to_update, "", MboConstants.NOACCESSCHECK) #Should I
    # set the field value to "" or to None? 
    service.log("An exception occurred")
    
#What the script does:
#     1. Takes the X&Y coordinates of a work order in Maximo
#     2. Generates a URL from the coordinates
#     3. Executes the URL via a separate script/library (LIB_HTTPCLIENT)
#     4. Performs a spatial query in an ESRI REST feature service (a separate GIS system)
#     5. JSON text is returned to Maximo with the attributes of the zone that the work 
#        order intersected
#     6. The zone number is parsed from the JSON text via a separate script/library 
#        (LIB_PARSE_JSON)
#     7. Inserts the zone number into the work order record
#
#Notes about libraries:
#     - Unfortunately, I'm unable to add external Python libraries (like urllib or 
#       urlparse) to my Maximo/Jython implementation.
#     - Furthermore, some libraries (exampple: JSON) that are normally included in Python
#       and Jython have been excluded from my Maximo implementation. I don't have control
#       over this.
#     - Instead, if there is functionality missing from my Jython implementation, I need 
#       to use Java classes to fill in the gap: 
#       https://www.ibm.com/support/knowledgecenter/ja/SSEUEX_2.0.2/com.ibm.javaeuc.doc/com/ibm/json/java/package-summary.html
#
#Notes about creating the URL:
#     - Previous code reviewers have strongly suggested "...splitting the URL creation 
#       into different variables and adding a looping function that adds query parameters 
#       to the URL."
#     - I've tried doing this, but I found that it made the url parts harder for me to 
#       manage, not easier. Maybe I'm nuts, but I've tried it, and figured it was overly 
#       complicated, so I think we can skip this idea for now. Thanks all the same.

from psdi.mbo import MboConstants
from java.util import HashMap

field_to_update = "ZONE"

def get_coords():
    """
    Get the y and x coordinates(UTM projection) from the WOSERVICEADDRESS table
    via the SERVICEADDRESS system relationship.
    The datatype of the LatitdeY and LongitudeX fields is decimal.
    """
    laty  = mbo.getDouble("SERVICEADDRESS.LatitudeY")
    longx = mbo.getDouble("SERVICEADDRESS.LongitudeX")

    return laty, longx


def is_latlong_valid(laty, longx):
    #Verify if the numbers are legitimate UTM coordinates
    return (4000000 <= laty <= 5000000 and
            600000 <= longx <= 700000)


def make_url(laty, longx):
    """
    Assemble the URL (including the longx and the laty).
    Note: The coordinates are flipped in the url.
    Consider replacing the field wildcard(*) with the specific field name (zone)
    """
    url="http://something.com/arcgis/rest/services/Something/Zones/MapServer/15/query?geometry={0}%2C{1}&geometryType=esriGeometryPoint&spatialRel=esriSpatialRelIntersects&outFields=*&returnGeometry=false&f=pjson".format(str(longx),str(laty))

    return url


def fetch_zone(url):
    # Get the JSON text from the feature service (the JSON text contains the zone value).
    ctx = HashMap()
    ctx.put("url", url)
    service.invokeScript("LIB_HTTPCLIENT", ctx)
    json_text = str(ctx.get("response"))

    # Parse the zone value from the JSON text
    ctx = HashMap()
    ctx.put("json_text", json_text)
    service.invokeScript("LIB_PARSE_JSON", ctx)
    parsed_val = str(ctx.get("parsed_val"))

    return parsed_val


#Is this sort of generic error checking acceptable (try/except)?
try:
    laty, longx = get_coords()
    if not is_latlong_valid(laty, longx):
        service.log('Invalid coordinates')
    else:
        url = make_url(laty, longx)
        zone = fetch_zone(url)
    
        #Insert the zone value into the zone field in the work order
        mbo.setValue(field_to_update, zone, MboConstants.NOACCESSCHECK)
except:
    """
    If the script fails, then set the field vaule to null.
    Reason: If the work order coordinates have changed (thereby triggering this script), 
    then setting the zone's field value to null is better than leaving it as the wrong 
    zone number (if the coordinates of the work order changed, that means
    that the zone number likely changed too).
    Furthermore, if there is an error in the script, then an error message will pop up 
    and prevent users from creating/saving work orders. 
    We don't want that! (error messages would render the system unusable until fixed)
    So, I figure, I'll set the field value to null if there is an error.
    """
    mbo.setValue(field_to_update, "", MboConstants.NOACCESSCHECK) #Should I
    # set the field value to "" or to None? 
    service.log("An exception occurred")
    
#What the script does:
#     1. Takes the X&Y coordinates of a work order in Maximo
#     2. Generates a URL from the coordinates
#     3. Executes the URL via a separate script/library (LIB_HTTPCLIENT)
#     4. Performs a spatial query in an ESRI REST feature service (a separate GIS system)
#     5. JSON text is returned to Maximo with the attributes of the zone that the work 
#        order intersected
#     6. The zone number is parsed from the JSON text via a separate script/library 
#        (LIB_PARSE_JSON)
#     7. Inserts the zone number into the work order record
#
#Notes about libraries:
#     - Unfortunately, I'm unable to add external Python libraries (like urllib or 
#       urlparse) to my Maximo/Jython implementation.
#     - Furthermore, some libraries (exampple: JSON) that are normally included in Python
#       and Jython have been excluded from my Maximo implementation. I don't have control
#       over this.
#     - Instead, if there is functionality missing from my Jython implementation, I need 
#       to use Java classes to fill in the gap: 
#       https://www.ibm.com/support/knowledgecenter/ja/SSEUEX_2.0.2/com.ibm.javaeuc.doc/com/ibm/json/java/package-summary.html
#
#Notes about creating the URL:
#     - Previous code reviewers have strongly suggested "...splitting the URL creation 
#       into different variables and adding a looping function that adds query parameters 
#       to the URL."
#     - I've tried doing this, but I found that it made the url parts harder for me to 
#       manage, not easier. Maybe I'm nuts, but I've tried it, and figured it was overly 
#       complicated, so I think we can skip this idea for now. Thanks all the same.

from psdi.mbo import MboConstants
from java.util import HashMap

field_to_update = "ZONE"

def get_coords():
    """
    Get the y and x coordinates(UTM projection) from the WOSERVICEADDRESS table
    via the SERVICEADDRESS system relationship.
    The datatype of the LatitdeY and LongitudeX fields is decimal.
    """
    laty  = mbo.getDouble("SERVICEADDRESS.LatitudeY")
    longx = mbo.getDouble("SERVICEADDRESS.LongitudeX")

    return laty, longx


def is_latlong_valid(laty, longx):
    #Verify if the numbers are legitimate UTM coordinates
    return (4000000 <= laty <= 5000000 and
            600000 <= longx <= 700000)


def make_url(laty, longx):
    """
    Assemble the URL (including the longx and the laty).
    Note: The coordinates are flipped in the url.
    Consider replacing the field wildcard(*) with the specific field name (zone)
    """
    url="http://example.com/arcgis/rest/services/Something/Zones/MapServer/15/query?geometry={0}%2C{1}&geometryType=esriGeometryPoint&spatialRel=esriSpatialRelIntersects&outFields=*&returnGeometry=false&f=pjson".format(str(longx),str(laty))

    return url


def fetch_zone(url):
    # Get the JSON text from the feature service (the JSON text contains the zone value).
    ctx = HashMap()
    ctx.put("url", url)
    service.invokeScript("LIB_HTTPCLIENT", ctx)
    json_text = str(ctx.get("response"))

    # Parse the zone value from the JSON text
    ctx = HashMap()
    ctx.put("json_text", json_text)
    service.invokeScript("LIB_PARSE_JSON", ctx)
    parsed_val = str(ctx.get("parsed_val"))

    return parsed_val


#Is this sort of generic error checking acceptable (try/except)?
try:
    laty, longx = get_coords()
    if not is_latlong_valid(laty, longx):
        service.log('Invalid coordinates')
    else:
        url = make_url(laty, longx)
        zone = fetch_zone(url)
    
        #Insert the zone value into the zone field in the work order
        mbo.setValue(field_to_update, zone, MboConstants.NOACCESSCHECK)
except:
    """
    If the script fails, then set the field vaule to null.
    Reason: If the work order coordinates have changed (thereby triggering this script), 
    then setting the zone's field value to null is better than leaving it as the wrong 
    zone number (if the coordinates of the work order changed, that means
    that the zone number likely changed too).
    Furthermore, if there is an error in the script, then an error message will pop up 
    and prevent users from creating/saving work orders. 
    We don't want that! (error messages would render the system unusable until fixed)
    So, I figure, I'll set the field value to null if there is an error.
    """
    mbo.setValue(field_to_update, "", MboConstants.NOACCESSCHECK) #Should I
    # set the field value to "" or to None? 
    service.log("An exception occurred")
    
deleted 26 characters in body
Source Link
User1974
  • 198
  • 1
  • 19
#What the script does:
#     1. Takes the X&Y coordinates of a work order in Maximo
#     2. Generates a URL from the coordinates
#     3. Executes the URL via a separate script/library (LIB_HTTPCLIENT)
#     4. Performs a spatial query in an ESRI REST feature service (a separate GIS system)
#     5. JSON text is returned to Maximo with the attributes of the zone that the work 
#        order intersected
#     6. The zone number is parsed from the JSON text via a separate script/library 
#        (LIB_PARSE_JSON)
#     7. Inserts the zone number into the work order record
#
#Notes about libraries:
#     - Unfortunately, I'm unable to add external Python libraries (like urllib or 
#       urlparse) to my Maximo/Jython implementation.
#     - Furthermore, some libraries (exampple: JSON) that are normally included in Python
#       and Jython have been excluded from my Maximo implementation. I don't have control
#       over this.
#     - Instead, if there is functionality missing from my Jython implementation, I need 
#       to use Java classes to fill in the gap: 
#       https://www.ibm.com/support/knowledgecenter/ja/SSEUEX_2.0.2/com.ibm.javaeuc.doc/com/ibm/json/java/package-summary.html
#
#Notes about creating the URL:
#     - Previous code reviewers have strongly suggested "...splitting the URL creation 
#       into different variables and adding a looping function that adds query parameters 
#       to the URL."
#     - I've tried doing this, but I found that it made the url parts harder for me to 
#       manage, not easier. Maybe I'm nuts, but I've tried it, and figured it was overly 
#       complicated, so I think we can skip this idea for now. Thanks all the same.

from psdi.mbo import MboConstants
from java.util import HashMap

field_to_update = "ZONE"

def get_coords():
    """
    Get the y and x coordinates(UTM projection) from the WOSERVICEADDRESS table
    via the SERVICEADDRESS system relationship.
    The datatype of the LatitdeY and LongitudeX fields is decimal.
    """
    laty  = mbo.getDouble("SERVICEADDRESS.LatitudeY")
    longx = mbo.getDouble("SERVICEADDRESS.LongitudeX")

    return laty, longx


def is_latlong_valid(laty, longx):
    #Verify if the numbers are legitimate UTM coordinates
    return (4000000 <= laty <= 5000000 and
            600000 <= longx <= 700000)


def make_url(laty, longx):
    """
    Assemble the URL (including the longx and the laty).
    Note: The coordinates are flipped in the url.
    Consider replacing the field wildcard(*) with the specific field name (zone)
    """
    url="http://vsgisapprod:6080something.com/arcgis/rest/services/Operational_ServicesSomething/Lagan_ServiceZonesZones/MapServer/515/query?geometry={0}%2C{1}&geometryType=esriGeometryPoint&spatialRel=esriSpatialRelIntersects&outFields=*&returnGeometry=false&f=pjson".format(str(longx),str(laty))

    return url


def fetch_zone(url):
    # Get the JSON text from the feature service (the JSON text contains the zone value).
    ctx = HashMap()
    ctx.put("url", url)
    service.invokeScript("LIB_HTTPCLIENT", ctx)
    json_text = str(ctx.get("response"))

    # Parse the zone value from the JSON text
    ctx = HashMap()
    ctx.put("json_text", json_text)
    service.invokeScript("LIB_PARSE_JSON", ctx)
    parsed_val = str(ctx.get("parsed_val"))

    return parsed_val


#Is this sort of generic error checking acceptable (try/except)?
try:
    laty, longx = get_coords()
    if not is_latlong_valid(laty, longx):
        service.log('Invalid coordinates')
    else:
        url = make_url(laty, longx)
        zone = fetch_zone(url)
    
        #Insert the zone value into the zone field in the work order
        mbo.setValue(field_to_update, zone, MboConstants.NOACCESSCHECK)
except:
    """
    If the script fails, then set the field vaule to null.
    Reason: If the work order coordinates have changed (thereby triggering this script), 
    then setting the zone's field value to null is better than leaving it as the wrong 
    zone number (if the coordinates of the work order changed, that means
    that the zone number likely changed too).
    Furthermore, if there is an error in the script, then an error message will pop up 
    and prevent users from creating/saving work orders. 
    We don't want that! (error messages would render the system unusable until fixed)
    So, I figure, I'll set the field value to null if there is an error.
    """
    mbo.setValue(field_to_update, "", MboConstants.NOACCESSCHECK) #Should I
    # set the field value to "" or to None? 
    service.log("An exception occurred")
    
#What the script does:
#     1. Takes the X&Y coordinates of a work order in Maximo
#     2. Generates a URL from the coordinates
#     3. Executes the URL via a separate script/library (LIB_HTTPCLIENT)
#     4. Performs a spatial query in an ESRI REST feature service (a separate GIS system)
#     5. JSON text is returned to Maximo with the attributes of the zone that the work 
#        order intersected
#     6. The zone number is parsed from the JSON text via a separate script/library 
#        (LIB_PARSE_JSON)
#     7. Inserts the zone number into the work order record
#
#Notes about libraries:
#     - Unfortunately, I'm unable to add external Python libraries (like urllib or 
#       urlparse) to my Maximo/Jython implementation.
#     - Furthermore, some libraries (exampple: JSON) that are normally included in Python
#       and Jython have been excluded from my Maximo implementation. I don't have control
#       over this.
#     - Instead, if there is functionality missing from my Jython implementation, I need 
#       to use Java classes to fill in the gap: 
#       https://www.ibm.com/support/knowledgecenter/ja/SSEUEX_2.0.2/com.ibm.javaeuc.doc/com/ibm/json/java/package-summary.html
#
#Notes about creating the URL:
#     - Previous code reviewers have strongly suggested "...splitting the URL creation 
#       into different variables and adding a looping function that adds query parameters 
#       to the URL."
#     - I've tried doing this, but I found that it made the url parts harder for me to 
#       manage, not easier. Maybe I'm nuts, but I've tried it, and figured it was overly 
#       complicated, so I think we can skip this idea for now. Thanks all the same.

from psdi.mbo import MboConstants
from java.util import HashMap

field_to_update = "ZONE"

def get_coords():
    """
    Get the y and x coordinates(UTM projection) from the WOSERVICEADDRESS table
    via the SERVICEADDRESS system relationship.
    The datatype of the LatitdeY and LongitudeX fields is decimal.
    """
    laty  = mbo.getDouble("SERVICEADDRESS.LatitudeY")
    longx = mbo.getDouble("SERVICEADDRESS.LongitudeX")

    return laty, longx


def is_latlong_valid(laty, longx):
    #Verify if the numbers are legitimate UTM coordinates
    return (4000000 <= laty <= 5000000 and
            600000 <= longx <= 700000)


def make_url(laty, longx):
    """
    Assemble the URL (including the longx and the laty).
    Note: The coordinates are flipped in the url.
    Consider replacing the field wildcard(*) with the specific field name (zone)
    """
    url="http://vsgisapprod:6080/arcgis/rest/services/Operational_Services/Lagan_ServiceZones/MapServer/5/query?geometry={0}%2C{1}&geometryType=esriGeometryPoint&spatialRel=esriSpatialRelIntersects&outFields=*&returnGeometry=false&f=pjson".format(str(longx),str(laty))

    return url


def fetch_zone(url):
    # Get the JSON text from the feature service (the JSON text contains the zone value).
    ctx = HashMap()
    ctx.put("url", url)
    service.invokeScript("LIB_HTTPCLIENT", ctx)
    json_text = str(ctx.get("response"))

    # Parse the zone value from the JSON text
    ctx = HashMap()
    ctx.put("json_text", json_text)
    service.invokeScript("LIB_PARSE_JSON", ctx)
    parsed_val = str(ctx.get("parsed_val"))

    return parsed_val


#Is this sort of generic error checking acceptable (try/except)?
try:
    laty, longx = get_coords()
    if not is_latlong_valid(laty, longx):
        service.log('Invalid coordinates')
    else:
        url = make_url(laty, longx)
        zone = fetch_zone(url)
    
        #Insert the zone value into the zone field in the work order
        mbo.setValue(field_to_update, zone, MboConstants.NOACCESSCHECK)
except:
    """
    If the script fails, then set the field vaule to null.
    Reason: If the work order coordinates have changed (thereby triggering this script), 
    then setting the zone's field value to null is better than leaving it as the wrong 
    zone number (if the coordinates of the work order changed, that means
    that the zone number likely changed too).
    Furthermore, if there is an error in the script, then an error message will pop up 
    and prevent users from creating/saving work orders. 
    We don't want that! (error messages would render the system unusable until fixed)
    So, I figure, I'll set the field value to null if there is an error.
    """
    mbo.setValue(field_to_update, "", MboConstants.NOACCESSCHECK) #Should I
    # set the field value to "" or to None? 
    service.log("An exception occurred")
    
#What the script does:
#     1. Takes the X&Y coordinates of a work order in Maximo
#     2. Generates a URL from the coordinates
#     3. Executes the URL via a separate script/library (LIB_HTTPCLIENT)
#     4. Performs a spatial query in an ESRI REST feature service (a separate GIS system)
#     5. JSON text is returned to Maximo with the attributes of the zone that the work 
#        order intersected
#     6. The zone number is parsed from the JSON text via a separate script/library 
#        (LIB_PARSE_JSON)
#     7. Inserts the zone number into the work order record
#
#Notes about libraries:
#     - Unfortunately, I'm unable to add external Python libraries (like urllib or 
#       urlparse) to my Maximo/Jython implementation.
#     - Furthermore, some libraries (exampple: JSON) that are normally included in Python
#       and Jython have been excluded from my Maximo implementation. I don't have control
#       over this.
#     - Instead, if there is functionality missing from my Jython implementation, I need 
#       to use Java classes to fill in the gap: 
#       https://www.ibm.com/support/knowledgecenter/ja/SSEUEX_2.0.2/com.ibm.javaeuc.doc/com/ibm/json/java/package-summary.html
#
#Notes about creating the URL:
#     - Previous code reviewers have strongly suggested "...splitting the URL creation 
#       into different variables and adding a looping function that adds query parameters 
#       to the URL."
#     - I've tried doing this, but I found that it made the url parts harder for me to 
#       manage, not easier. Maybe I'm nuts, but I've tried it, and figured it was overly 
#       complicated, so I think we can skip this idea for now. Thanks all the same.

from psdi.mbo import MboConstants
from java.util import HashMap

field_to_update = "ZONE"

def get_coords():
    """
    Get the y and x coordinates(UTM projection) from the WOSERVICEADDRESS table
    via the SERVICEADDRESS system relationship.
    The datatype of the LatitdeY and LongitudeX fields is decimal.
    """
    laty  = mbo.getDouble("SERVICEADDRESS.LatitudeY")
    longx = mbo.getDouble("SERVICEADDRESS.LongitudeX")

    return laty, longx


def is_latlong_valid(laty, longx):
    #Verify if the numbers are legitimate UTM coordinates
    return (4000000 <= laty <= 5000000 and
            600000 <= longx <= 700000)


def make_url(laty, longx):
    """
    Assemble the URL (including the longx and the laty).
    Note: The coordinates are flipped in the url.
    Consider replacing the field wildcard(*) with the specific field name (zone)
    """
    url="http://something.com/arcgis/rest/services/Something/Zones/MapServer/15/query?geometry={0}%2C{1}&geometryType=esriGeometryPoint&spatialRel=esriSpatialRelIntersects&outFields=*&returnGeometry=false&f=pjson".format(str(longx),str(laty))

    return url


def fetch_zone(url):
    # Get the JSON text from the feature service (the JSON text contains the zone value).
    ctx = HashMap()
    ctx.put("url", url)
    service.invokeScript("LIB_HTTPCLIENT", ctx)
    json_text = str(ctx.get("response"))

    # Parse the zone value from the JSON text
    ctx = HashMap()
    ctx.put("json_text", json_text)
    service.invokeScript("LIB_PARSE_JSON", ctx)
    parsed_val = str(ctx.get("parsed_val"))

    return parsed_val


#Is this sort of generic error checking acceptable (try/except)?
try:
    laty, longx = get_coords()
    if not is_latlong_valid(laty, longx):
        service.log('Invalid coordinates')
    else:
        url = make_url(laty, longx)
        zone = fetch_zone(url)
    
        #Insert the zone value into the zone field in the work order
        mbo.setValue(field_to_update, zone, MboConstants.NOACCESSCHECK)
except:
    """
    If the script fails, then set the field vaule to null.
    Reason: If the work order coordinates have changed (thereby triggering this script), 
    then setting the zone's field value to null is better than leaving it as the wrong 
    zone number (if the coordinates of the work order changed, that means
    that the zone number likely changed too).
    Furthermore, if there is an error in the script, then an error message will pop up 
    and prevent users from creating/saving work orders. 
    We don't want that! (error messages would render the system unusable until fixed)
    So, I figure, I'll set the field value to null if there is an error.
    """
    mbo.setValue(field_to_update, "", MboConstants.NOACCESSCHECK) #Should I
    # set the field value to "" or to None? 
    service.log("An exception occurred")
    
deleted 3 characters in body
Source Link
User1974
  • 198
  • 1
  • 19
Loading
deleted 3 characters in body
Source Link
User1974
  • 198
  • 1
  • 19
Loading
deleted 1 character in body; deleted 66 characters in body; edited body
Source Link
User1974
  • 198
  • 1
  • 19
Loading
added 13 characters in body
Source Link
User1974
  • 198
  • 1
  • 19
Loading
Source Link
User1974
  • 198
  • 1
  • 19
Loading