---
title: "Sample Python Scripts"
canonical: "https://help.agiloft.com/space/HELP/43718770/Sample+Python+Scripts"
format: markdown
---
<span style="color: #000000">The following scripts provide examples of using custom Python scripting capabilities in a typical Agiloft installation. The Agiloft distribution includes Python and will automatically use it to execute scripts with a .py extension. If you want to use your own version of python, you may do so by setting the KB global variable "Location of external Python directory".</span>

> ⚠️ You must call these functions in every Python script used in Agiloft in order for the script to terminate properly:
> ⚠️ 
> ⚠️ `set_data.exitAction(ALset.ACCEPT)`
> ⚠️ 
> ⚠️ `set_data.save()`

# Samples

Click a sample below to see the script text.

<details>
<summary>Sample Script 1 redirects the user after they submit a record.</summary>

```python
######################################################################
# Synopsis:
#
# Redirect the user after he/she submits the ticket.
#
# There is no need to specify the Python interpreter if the script is named *.py
# because the system recognizes .py scripts as being Python based and will
# use the Python that is included in the distribution.
#
# If you use this script with your own copy of Python, please note that
# it requires ElementTree module to be installed
#
######################################################################
import sys
from ALget import ALget
from ALset import ALset
def action(inputXMLFile, outputXMLFile):
    get_data = ALget(inputXMLFile)
    set_data = ALset(outputXMLFile)
    ######################################################################
    #
    # Only log the user out if he/she is not a member of the Staff
    # group. In other words, we want all members of staff to be able
    # to log tickets on behalf of users without being logged out.
    ######################################################################
    groups = get_data.globalVariable('my_groups')
    groupAllowedToClose = "Staff"
    if groupAllowedToClose not in groups.split(","):
        set_data.redirect("http://www.example.com")
        set_data.exitAction(ALset.ACCEPT_REDIRECT)
    else:
        set_data.exitAction(ALset.ACCEPT)
    set_data.save()
action(sys.argv[1], sys.argv[2]) 
```
</details>

<details>
<summary>Sample Script 2 sends new users a confirmation email with their login and password.</summary>

```python
#####################################################################
# Synopsis:
# Send new users a confirmation email with their login and password
#
# There is no need to specify the Python interpreter if the script is named *.py
# because the system recognizes .py scripts as being Python based and will
# use the Python that is included in the distribution.
#
# If you use this script with your own copy of Python, please note that
# it requires ElementTree module to be installed
#
######################################################################
import sys
from ALget import ALget
from ALset import ALset
import smtplib
from email.mime.text import MIMEText
######################################################################
# Only send email to members of the Customer group.
# For security reasons, it is unsafe to send password information in
# plain text to privileged groups such as staff or admin.
######################################################################
def action(inputXMLFile, outputXMLFile):
    get_data = ALget(inputXMLFile)
    set_data = ALset(outputXMLFile)
    groups = get_data.valueMultipleLF("f_group")
    me = "support@CompanyName.com"
    you = get_data.value("email")
    if groups and groups.containsValue("Customer") and you:
        body = ("Dear " + get_data.value("full_name") + ",\n"
                "Thank you for registering with our support system.\n\n"
                "You have chosen the following\n"
                "login: " + get_data.value("_login") + "\n"
                "password: " + get_data.value("password") + "\n"
                "Thank you,\n"
                "CompanyName Support staff\n")
        msg = MIMEText(body)
        msg['Subject'] = "Welcome to CompanyName Support"
        msg['From'] = me
        msg['To'] = get_data.value("email")
        s = smtplib.SMTP('localhost')
        s.sendmail(me, [you], msg.as_string())
        s.quit()
    set_data.exitAction(ALset.ACCEPT)
    set_data.save()
action(sys.argv[1], sys.argv[2]) 
```
</details>

<details>
<summary>Sample Script 3 checks if the user belongs to QA group and blocks non-QA users from closing the ticket.</summary>

