Setting a column value by using a script variable as the column name

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-17-2014 09:43 PM
I am attempting to use a script to set the value of a column using setValue(myvariable, value)
So current is my table A, I query table B from which I pull column names for setting values in table C.
Table A has a common field with B and C.
Essentially I am trying to update table C with values from table B when table A is updated (if that makes sense).
Here is an simple example of the code I am having trouble with. It seems I cannot use a variable as the column name in my gliderecord setValue function.
The red text below is where I am having trouble setting the column to use in the second gliderecord.
var grb = new GlideRecord('table_b');
grb.query();
while(grb.next()){
var column = grb.name;
var value = grb.value;
var grc = new GlideRecord('table_c');
grc.get(field, value);
grc.setValue(column, value);
grc.update();
I have verified that the value of column is a column name in table c.
Anyone have a clue as to how I might perform this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2014 03:24 AM
Looks like you are using a variable field on line 7 that you have not defined.
When using grc.get() it is best practice to use it within an if statement to verify the record was found before proceeding, like below:
var grb = new GlideRecord('table_b');
grb.query();
var column, value, field;
while(grb.next()) {
column = grb.name;
value = grb.value;
field = "sys_id";
var grc = new GlideRecord('table_c');
if (grc.get(field, value)) {
grc.setValue(column, value);
grc.update();
}
}
Although note your code still doesnt really make sense as you are looping through every record in table_b and using that to find a corresponding record in table_c but then you are only updating the same field, so the likely result will be no change. Also you are not using table_a anywhere in your code.
From your description you may want to take a look at GlideRecord - ServiceNow Wiki as it sounds like you want to loop each column in a GlideRecord? getFields() will help you do that.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2014 06:30 AM
The code I posted was only provided to describe my issue with the setValue function that I highlighted in red.
It is not anywhere near my actual code, but I wanted to give an idea on what I am trying to do. I should have made that more clear.
In a nutshell, I am wanting to use a variable inside the setValue function as the column name.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2014 08:35 AM
I have done something like what you are doing. How about you post your code that way maybe we can see if you have a syntax or logic error. One thing I always do when I do this kind of thing is add .toString() to the end to make sure I have the string value and not an object of some type.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2014 11:31 AM
Thanks Drew,
The ".toString()" was the key...
So, to use a variable as a parameter inside the gr.setValue function, you must use something like this:
var z = 5;
var x = y;
x = x.toString();
gr.setValue(x, z);
In my code I was not setting my variable to string.