Message handler isn't opening/closing popups

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

The popup is intended to work as follows:

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.
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().
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.

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.

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

1 Like

My mistake, I just reposted it with the code.

1 Like

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.

2 Likes
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.

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

(post deleted by author)

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