Assistance Needed with Script Execution for Report Generation in Ignition

Good Afternoon,

I have a script that I want to run to complete the following task:

  1. Call a report from the reporting module.
  2. Save the report to a folder with the date and time in the file name.

The purpose of this is to prevent the report from overwriting itself each time it is called.

Here is the code and the result:

# Executes and distributes the report to save as a PDF
import system

# Read the current date and time from the [System]Gateway/CurrentDateTime tag
current_datetime = system.tag.read("[System]Gateway/CurrentDateTime").value

# Format the datetime as "dd-MM-yyyy hh.mm a"
formatted_datetime = system.date.format(current_datetime, "dd-MM-yyyy hh.mm a")

# Replace the space between time and AM/PM with an underscore
formatted_datetime = formatted_datetime.replace(' ', '_')

file_name = "Dryline Scrap Report_" + formatted_datetime + ".pdf"

settings = {"path": "C:\\Ignition Reports", "fileName": file_name, "format": "pdf"}
system.report.executeAndDistribute(path="Scrap Report", project="L12HBA_Scrap", action="save", actionSettings=settings)


Currently, it works great in the script console, but I want this to trigger when a button is pressed. I've been trying to set this up with an event trigger, but I get an error because it seems the script cannot call the system functions.

Does anyone have any ideas on how to resolve this?

Thanks,
Ryan

No need to import system.

Perspective scripts run on the gateway. So the reports will be saved locally on the gateway.

Are you trying to get the perspective session to download on the PC running the session?

1 Like

Can you post the exact error message here, with stack trace?

Yes correct, I want to store the report to this PC or any file location on the network

So if you are going to want to grab a report and download it to the PC, you will want to use a combination of system.report.executeReport | Ignition User Manual which will give you a PDF of the report, and system.perspective.download | Ignition User Manual which will prompt the person running the session to save that to the PC.

This would be all user-generated and can't be automatic.

If you want to create a report, and then save it to a known network file location, you are on the right track. You will want to use system.report.executeAndDistribute | Ignition User Manual and use the file action to save it to a known good location on your network or the gateway. This can be done w/o user interaction. But note... that the OS user that is running the Ignition Gateway, not the user logged into Perspective, must have permissions on the network resource to save the file. If you are using Windows, the Ignition service is started using the local service account that does not have network privileges, and the user must be changed to one that has the proper operating system rights.

1 Like

Awesome thanks mate.

Did get it sorted. I was a combination for indenting and removing the "import" call in the script.

This is what the result is:

# Read the current date and time from the [System]Gateway/CurrentDateTime tag
	current_datetime = system.tag.read("[System]Gateway/CurrentDateTime").value
	
	# Format the datetime as "dd-MM-yyyy_hh.mm a"
	formatted_datetime = system.date.format(current_datetime, "dd-MM-yyyy_hh.mm a")
	
	# Replace the space between time and AM/PM with an underscore
	formatted_datetime = formatted_datetime.replace(' ', '_').replace(':', '.')
	
	file_name = "Dryline Scrap Report_" + formatted_datetime + ".pdf"
	
	settings = {"path": "C:\\Ignition Reports", "fileName": file_name, "format": "pdf"}
	system.report.executeAndDistribute(path="Scrap Report", project="L12HBA_Scrap", action="save", actionSettings=settings)

Minor improvement: There's no need to read the current date time tag, you can just get the current system time, since Perspective scripts are already running on the gateway:
current_datetime = system.date.now()

Thanks Paul, you can tell I am still learning

Paul =)