# Message handler isn't opening/closing popups

**URL:** <https://forum.inductiveautomation.com/t/message-handler-isnt-opening-closing-popups/103536>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [March 11, 2025, 7:00pm UTC](https://forum.inductiveautomation.com/t/message-handler-isnt-opening-closing-popups/103536 "2025-03-11T19:00:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noah\_Gilgo](https://avatars.discourse-cdn.com/v4/letter/n/c89c15/32.png) [@Noah\_Gilgo](https://forum.inductiveautomation.com/u/Noah_Gilgo)\
**Post date:** [March 11, 2025, 7:00pm UTC](https://forum.inductiveautomation.com/t/message-handler-isnt-opening-closing-popups/103536/1 "2025-03-11T19:00:12Z")

</div>

I have a popup that I want to close after clicking the import button and successfully importing records.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/d/7de7c4a762990a7e81f5f5ad34d69baeee82a2a4.png)

The popup is intended to work as follows:

```python
def runAction(self, event):
	logger = system.util.getLogger('state_export')
	
	try:
		params = { 'Type': 'states' }
		system.perspective.openPopup("ImportFile", "GlobalComponents/Modals/ImportFile", params)

	except Exception as e:
		gpa.frontEnd.localUI.throwError(str(e))

```

1. When the Import button in the window is clicked, the popup opens, receiving the 'ImportFile' ID and the 'states' parameter.

```python
def runAction(self, event):
	logger = system.util.getLogger('importFile')
	system.perspective.print('testing import file')
	
	try:
		csvFile = self.parent.parent.getChild("FileUpload").custom.configCSV
		csvDS = self.parent.parent.getChild("FileUpload").custom.csvDict
		eqPath = self.view.params.EqPath
		payload = { 'csvString': csvFile, 'csvDS': csvDS, 'EqPath': eqPath }
		
		type_mapping = {
		    'states': 'importStates',
		    'modes': 'importModes',
		    'scrapCategory': 'importScrapCategory',
		    'scrapReasons': 'importScrapReasons',
		    'materials': 'importMaterials',
		    'sites': 'importSites',
		    'areas': 'importAreas',
		    'lines': 'importLines',
		    'cells': 'importCells',
		    'attributes': 'importAttributes',
		    'productcodes': 'importProductCodes'
		}
		
		messageType = str(type_mapping[str(self.view.params.Type)])

		system.perspective.print(messageType)
		if messageType:
			system.perspective.sendMessage(messageType, payload)
		self.parent.parent.getChild("FileUpload").custom.configCSV = ''
		system.dataset.clearDataset(self.parent.parent.getChild("FileUpload").custom.csvDict)
	except Exception as e:
		logger.warn('There was an error: ' + str(e))

```

1. When the user clicks the 'Import' button in the popup, the onClick event maps type\_mapping['states'] to the 'importStates' message handler name and uses it as a parameter in system.perspective.sendMessage().

```python
def onMessageReceived(self, payload):
	logger = system.util.getLogger("insertStates_message")
	
	try:
		system.perspective.print('test')
		csvString = payload.get("csvString")
		csvDict = payload.get("csvDS")
		pyData = system.dataset.toPyDataSet(csvDict)
		existingData = system.dataset.toPyDataSet(self.view.custom.Data)
		
		for row in pyData:
			system.perspective.print(row["AltCode"]) 
			if any(r["ReasonCode"] == row["ReasonCode"] and r["LineID"] == row["LineID"] for r in existingData):
				# If the combination already exists, log and continue to the next row
				logger.warn("Combination of ReasonCode {} and LineID {} already exists.".format(row["ReasonCode"], row["LineID"]))
				continue
				
			
			# If the combination doesn't exist, insert the new record
			params = {
				"AltCode": row["AltCode"],
				"OperatorSelectable": row["OperatorSelectable"],
				"SubReasonOf": row["SubReasonOf"],
				"StateID": 0,
				"LineID": self.view.params.ID,
				"Description": row["Description"],
				"DowntimeCategoryCode": row["DowntimeCategoryCode"],
				"DisplayColor": row["DisplayColor"],
				"PlannedDowntime": row["PlannedDowntime"],
				"ReasonCode": row["ReasonCode"],
				"RecordDowntime": row["RecordDowntime"],
				"IconPath": row["IconPath"],
				"StateTypeID": row["StateTypeID"]
			}
			system.perspective.print(params)
			gpa.frontEnd.localUI.throwSuccess("adding the line state")
			res = mes.config.sp.addEditLineState(params)
			gpa.frontEnd.localUI.throwSuccess("res = " + str(res))
			if str(res) is not "Success": 
				raise ValueError('Sproc call failed for row {}. Result: {}'.format(row, str(res)))
		
		system.perspective.sendMessage('refreshTree')
		system.perspective.sendMessage('refreshStatesTable')
		system.perspective.closePopup("ImportFile")
	
	except Exception as e:
		errorParams = {
			"ModalName": "ErrorModal",
			"ErrorMessage": str(e),
		}
	    
		system.perspective.openPopup("ErrorModal", "GlobalComponents/Modals/ErrorModal", errorParams)
		logger.warn('There was an error: ' + str(e))

```

1. The `importStates` message handler executes a sproc call and closes the ImportFile popup.

