Quickly Update Data Through X++ Scripts
Posted by Author, Curator, Visually Impared Squirrel Literacy Advocate & Dynamics AX Technical Solution Professional (TSP) at Microsoft on
Normally I don’t promote to coding scripts within Dynamics AX because it is always perceived as something that a developer has to do. And for the power user, there are so many other ways to tinker with the system that getting your hands dirty with X++ is not necessary. One situation though that it’s sometimes just better to roll up your sleeves and write a little bit of X++ code is when it comes to updating masses of data. I’m not talking about updating 100 or so records because you can use Excel for that, but if you need to update 1,000’s or 10’s of thousands of records then Excel can be a little slow, and a script is so much faster.
Hopefully this worked example will demystify scripting a little and make you feel a little like a Whiz Kid.
How To Do It…
In this example, I want to update the Calculation Group on all of my released products – because I forgot to load it through DIEF.
- To create the script, just open up the AOT by pressing CTRL+D.
- And then press CTRL+SHIFT+P to open up the Projects explorer.
- Right-mouse-click on the Projects folder and select the Project option from the New sub-menu to create a new Project.
- This will create a new Project folder for you and if you click on it then you will be able to access the Properties window.
- Change the name on the Project so that it is a little more descriptive.
- Then right-mouse-click on the project and select the Open option to open up the project itself.
- When the Project is displayed, right-mouse-click on the header and select the New menu, and then the Job option.
- This will open up an X++ scripting pane for you to write your code within.
-
Now is the dirty part. Create a variable that points to the table you want to update – in this case it’s the InventTable by adding this line of code: InventTable item;
-
Then create a loop that will step through every record by typing this:
while select forUpdate item
{
}
-
Then add your code to see if there is a record in the table to update:
if (item)
{
}
-
Finally, add your code to update the record:
ttsBegin;
item.BOMCalcGroupId = “DEFAULT”;
item.update();
ttscommit;
-
The full code will look like this.
static
void Job11(Args _args)
{
InventTable item;
while select forUpdate item
{
if (item)
{
ttsBegin;
item.BOMCalcGroupId = “DEFAULT”;
item.update();
ttscommit;
}
}
}
- To run the job, just click on the green play button in the menu bar.
Now all of the records will be updated for you.
That wasn’t as hard as you probably thought was it.
To view the original post and also download the walkthrough as a PDF with step by step images, click on the following link:
Share this post
- 0 comment
- Tags: Dynamics AX
0 comment