```
############################################################################
# Synopsis:
# Check if the user belongs to QA group and block closure of the ticket if
#  he/she doesn't.
#
# There is no need to specify the Python interpreter if the script is named *.py
# because the system recognizes .py scripts as being Python based and will
# use the Python that is included in the distribution.
#
# If you use this script with your own copy of Python, please note that
# it requires ElementTree module to be installed
############################################################################
import sys
from ALget import ALget
from ALset import ALset
def action(inputXMLFile, outputXMLFile):
    get_data = ALget(inputXMLFile)
    set_data = ALset(outputXMLFile)
    groups = get_data.globalVariable('my_groups')
    groupAllowedToClose = "QA"
    if groupAllowedToClose not in groups.split(",") and get_date.value("wfstate") == "Closed" and get_date.value("wfstate", get_date.oldRecordsNamesList()[0]) != "Closed":
        # Set a field value. In this case, we set the status field
        # to its old value
        set_data.recordField("wfstate", get_data.value("wfstate", get_data.oldRecordsNamesList()[0]))
        print("Restore state")
    set_data.exitAction(ALset.ACCEPT)
    set_data.save()
action(sys.argv[1], sys.argv[2])
```
</details>

<details>
<summary>Sample Script 4 illustrates some common tasks you would perform in a typical script.</summary>

```python
######################################################################
# Synopsis:
# This script performs some common tasks typical of a script.
# You could run this script against the input file example:
# filename.py input.xml output.xml
#
# There is no need to specify the Python interpreter if the script is named *.py
# because the system recognizes .py scripts as being Python based and will
# use the Python that is included in the distribution.
#
# If you use this script with your own copy of Python, please note that
# it requires ElementTree module to be installed
#
######################################################################
import sys
# these are interface modules to interact with input and output xml files
from ALget import ALget
from ALset import ALset
def action(inputXMLFile, outputXMLFile):
    # Load the XML files into internal structures. This is a required step before interacting with input using API calls.
    get_data = ALget(inputXMLFile)
    set_data = ALset(outputXMLFile)
    # we will use this to accumulate messages to the user
    message = ""
    # Let's retrieve the user's name to provide a personalized message.
    # That would be the Full Name from the Contacts table entry for the user editing the ticket.
    full_name = set_data.globalVariable('my_full_name')
    # Let's check if user is a creator of the record and prohibit editing if not, with an appropriate message.
    # The same could be done with permissions, but access control through scripts can be more sophisticated.
    # For example, you could use get_data.globalVariable('my_start_work') and get_data.globalVariable('my_stop_work')
    # (if there are such fields in Contacts table) to allow modifications only within the user's working hours.
    print("Editor name:" + get_data.globalVariable('my_full_name'))
    print("Creator name:" + get_data.value('created_by'))
    if get_data.globalVariable('my_login') != get_data.value('login'):
        # We compare the login of the user who edits the record (via the global variable) with
        # the login of the record's creator. Note that this example would be for the Support Cases table,
		# in which the Creator Login variable is simply "login"). If they're not the same, we add a message to
		#  the message variable we defined earlier, to describe the reason for denial.  
        message += "Sorry, " + full_name + ", you are not the creator of this record, you cannot modify it."
		# Then we set the exit code to reject the changes.
        set_data.exitAction(ALset.BLOCK)
    else:
        # Now let's try to detect some changes in the record.
        # For example, let's make deleteable flag non-editable without blocking other changes.
		# The second parameter of value() is optional.  Passing this argument in will refer to the version just before
		# the user's edit.  If the current (edited) version doesn't equal that version, the user must've changed it.
        if get_data.value('deleteable') != get_data.value('deleteable', get_data.oldRecordsNamesList()[0]):
            # We will notify user that the change is not allowed.
            message += "Sorry, " + full_name + ", you cannot modify the deleteable flag, your change will be ignored."
            # Let's return the old value back.
            set_data.recordField('deleteable', get_data.value('deleteable', get_data.oldRecordsNamesList()[0]))
		# Now some more complex techniques - linked records access.
    	# Let's find out, for example, the name and phone number of the record owner
    	# if a Support Case is "open" ("owned by" is a Linked Field relationship with the contacts table)
    	if "Open" == get_data.value('wfstate'):
        	# Retreive phone and name from first (index=0) record linked to owned_by field.
        	owner_phone = get_data.linkedValue("end_user_name", 0, "direct_phone")
        	owner_name = get_data.linkedValue("end_user_name", 0, "full_name")
        	message += "You can contact " + owner_name + ", owner of this support case by phone at " + owner_phone + "."
    	# Now check if this case is assigned to a team as opposed to a user
		# NOTE: this example is outdated because the assigned_team field in support cases only links to the Teams table,
		# but the methods used here are interesting regardless.
    	linkedRecordNames = get_data.linkedRecordsNamesList("assigned_team")
    	if get_data.tableNameForRecord(linkedRecordNames[0]) == "teams":
    	    message += "This issue assigned to " + get_data.linkedValue("assigned_team", 0, "_name") + " team."
    	# Accept changes to ticket
    	set_data.exitAction(ALset.ACCEPT)
    # Store the full message we've been accumulating in the variable "message" using the message() method, so the user can read it all.
    set_data.message(message)
    # And don't forget to save the output file.
    set_data.save()
# Retrieve input and output XML files from sys.argv and pass them into our function!
action(sys.argv[1], sys.argv[2]) 				
```
</details>

