# Run periodically executeAndDistribute() report function

**URL:** <https://forum.inductiveautomation.com/t/run-periodically-executeanddistribute-report-function/60783>\
**Category:** Ignition\
**Tags:** perspective, ignition81\
**Created:** [June 15, 2022, 9:06am UTC](https://forum.inductiveautomation.com/t/run-periodically-executeanddistribute-report-function/60783 "2022-06-15T09:06:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carla\_Ratto](https://avatars.discourse-cdn.com/v4/letter/c/9de053/32.png) [@Carla\_Ratto](https://forum.inductiveautomation.com/u/Carla_Ratto)\
**Post date:** [June 15, 2022, 9:06am UTC](https://forum.inductiveautomation.com/t/run-periodically-executeanddistribute-report-function/60783/1 "2022-06-15T09:06:38Z")

</div>

Hi everyone,

To present a little bit my project architecture to help you visualize, I have a general UDT which possess several parameters (client name, database name, …), and I create an instance of this UDT for each of my clients, specifying the value of the parameters.  
I also created “template” views, and with the dropConfig property I create an instance of these templates for each clients. One on this template uses the ReportViewer component, and I pass the different parameters to this component so that the report could be generated for each client.

Now, I want to send periodically a report to my different clients. At first, I tried to use the Schedule Tab of the report module, but it doesn’t seems to fit with my architecture because I need to specify the value of the parameters, but as I said previously I have to pass them to the ReportViewer component so that the report can be generated for each client. Then, I’m trying to use the executeAndDistribute function, which allows me to pass the parameters in the arguments of the function. To test this function, I created an event script when clicking on a button, containing this code, and it works fine :

```auto
system.report.executeAndDistribute(path="Filtration_report", project="Filtration", action= "email",
	  parameters={"StartDate": system.date.addDays(system.date.now(), -7), "udtName":self.view.params.udtElements.Parameters.UDTName, "database":self.view.params.udtElements.Parameters.Database,"H2O2displayFilter2":self.getSibling("ReportViewer").props.params.H2O2displayFilter2,"CLO2displayFilter2":self.getSibling("ReportViewer").props.params.CLO2displayFilter2},
	  actionSettings = {"to":["myaddress@mail.com"], "smtpServerName":"My_SMTP", "from":"filtration@mail.com", "subject":"Filtration Report"})

```

But I don’t really know how to launch this code periodically (let’s say on sunday evening) and not when clicking on a button. I though about using a loop which verify if the date correspond to sunday 23h59 but I’m afraid that it generates too many useless calculations. Is there another way to do this ?

Thanks,

Carla

---

<div class="post-metadata">

**Author:** ![jonathan.taylor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jonathan.taylor/32/4048_2.png) [@jonathan.taylor](https://forum.inductiveautomation.com/u/jonathan.taylor)\
**Post date:** [June 15, 2022, 10:21am UTC](https://forum.inductiveautomation.com/t/run-periodically-executeanddistribute-report-function/60783/2 "2022-06-15T10:21:55Z")

</div>

You could do this as a gateway timer script and check the time matches before you run your report; alternatively and probably more effciently, you could create expression tags that go true when the time and date matches when you want to send the report and have it trigger a gateway tag change script

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 15, 2022, 11:58am UTC](https://forum.inductiveautomation.com/t/run-periodically-executeanddistribute-report-function/60783/3 "2022-06-15T11:58:24Z")

</div>

Use a gateway scheduled script  
[Gateway Event Scripts - Ignition User Manual 8.1 - Ignition Documentation (inductiveautomation.com)](https://docs.inductiveautomation.com/display/DOC81/Gateway+Event+Scripts)

---

<div class="post-metadata">

**Author:** ![Carla\_Ratto](https://avatars.discourse-cdn.com/v4/letter/c/9de053/32.png) [@Carla\_Ratto](https://forum.inductiveautomation.com/u/Carla_Ratto)\
**Post date:** [June 16, 2022, 7:56am UTC](https://forum.inductiveautomation.com/t/run-periodically-executeanddistribute-report-function/60783/5 "2022-06-16T07:56:18Z")

</div>

It is perfectly what I need.

To explain a little bit what I did, I created a Gateway Scheduled Script where I had firstly to find the right UDT instance (which contains the keyword “Filtration” in its name), and once I have this UDT, I retrieve the UDT parameters I need and pass them to the function.

Here is the code

```auto
tags = system.tag.browse("[default]")
	
	for tag in tags : 
		if "Filtration" in tag['name'] : 
			Filtration_tag = tag 
		
	udtName = system.tag.readBlocking(["[default]/"+Filtration_tag['name']+"/_Parameters.UDTName"])[0].value
	database = system.tag.readBlocking(["[default]/"+Filtration_tag['name']+"/_Parameters.Database"])[0].value
	
	system.report.executeAndDistribute(path="Filtration_report", project="Moutounade", action= "email",
	  parameters={"StartDate":system.date.addDays(system.date.now(), -7), "udtName":udtName, "database":database},
	  actionSettings = {"to":["myaddress@mail.com"], "smtpServerName":"My_SMTP", "from":"filtration@mail.com", "subject":"Filtration Report"})

```

Thanks for your help !

---

<div class="post-metadata">

**Author:** ![Carla\_Ratto](https://avatars.discourse-cdn.com/v4/letter/c/9de053/32.png) [@Carla\_Ratto](https://forum.inductiveautomation.com/u/Carla_Ratto)\
**Post date:** [June 16, 2022, 9:56am UTC](https://forum.inductiveautomation.com/t/run-periodically-executeanddistribute-report-function/60783/6 "2022-06-16T09:56:55Z")

</div>

Sorry, I have another question. On the documentation it is written that it is optional to specify the project name. If I don’t specify it and that this script is present in several projects, is there going to be a conflict ?

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 16, 2022, 12:15pm UTC](https://forum.inductiveautomation.com/t/run-periodically-executeanddistribute-report-function/60783/7 "2022-06-16T12:15:24Z")

</div>

> [@Carla\_Ratto](#):
>
> ```auto
> udtName = system.tag.readBlocking(["[default]/"+Filtration_tag['name']+"/_Parameters.UDTName"])[0].value
> database = system.tag.readBlocking(["[default]/"+Filtration_tag['name']+"/_Parameters.Database"])[0].value
> 	
> 
> ```

Wrap this up into a single read for better performance. It may not matter much here, but it is a good habit to get into.

```auto
udtPath = "[default]/%s/_Parameters.UDTName" % Filtration_tag['name']
dbPath = "[default]/%s/_Parameters.Database" % Filtration_tag['name']

tags = system.tag.readBlocking([udtPath ,dbPath])
udtName = tags[0].value
database = tags[1].value

```

> [@Carla\_Ratto](#):
>
> optional to specify the project name

Specifying the project name for `system.report.executeAndDistribute` is only optional when you call it in the client scope. Since you are calling the function in a _Gateway_ Scheduled Script, you are in the gateway scope, so yes, project is required.

> [@Carla\_Ratto](#):
>
> this script is present in several projects

Not sure what you mean here. Why would you need _this_ script be in multiple projects?

---

<div class="post-metadata">

**Author:** ![Carla\_Ratto](https://avatars.discourse-cdn.com/v4/letter/c/9de053/32.png) [@Carla\_Ratto](https://forum.inductiveautomation.com/u/Carla_Ratto)\
**Post date:** [June 16, 2022, 12:23pm UTC](https://forum.inductiveautomation.com/t/run-periodically-executeanddistribute-report-function/60783/8 "2022-06-16T12:23:10Z")

</div>

> [@dkhayes117](#):
>
> Wrap this up into a single read

Thanks, I was just trying to figure out how to do that

> [@dkhayes117](#):
>
> you are in the gateway scope, so yes, project is required.

okay, I see, I didn’t quite understand.

> [@dkhayes117](#):
>
> Why would you need _this_ script be in multiple projects?

Hmm... For the moment it is not done, but in the long term we thought perhaps to create a project per client, so that each one has its own page, and therefore it would imply that this function is used in different projects.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 16, 2022, 12:26pm UTC](https://forum.inductiveautomation.com/t/run-periodically-executeanddistribute-report-function/60783/9 "2022-06-16T12:26:49Z")

</div>

> [@Carla\_Ratto](#):
>
> this function is used in different projects

Are you saying this _exact_ script would be in each project? Or do you mean the same script with a different UDT tag, different functionality, or something of the like?

---

<div class="post-metadata">

**Author:** ![Carla\_Ratto](https://avatars.discourse-cdn.com/v4/letter/c/9de053/32.png) [@Carla\_Ratto](https://forum.inductiveautomation.com/u/Carla_Ratto)\
**Post date:** [June 16, 2022, 2:23pm UTC](https://forum.inductiveautomation.com/t/run-periodically-executeanddistribute-report-function/60783/10 "2022-06-16T14:23:01Z")

</div>

Maybe I missed something, but in the Page Configurator, we can’t put parameters in the views paths, right ?  
If I’m right, it means that we will have to create several projects, one for each of our clients, so that every client could have his own page. And in this case, as we will have several projects, this script will be present several times too. Then, the code itself will remain the same, but with different parameters (database, udtName etc won’t have the same value for each client). But as the parameters (and project name) won’t be the same, I don’t think there will be a problem then.