The sproc seems to be executed, and the other sendMessage() commands seem to be executed, but the popup is not closing from the `system.perspective.closePopup("ImportFile")` call. I'm not sure what could be causing this, but I'm certain the message handler is executing because the sproc is being executed, and the sproc is only referenced inside of this message handler and nowhere else.

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [March 11, 2025, 7:02pm UTC](https://forum.inductiveautomation.com/t/message-handler-isnt-opening-closing-popups/103536/2 "2025-03-11T19:02:54Z")

</div>

Post code, not pictures of code, it helps to speed up responses as people can just copy/paste instead of trying to read a picture and type. Please see [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831).

You can edit your post by clicking the pencil icon at the bottom right.

---

<div class="post-metadata">

**Author:** ![Noah\_Gilgo](https://avatars.discourse-cdn.com/v4/letter/n/c89c15/32.png) [@Noah\_Gilgo](https://forum.inductiveautomation.com/u/Noah_Gilgo)\
**Post date:** [March 11, 2025, 7:07pm UTC](https://forum.inductiveautomation.com/t/message-handler-isnt-opening-closing-popups/103536/3 "2025-03-11T19:07:36Z")

</div>

My mistake, I just reposted it with the code.

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [March 11, 2025, 7:15pm UTC](https://forum.inductiveautomation.com/t/message-handler-isnt-opening-closing-popups/103536/4 "2025-03-11T19:15:44Z")

</div>

Nothing immediately jumps out to me in your code other than some minor unrelated things. Have you checked your logs for any errors when trying this workflow?

You could always slap `logger.info('close popup called')` right after the call to close the popup and see if you see that show up in your logs. That would tell you without a doubt if the line was called.

In your error handling, don't stringify the exception when passing it to the logger, as you truncate the stack trace. Instead, pass it as a second parameter to the call, like `logger.warn('There was an error', e)`. That will preserve the stack trace in the logs.

---

<div class="post-metadata">

**Author:** ![Noah\_Gilgo](https://avatars.discourse-cdn.com/v4/letter/n/c89c15/32.png) [@Noah\_Gilgo](https://forum.inductiveautomation.com/u/Noah_Gilgo)\
**Post date:** [March 11, 2025, 8:15pm UTC](https://forum.inductiveautomation.com/t/message-handler-isnt-opening-closing-popups/103536/5 "2025-03-11T20:15:27Z")

</div>

> [@ryan.white](#):
>
> logger.info('close popup called')

```python
def onMessageReceived(self, payload):
	logger = system.util.getLogger("insertStates_message")
	
	try:
		system.perspective.print('test')
		csvString = payload.get("csvString")
		csvDict = payload.get("csvDS")
		pyData = system.dataset.toPyDataSet(csvDict)
		existingData = system.dataset.toPyDataSet(self.view.custom.Data)
		
		logger.info('running the message handler')
		
		for row in pyData:
			system.perspective.print(row["AltCode"]) 
			if any(r["ReasonCode"] == row["ReasonCode"] and r["LineID"] == row["LineID"] for r in existingData):
				# If the combination already exists, log and continue to the next row
				logger.warn("Combination of ReasonCode {} and LineID {} already exists.".format(row["ReasonCode"], row["LineID"]))
				continue
				
			
			# If the combination doesn't exist, insert the new record
			params = {
				"AltCode": row["AltCode"],
				"OperatorSelectable": row["OperatorSelectable"],
				"SubReasonOf": row["SubReasonOf"],
				"StateID": 0,
				"LineID": self.view.params.ID,
				"Description": row["Description"],
				"DowntimeCategoryCode": row["DowntimeCategoryCode"],
				"DisplayColor": row["DisplayColor"],
				"PlannedDowntime": row["PlannedDowntime"],
				"ReasonCode": row["ReasonCode"],
				"RecordDowntime": row["RecordDowntime"],
				"IconPath": row["IconPath"],
				"StateTypeID": row["StateTypeID"]
			}
			system.perspective.print(params)
			logger.info('executing the sproc')
			res = mes.config.sp.addEditLineState(params)
			gpa.frontEnd.localUI.throwSuccess("res = " + str(res))
			if str(res) is not "Success": 
				raise ValueError('Sproc call failed for row {}. Result: {}'.format(row, str(res)))
		
		logger.info('calling messages')
		system.perspective.sendMessage('refreshTree')
		system.perspective.sendMessage('refreshStatesTable')
		system.perspective.closePopup("ImportFile")
	
	except Exception as e:
		errorParams = {
			"ModalName": "ErrorModal",
			"ErrorMessage": str(e),
		}
	    
		system.perspective.openPopup("ErrorModal", "GlobalComponents/Modals/ErrorModal", errorParams)
		logger.warn('There was an error ', e)

```

I added several logger calls to the code, but none of them are being written to the console. I double checked my database and verified that the sproc in the event is being executed, so I'm still perplexed on what's happening.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 11, 2025, 8:17pm UTC](https://forum.inductiveautomation.com/t/message-handler-isnt-opening-closing-popups/103536/6 "2025-03-11T20:17:49Z")

</div>

Loggers in Perspective do not write to any console. The run in the gateway, so they log to the gateway.

---

<div class="post-metadata">

**Author:** ![Noah\_Gilgo](https://avatars.discourse-cdn.com/v4/letter/n/c89c15/32.png) [@Noah\_Gilgo](https://forum.inductiveautomation.com/u/Noah_Gilgo)\
**Post date:** [March 11, 2025, 8:40pm UTC](https://forum.inductiveautomation.com/t/message-handler-isnt-opening-closing-popups/103536/8 "2025-03-11T20:40:50Z")

</div>

My mistake, I meant to say "Gateway" instead of "console."