<details>
<summary>Sample Script 5 verifies the return of various field types using the example from the Support case table.</summary>

```python
# al_get_set.py

######################################################################
# Agiloft's python interface allows custom scripts to get/set values in a record.
# This script is a test script to verify the return of various field types. Based
# on the table you are testing, update the global constants to reflect the actual
# field names. The current selection is from the Support case table.
######################################################################

OUT_FIELD = 'problem_description'
GLOBAL_VAR = 'hotlinkserverroot'
TEXT_FIELD = 'summary'
CHOICE_FIELD = 'wfstate'
FILE_FIELD = 'inbound_attachments'
LINKED_FIELD = 'linked_company'
LINKED_TABLE_FIELD = 'status'

import sys
from ALget import ALget
from ALset import ALset
import traceback

def action(inputXMLFile, outputXMLFile):
    get_data: ALget = ALget(inputXMLFile)
    set_data: ALset = ALset(outputXMLFile)

    out_list: list = []
    out_list.append("#al_get_set.py\n")
    out_list.append("Result of testing various field types:")

    try:
        global_var = get_data.globalVariable(GLOBAL_VAR)
        out_list.append(f"get_data.globalVariable({GLOBAL_VAR}) returns:\t{global_var}")

        text_field = get_data.value(TEXT_FIELD)
        out_list.append(f"get_data.value({TEXT_FIELD}) returns:\t{text_field}")

        choice_field = get_data.value(CHOICE_FIELD)
        out_list.append(f"get_data.value({CHOICE_FIELD}) returns:\t{choice_field}")

        linked_field = get_data.linkedValue(LINKED_FIELD, 0, LINKED_TABLE_FIELD)
        out_list.append(
            f"get_data.linkedValue({LINKED_FIELD}, 0, {LINKED_TABLE_FIELD}) returns:\t{linked_field}")

        attached_files = get_data.attachedFiles(FILE_FIELD)
        out_list.append(f"get_data.attachedFiles({FILE_FIELD}) returns:\t{attached_files}")

        name, path = attached_files[0].name, attached_files[0].path
        out_list.append(f"attached_files[0].name = {name}")
        out_list.append(f"attached_files[0].path = {path}")
        
    except Exception as e:
        out_list.append("\nException encountered:")
        out_list.append(traceback.format_exc())

    set_data.recordField(OUT_FIELD, '\n'.join(out_list))

    set_data.exitAction(ALset.ACCEPT)
    set_data.save()

action(sys.argv[1], sys.argv[2])				
```
</details>

#### Related articles

> Macro (contentbylabel)

<span style="color: #000000"> </span>