# Using message handler to run script

**URL:** <https://forum.inductiveautomation.com/t/using-message-handler-to-run-script/52818>\
**Category:** Ignition\
**Created:** [November 8, 2021, 1:35am UTC](https://forum.inductiveautomation.com/t/using-message-handler-to-run-script/52818 "2021-11-08T01:35:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael.black](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@michael.black](https://forum.inductiveautomation.com/u/michael.black)\
**Post date:** [November 8, 2021, 1:35am UTC](https://forum.inductiveautomation.com/t/using-message-handler-to-run-script/52818/1 "2021-11-08T01:35:19Z")

</div>

I have a frontend and backend gateway. Frontend contains perspective visuals and reporting modules. Backend contains the tag provider and DB connection. Frontend also has DB connection. Originally this was one gateway where all the scripting was on the one gateway. Now I have some tag event scripts which reside on the backend. I have a script based off of a tag event script which contains a call to save a report however it will not work because the frontend contains the report and the backend contains the tag calling the script. Whats the best way and example to complete this from the backend to the frontend? Can I use a message handler? If so can you give an example of what it would look like? I am very new to message handlers to do this type of thing.

Here is the existing tag event script being called from the backend tag:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/f/4faa75a39c4249eb2f63a99f7242b41a900b42c2.png)

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [November 8, 2021, 7:53am UTC](https://forum.inductiveautomation.com/t/using-message-handler-to-run-script/52818/2 "2021-11-08T07:53:43Z")

</div>

This might help you [Calling function on another gateway? - #3 by nminchin](https://forum.inductiveautomation.com/t/calling-function-on-another-gateway/51926/3)

---

<div class="post-metadata">

**Author:** ![michael.black](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@michael.black](https://forum.inductiveautomation.com/u/michael.black)\
**Post date:** [November 11, 2021, 10:06pm UTC](https://forum.inductiveautomation.com/t/using-message-handler-to-run-script/52818/3 "2021-11-11T22:06:42Z")

</div>

Thanks! Here is my attempt of the tag event script.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/9/99fb5de828942b7d8242f7cedb165ee5908a88dc.png)

Question I have is what to put for the hostName or remoteServers parameters. Would I be able to put the IP address of the gateway connection in one of these? Would I be able to put the gateway name of the frontend gateway which contains the messagehandler in one of these?

Having trouble determining which one to use and what to put for one of them. Thanks.

---

<div class="post-metadata">

**Author:** ![michael.black](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@michael.black](https://forum.inductiveautomation.com/u/michael.black)\
**Post date:** [November 11, 2021, 10:14pm UTC](https://forum.inductiveautomation.com/t/using-message-handler-to-run-script/52818/4 "2021-11-11T22:14:27Z")

</div>

If I am reading things correctly, if the frontend gateway which contains the message handler I want to call has the system name of ‘Ignition-BladeSide-Frontend’ within the Gateway Settings, I should be able to include the line:

remoteServers = ‘Ignition-BladeSide-Frontend’

Is this correct?

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [November 11, 2021, 10:18pm UTC](https://forum.inductiveautomation.com/t/using-message-handler-to-run-script/52818/5 "2021-11-11T22:18:26Z")

</div>

Yep, remoteServers should be the gateway system name, but it should be a list

---

<div class="post-metadata">

**Author:** ![michael.black](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@michael.black](https://forum.inductiveautomation.com/u/michael.black)\
**Post date:** [November 12, 2021, 4:34am UTC](https://forum.inductiveautomation.com/t/using-message-handler-to-run-script/52818/6 "2021-11-12T04:34:07Z")

</div>

Yea I did see it had to be a list. I am pretty sure I have everything correct but still not working.  
Backend tag event script is creating the call of the messagehandler on the frontend.  
Frontend project is called ‘PW\_BladeSide’, frontend gateway is ‘Ignition-BladeSide-Frontend’.  
When cycle\_complete bit goes high it builds the call and then sends out the message.  
Here is revised code for tag event script:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/0/d02fe2eeedf5fa54ddf32fb09226e1c7802614de.png)  
The gateway message handler on the frontend is:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/d/1d1185cc8c368cc2f5d18d7fe2865640709e4269.png)  
It is supposed to call ReportControl script in the frontend gateway scripting and execute code that creates a folder if one is not already created, save the report as a pdf and save it in that folder, and also print the report out. It is not even creating the folder.  
Here is the code within ReportControl. I know this part of the code works when I was able to do it without using a messagehandler:

```auto
def create_report_folders():
	"""Create report folders if they do not exist."""
	
	import os
	base_dir = 'C:\Reports'
	dirs = [base_dir]
	for furnace_number in ['548642', '548643', '548644', '548645', '548646', '548647']:
		dirs.append('{}\BT{}'.format(base_dir, furnace_number))
	for dir in dirs:
		if not os.path.isdir(dir):
			os.mkdir(dir)

def create_report(furnace_brass_tag_num, command='save'):
	"""Generate report for furnace.
	
	Params:
		furnace_brass_tag_num (str): Brass tag number of furnace
		command (str): One of 'save' or 'print'
	"""
	
	furnace_cycle_id = system.tag.read('[BladesideSR]BS/FurnaceBT{}/Recipe/Lots/furnace_cycle_id'.format(furnace_brass_tag_num)).value
	params = {'FurnaceCycleID': furnace_cycle_id}
	if command == 'save':
		create_report_folders()
		date = system.date.format(system.date.now(), 'yyyy-MM-dd HH-mm-ss')
		filename = 'BT{} {}.pdf'.format(furnace_brass_tag_num, date)		
		path = 'C:\Reports\BT{}'.format(furnace_brass_tag_num)		
		settings = {'path': path, 'fileName': filename, 'format': 'pdf'}
		system.report.executeAndDistribute(path='FurnaceCycle', parameters=params, action='save', actionSettings=settings)
	elif command == 'print':
		settings = {'primaryPrinterName': 'HP LaserJet M608 [847294]', 'backupPrinterName': 'none', 'copies': 1, 'printBothSides': True,
					'collate': True, 'useRaster': False, 'rasterDPI': 1200, 'useAutoLandscape': False, 'pageOrientation': 'portrait'}
		system.report.executeAndDistribute(path='FurnaceCycle', parameters=params, action='print', actionSettings=settings)

```

I am not getting to do anything yet so far…is there anything obvious I am doing wrong as to why its not working?

Also BrassTagNum is a string parameter within the UDT.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [November 12, 2021, 4:37am UTC](https://forum.inductiveautomation.com/t/using-message-handler-to-run-script/52818/7 "2021-11-12T04:37:49Z")

</div>

I would try putting some debugging messages into the message handler to see if it’s actually running at all. Add in some print statements or logger traces and check the gateway wrapper log or server logs.

I’m also not sure that `'{BrassTagNum}'` works. I thought in order to use that syntax you needed to use `f'{BrassTagNum}'`, but I don’t think that’s available in Jython2.7. Try replacing with the `'{}'.format(BrassTagNum)` method.  
Wait, why do you even need to indirectly reference it? Why not just  
`ReportControl.create_report(BrassTagNum, 'save')`?

---

<div class="post-metadata">

**Author:** ![michael.black](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@michael.black](https://forum.inductiveautomation.com/u/michael.black)\
**Post date:** [November 12, 2021, 4:41am UTC](https://forum.inductiveautomation.com/t/using-message-handler-to-run-script/52818/8 "2021-11-12T04:41:51Z")

</div>

That was my next step, to see if its actually getting through. I know how to do the logger util statement inside a script function. Could I use the same thing? If I used the ‘print’ statement where would I look to see what it prints?

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [November 12, 2021, 4:43am UTC](https://forum.inductiveautomation.com/t/using-message-handler-to-run-script/52818/9 "2021-11-12T04:43:31Z")

</div>

> [@michael.black](#):
>
> I know how to do the logger util statement inside a script function. Could I use the same thing?

Yep.

```auto
LOGGER = system.util.getLogger("Hello")

LOGGER.trace('I tawt I saw a puddy tat..')

```

> [@michael.black](#):
>
> If I used the ‘print’ statement where would I look to see what it prints?

prints in a gateway context print to the wrapper log

---

<div class="post-metadata">

**Author:** ![michael.black](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@michael.black](https://forum.inductiveautomation.com/u/michael.black)\
**Post date:** [November 12, 2021, 4:46am UTC](https://forum.inductiveautomation.com/t/using-message-handler-to-run-script/52818/10 "2021-11-12T04:46:35Z")

</div>

It all worked when everything was on one gateway with the following code for the tag event script on cycle\_complete tag

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/3/e319c1d7ca2a7cef3c536d8a206978d5c115398c.jpeg)  
I think the squiggly brackets mean you want to use the value of BrassTagNum and not the actual string. I thought I can just use this same code but complete lines 5 and 6 in the message handler on the frontend since the frontend contains the report I want to use and also where I want to save the file to.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [November 12, 2021, 4:49am UTC](https://forum.inductiveautomation.com/t/using-message-handler-to-run-script/52818/11 "2021-11-12T04:49:16Z")

</div>

Pretty sure that syntax is only supported in Python 3+ (and it needs the f notation at the start)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/0/506a236dbb397590df1c97645ada01858e2e3d32.png)
