# Error running function from system.util.invokeAsynchronous

**URL:** <https://forum.inductiveautomation.com/t/error-running-function-from-system-util-invokeasynchronous/26973>\
**Category:** Ignition\
**Created:** [July 29, 2019, 11:37pm UTC](https://forum.inductiveautomation.com/t/error-running-function-from-system-util-invokeasynchronous/26973 "2019-07-29T23:37:20Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![mriederer](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@mriederer](https://forum.inductiveautomation.com/u/mriederer)\
**Post date:** [July 30, 2019, 6:00pm UTC](https://forum.inductiveautomation.com/t/error-running-function-from-system-util-invokeasynchronous/26973/5 "2019-07-30T18:00:25Z")

</div>

The end goal of this is to provide a summary of process activity after some period (overnight, weekly, etc.) so it was sending out an email for each process change. Possibly the whole thing should be re-worked to send only one email with a summary of all changes...

I got rid of the invokeAsynch. I was thinking the executeAndDistribute might need it to send multiple emails in a loop... Not sure.

The _sendEmail_ function and new code (i.e. invokeAsynch) are within a _for_ loop as mentioned in the post. Iterating through a SQL table for tank numbers & info.

```auto
for n in range(len(pyTankDS)):
    tankNo = pyTankDS[n][0]
	tank = 'Tank ' + tankNo

	#read relevant properties from tag path
	volume = str(round(system.tag.read('[default]Tank Status History/'+tank+'/Volume').value,1))
	
    def sendEmail(tankNo=tankNo, tank = tank, volume = volume):	
    	import system
    	recipients=["jane@test.com","jon@xyz.com"] #
	    system.report.executeAndDistribute(path="xyz", parameters = {"tankNumber":tankNo}, project="xyz", action= "email", actionSettings = {"to":recipients, "smtpServerName":"Mail Server", "from":"jon@xyz.com", "subject":tank + 'test email', "body":tank + " test " + volume})
    system.util.invokeAsynchronous(sendEmail)

print 'post-email function'
system.db.runUpdateQuery('DELETE FROM tank_fills')

```

Changed to:

```auto
for n in range(len(pyTankDS)):
    tankNo = pyTankDS[n][0]
	tank = 'Tank ' + tankNo

	#read relevant properties from tag path
	volume = str(round(system.tag.read('[default]Tank Status History/'+tank+'/Volume').value,1))

    recipients=["jane@test.com","jon@xyz.com"] 
    system.report.executeAndDistribute(path="xyz", parameters = {"tankNumber":tankNo}, project="xyz", action= "email", actionSettings = {"to":recipients, "smtpServerName":"Mail Server", "from":"jon@xyz.com", "subject":tank + 'test email', "body":tank + " test " + volume})

print 'post-email function'
system.db.runUpdateQuery('DELETE FROM tank_fills')

```

More info on this thread:

> [@Waiting for certain time to send email with executeAndDistribute](http://forum.inductiveautomation.com/t/waiting-for-certain-time-to-send-email-with-executeanddistribute/26448/4):
>
> The emails seem to be sending now, but there is an INDENT EOF error occurring after the script runs. I’m not sure if this is a cause for concern or how to prevent the error. FWIW, this code is in a Gateway Event Timer Script contained within a for loop. def sendEmail(tankNo=tankNo, tank = tank): import system recipients=["jane@test.com","jon@xyz.com"] # system.report.executeAndDistribute(path="xyz", parameters = {"tankNumber":tankNo}, project="xyz", action= "email", actionSetti…

---

_[View the full topic](https://forum.inductiveautomation.com/t/error-running-function-from-system-util-invokeasynchronous/26973)._
