Power Table - IsCellEditable script to get username from rowId

Hi everyone,

I have a power table linked to dB table from which I’m trying to get the username based on rowId and check if the logged in user matches the one from db and then let them edit any row that has their usernames.

I’m able to run this script from the script console and get the data I want but the table becomes un-editable when I put it in isCellEditable. Anything I’m doing wrong here?

	rowOwner = system.db.runScalarQuery("SELECT userName FROM XXXXXX WHERE id LIKE %d" % id)
	if colName == 'AAA' or colName == 'BBB':
		if 'Administrator' in system.security.getRoles():
			return True
		elif 'Lead' in system.security.getRoles():
			return True
		elif system.security.getUsername() == rowOwner:
			return True
		else: return False

Thank you

In your query, the variable ‘id’ does not have a value.

1 Like

You will need to get ‘id’ from elsewhere in the row.

Notes:

  • In my example I put the query lower so that it only runs on cells that require it. Consider putting the query in a custom property on the table with polling turned off to reduce the number of calls to the db.
  • added a list called allowedRoles, then used the any method to find a match. The number of iterations stays the same, but has the advantage of only calling getRoles() once. It also, IMO, makes it easier to Administer the allowed roles.
	if colName in ['AAA', 'BBB']:
		allowedRoles = ['Administrator', 'Lead']
		rowOwner = system.db.runScalarQuery("SELECT userName FROM XXXXXX WHERE id LIKE %d" % self.data.getValueAt(rowIndex, 'id'))
		# Check if any allowed role is in the users current roles
		if any(role in allowedRoles for role in system.security.getRoles()):
			return True
		elif system.security.getUsername() == rowOwner:
			return True
		else: return False
2 Likes

Thanks @JordanCClark and @mcgheeiv. Sorry, I missed the id part while copy pasting but my code used to stop running as soon as it got to elif system.security.getUsername() == rowOwner. Even the authorized roles were not able to edit.

Anyway, yours made it work :slightly_smiling_face:

1 Like