Pregunta

Im working in AX 2012. I try to make code where i count all sales for each month and insert them into fields in a table. These fields are called Sales1, Sales2, Sales3 etc... representing the months in a year.

is there a way i can select these fields and insert into them in a while select like this example below?

while select myTable where myTable.date >= startDate && myTable.date <= endDate
{
     MyTable.("Sales" +MthOfYear(MyTable.Date)) += MyTable.SalesQty;
}

myTable.insert();

it's a stupid example, but it should show what i want to achieve... any ideas?

Regards Kent

¿Fue útil?

Solución

I think your main question is how to access a field by having its name in a string variable? If that's the case then please see the following two links:

How to convert field name to its ID

How to access a table field by ID

In short: First of all you have to convert your field name to the corresponding field ID by using fieldName2Id. After that, you can access this field by using the syntax myTable.(fieldId).

To put it all together for your case:

myTable.(fieldName2Id(myTable.TableId, strfmt("%1%2", "Sales", mthofyr(myTable.Date)))) += myTable.SalesQty

If you have any problems or questions don't hesitate to ask via a comment.

Otros consejos

While the fieldName2Id may save your day, it is an AX anti-pattern to have fields named Sales1, Sales2 ... Sales17. Why? Because AX supports arrays in tables (albeit only of a fixed size).

To use that, define a new extended data type SalesAmountMonth exending SalesAmount (or whatever). Label the it "January". Then in the node "Array Elements" add an new array element for "February", "Marts" up till "December". See How to Define an Extended Data Type as an Array.

Then add the extended data type to your table.

Your field access is then quite simple:

myTable.Sales[mthofyr(myTable.Date)] += myTable.SalesQty;

I prefer using the intvNo function for date indexing like this, as it has more flexibility:

myTable.Sales[intvNo(myTable.date, startDate, IntvScale::YearMonth) + 1] += myTable.SalesQty;

By changing the IntvScale enumeration you can group by month, week, quarter or whatever it supports.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top