Discussion:
How to query table data in X++ by using table and field variables?
(too old to reply)
Newbie
2010-05-04 18:56:01 UTC
Permalink
I have a method on a form which will validate the user input against the
table data. Normally I will validate by calling the Find method on the table
like CustTable::find(this.getvalue()).

The problem I am having is that I have a variable for table and a variable
for the field in the method. At each call to this method, the values of the
table and field variables can be different, i.e., one call it will be
InventTable, ItemId. Another call, it will be ProjTable, ProjId.

How do I query the table and validate the field input to see if it exists in
the table in X++ by using the variables? What is the syntax?

Thank you very much.
--
Ax Newbie
Vilmos Kintera
2010-05-05 01:16:01 UTC
Permalink
You can pass in the table and field names as string, and just replace the
hardcoded one.

static void WIK_CheckFieldExists(Args _args)
{
DictTable dictTable = new
DictTable(Global::tableName2Id("InventTable"));
;

if (dictTable.fieldName2Id("ItemId"))
{
info("Field exists");
}
else
{
info("Field is missing");
}
}

Regards,
Vilmos Kintera
http://daxrunbase.blogspot.com/
Newbie
2010-05-05 13:14:01 UTC
Permalink
Vilmos,

Thank you for your reply. But I need to query the table with the user input
to see if the field value exists in the table.

public void modified()
{
anytype table_name, field_name;
str userInput;
;
table_name = some method will get the value for it;
field_name = some other method will get the value for it;

userInput = this.getValue();
//what I need is this part
if((select table_name where table_name.field_name == userInput).recId ==0)
{
throw error("Input value is not value.";
}
}

Please advise.

Thanks.
--
Ax Newbie
Post by Vilmos Kintera
You can pass in the table and field names as string, and just replace the
hardcoded one.
static void WIK_CheckFieldExists(Args _args)
{
DictTable dictTable = new
DictTable(Global::tableName2Id("InventTable"));
;
if (dictTable.fieldName2Id("ItemId"))
{
info("Field exists");
}
else
{
info("Field is missing");
}
}
Regards,
Vilmos Kintera
http://daxrunbase.blogspot.com/
Martin Drab
2010-05-06 07:11:01 UTC
Permalink
Hi, I'm just working on something a bit similar (searching for the object
ID/names references). See my solution:

SysDictTable dictTable = new SysDictTable(tableName2Id(tableName));
Common common = dictTable.makeRecord();

select firstOnly RecId from common
where common.(dictTable.fieldName2Id(fieldName)) == value;
--
Best regards,
Martin Drab (Czech Blog: http://dax-cz.blogspot.com)
Post by Newbie
Vilmos,
Thank you for your reply. But I need to query the table with the user input
to see if the field value exists in the table.
public void modified()
{
anytype table_name, field_name;
str userInput;
;
table_name = some method will get the value for it;
field_name = some other method will get the value for it;
userInput = this.getValue();
//what I need is this part
if((select table_name where table_name.field_name == userInput).recId ==0)
{
throw error("Input value is not value.";
}
}
Please advise.
Thanks.
--
Ax Newbie
Post by Vilmos Kintera
You can pass in the table and field names as string, and just replace the
hardcoded one.
static void WIK_CheckFieldExists(Args _args)
{
DictTable dictTable = new
DictTable(Global::tableName2Id("InventTable"));
;
if (dictTable.fieldName2Id("ItemId"))
{
info("Field exists");
}
else
{
info("Field is missing");
}
}
Regards,
Vilmos Kintera
http://daxrunbase.blogspot.com/
Newbie
2010-05-06 14:06:01 UTC
Permalink
Martin,

Thank you very much. That is exactly what I was looking for. Thanks again!
Ax Newbie
Post by Martin Drab
Hi, I'm just working on something a bit similar (searching for the object
SysDictTable dictTable = new SysDictTable(tableName2Id(tableName));
Common common = dictTable.makeRecord();
select firstOnly RecId from common
where common.(dictTable.fieldName2Id(fieldName)) == value;
--
Best regards,
Martin Drab (Czech Blog: http://dax-cz.blogspot.com)
Post by Newbie
Vilmos,
Thank you for your reply. But I need to query the table with the user input
to see if the field value exists in the table.
public void modified()
{
anytype table_name, field_name;
str userInput;
;
table_name = some method will get the value for it;
field_name = some other method will get the value for it;
userInput = this.getValue();
//what I need is this part
if((select table_name where table_name.field_name == userInput).recId ==0)
{
throw error("Input value is not value.";
}
}
Please advise.
Thanks.
--
Ax Newbie
Post by Vilmos Kintera
You can pass in the table and field names as string, and just replace the
hardcoded one.
static void WIK_CheckFieldExists(Args _args)
{
DictTable dictTable = new
DictTable(Global::tableName2Id("InventTable"));
;
if (dictTable.fieldName2Id("ItemId"))
{
info("Field exists");
}
else
{
info("Field is missing");
}
}
Regards,
Vilmos Kintera
http://daxrunbase.blogspot.com/
d***@gmail.com
2013-10-23 07:45:09 UTC
Permalink
Post by Vilmos Kintera
You can pass in the table and field names as string, and just replace the
hardcoded one.
static void WIK_CheckFieldExists(Args _args)
{
DictTable dictTable = new
DictTable(Global::tableName2Id("InventTable"));
;
if (dictTable.fieldName2Id("ItemId"))
{
info("Field exists");
}
else
{
info("Field is missing");
}
}
Regards,
Vilmos Kintera
http://daxrunbase.blogspot.com/
Thanks !

Tro Diep
2012-07-03 06:59:20 UTC
Permalink
Post by Newbie
I have a method on a form which will validate the user input against the
table data. Normally I will validate by calling the Find method on the table
like CustTable::find(this.getvalue()).
The problem I am having is that I have a variable for table and a variable
for the field in the method. At each call to this method, the values of the
table and field variables can be different, i.e., one call it will be
InventTable, ItemId. Another call, it will be ProjTable, ProjId.
How do I query the table and validate the field input to see if it exists in
the table in X++ by using the variables? What is the syntax?
Thank you very much.
--
Ax Newbie
Hi AX Newbie,

I have similar requirements as yours do you mind tell me where you place the method on the form and how this method is called. I like to check customer order number in the sale order form when user input. if it is exist throw message the customer number has been used.

Thank you for your help

Tro.

Can you please let me know how you do
Loading...