# Variable in a script not working

**URL:** <https://forum.inductiveautomation.com/t/variable-in-a-script-not-working/31962>\
**Category:** Ignition\
**Created:** [February 5, 2020, 6:27pm UTC](https://forum.inductiveautomation.com/t/variable-in-a-script-not-working/31962 "2020-02-05T18:27:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lkruper](https://avatars.discourse-cdn.com/v4/letter/l/a88e4f/32.png) [@lkruper](https://forum.inductiveautomation.com/u/lkruper)\
**Post date:** [February 5, 2020, 6:27pm UTC](https://forum.inductiveautomation.com/t/variable-in-a-script-not-working/31962/1 "2020-02-05T18:27:37Z")

</div>

When I hardcode “.text” ( line 10 that is commented out) when populating the text for a component it works, but using a variable does not. I know I can work around this by testing for the type from my power table, but is there a way to make this assignment without doing this?

```auto
def loopPowerTable():
	pyRowData = system.dataset.toPyDataSet(event.source.parent.getComponent('formOnePopulated2').data)
	for row in pyRowData:
		sqlField = row[1]
		dataType = row[2]
		componentName = row[3] + " " + row[1] 
		assign = sqlField+" = event.source.parent.getComponent('"+componentName+"')."+dataType

		try:
		  sqlField = event.source.parent.getComponent(componentName).dataType
		  #sqlField = event.source.parent.getComponent(componentName).text
		  print "OK"+assign
		except AttributeError:
		  print "AttributeError:"+assign
```

---

<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:** [February 5, 2020, 7:07pm UTC](https://forum.inductiveautomation.com/t/variable-in-a-script-not-working/31962/2 "2020-02-05T19:07:11Z")

</div>

Python has an exec statement and an eval() function that can do these sorts of things, but are rather prone to programmer error. Consider the use of getattr() and a target dictionary.

```auto
someDictionary[sqlField] = getattr(event.source.parent.getComponent(componentName), dataType)

```
