80671ae development ii in microsoft dynamics ax 2012 r3 cu8 hol

Upload: teofil

Post on 20-Feb-2018

235 views

Category:

Documents


2 download

TRANSCRIPT

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    1/25

    0671AE: Development II in MicrosoftDynamics_AX 2012 R3 CU8ands-On-Lab

    Learning Portal

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    2/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Table of Contents

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8 .......................................... 1

    Exercise 1 Print to the Screen ........................................................................................................................................ 2

    Exercise 2 Debug the Job ............................................................................................................................................... 3

    Exercise 3 Create an XML Developer Document ........................................................................................................... 4

    Exercise 4 Create a Times Table Using a While Loop ..................................................................................................... 5

    Exercise 5 Create a Times Table Using a Do...while Loop .............................................................................................. 6

    Exercise 6 Create a Times Table Using a for Statement ................................................................................................ 7

    Exercise 7 Create a Yes No Box ...................................................................................................................................... 8

    Exercise 8 Create an Infolog Tree .................................................................................................................................. 9

    Exercise 9 Create a Dialog Box ..................................................................................................................................... 10

    Exercise 10 Use X++ Control Statements ..................................................................................................................... 11

    Exercise 11 Create a New Class ................................................................................................................................... 12

    Exercise 12 Allow Access to Methods .......................................................................................................................... 13

    Exercise 13 Instantiating a Class .................................................................................................................................. 14

    Exercise 14 Use Method Parameters ........................................................................................................................... 15

    Exercise 15 Create a Run Method ............................................................................................................................... 16

    Exercise 16 Create a Calculator Class .......................................................................................................................... 17

    Exercise 17 Retrieving Data ......................................................................................................................................... 19

    Exercise 18 Update ...................................................................................................................................................... 20

    Exercise 19 Create a Query Using X++ ......................................................................................................................... 21

    Exercise 20 Handle an Exception ................................................................................................................................. 22

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    3/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 1 of 23

    80671AE: Development II in Microsoft

    Dynamics AX 2012 R3 CU8

    Objectives After completing this lab, you will be better able to:

    Use the X++ editor to write code and the various operators available in X++

    Complie code and review errors with the Complier

    Create breakpoints and step through executing code with the Debugger

    Declare and use extended data types for variables

    Control program flow using condidtional statements in X++

    Repetitively call the same blocks of code by using Loop statements

    Create and use classes, methods

    Access the Database

    Handle exception

    Scenario As a member of the Microsoft Dynamics AX 2012 R3 CU8 project team forAdventure Works Cycles you are asked to set up various items in Dynamics AX.

    Estimated Time to

    Complete This Lab30 Minutes

    Computers used in this

    Lab AX2012R3

    The password for the Administrator account on all computers in this lab is:

    w0rd@p@$$

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    4/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 2 of 23

    Exercise 1

    Print to the Screen

    ScenarioThe client wants you to demonstrate how to print a message to the screen. Create a job that prints a message to

    the screen.

    Create a job that prints the message "Microsoft Dynamics AX is fantastic.

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1. Print to the Screen

    a. Press Ctrl + Dinside the development workspace to open the AOT.

    b. Right-click the Jobsnode and select New Job.

    c. Select the jobs name in the first row.

    d. Rename the job.

    e. Add the following code: info("Microsoft Dynamics AX is fantastic.");.

    f. Click the Compilebutton on the toolbar or press F7 to compile..

    g. Press F5or click the Gobutton on the toolbar to run the job.

    h. Close the Infolog dialog.

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    5/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 3 of 23

    Exercise 2

    Debug the Job

    ScenarioThe client wants you to verify the code executes without errors. Debug the job that you created in the previous

    lab.

    Set a breakpoint in the code in the job from the previous practice. Run the code and step through it.

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1. Debug the Job

    a. In the AOT, click the Jobs node.

    b. Double-click on the job that created in the Print to the Screenexercise.

    c. Select the line of code: static void (Args _args).

    d. Click the Toggle breakpoint icon.

    e. Save the code by pressing Ctrl+S.

    f. Press the F5 key to run the code.

    g. Step through the code by using the icon on the toolbar or F11.

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    6/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 4 of 23

    Exercise 3

    Create an XML Developer Document

    ScenarioYou have been asked to create developer documentation for the Credit Limit modifications made in the standard

    Microsoft Dynamics AX application.

    Create an XML file by using the XML documentation generation, for the Credit Limit development project.

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1. Create an XML

    Developer

    Document

    a. Press Ctrl+Shift+Pinside the development workspace to open the development

    project tree.

    b. Find the Credit Limitproject.

    c. Right-click the project, and select Add-Ins> Extract XMLdocumentation.

    d.

    Click the Folder icon under the Documentation check box.

    e. Enter a file name.

    f. Click Save.

    g. Clear the Reflection check box.

    h. Click OK.

    i. Click Closeto close the Infologwindow. The .xmlfile will be created at the

    specified path.

    j. Go to the path provided by you for the file created on your system.

    k. Review the file created.

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    7/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 5 of 23

    Exercise 4

    Create a Times Table Using a While Loop

    ScenarioIsaac has been asked to demonstrate the use of a while statement by printing out the multiplication table for 2.

    Create a job that prints the multiplication table for 2 using a while statement

    Have the list contain all even numbers up to 50

    Use a 'counter' variable to increment every time

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1. Create a Times Table

    Using a While Loop

    a. Open the AOT.

    b. Right-click the Jobsnode and select New Job.

    c.

    Rename the job.d. Add the following code:

    int counter = 1;

    while (counter

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    8/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 6 of 23

    Exercise 5

    Create a Times Table Using a Do...while Loop

    ScenarioIsaac has been asked to demonstrate the use of a do...while statement by printing out the multiplication table for

    2.

    Create a job that prints the multiplication table for 2 using a do...while statement

    Have the list contain all even numbers up to 50

    Use a 'counter' variable to increment every time

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1. Create a Times Table

    Using a Do...while

    Loop.

    a. Open the AOT.

    b. Right-click the Jobsnode and select New Job.

    c. Rename the job.

    d.

    Add the following code.

    int counter = 1;

    do

    {

    print counter * 2;

    counter++;

    }

    while(counter

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    9/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 7 of 23

    Exercise 6

    Create a Times Table Using a for Statement

    ScenarioIsaac has been asked to demonstrate the use of a for statement by printing out the multiplication table for 2.

    Create a job that prints the multiplication table for 2 using a for statement

    Have the list contain all even numbers up to 50

    Use a 'counter' variable to increment every time

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1. Create a Times Table

    Using a for

    Statement.

    a. Open the AOT.

    b. Right-click the Jobsnode and select New Job.

    c.

    Rename the job.d. Add the following code.

    int counter;

    for(counter = 1; counter

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    10/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 8 of 23

    Exercise 7

    Create a Yes No Box

    ScenarioIsaac has been asked to create a job that will prompt the user to continue or cancel. The user should be asked

    whether he or she wants to continue and be able to choose Yes or No.

    Create a new job in the AOT that contains a box where the user can decide to continue or cancel.

    The caption on the window is "Question"

    The text should be "Do you want to continue?"

    The default button is "Yes"

    Tasks Detailed Steps

    Complete the followingtasks on:

    AX2012R3

    1. Create a Yes No Box.

    a. Open the AOT.

    b. Right-click the Jobsnode and select New Job.

    c. Rename the job.

    d. Add the following code:

    DialogButton dialogButton;

    dialogButton = Box::yesNoCancel("Do you want to continue?", DialogButton::Yes,

    "Question", "Bottom help text");

    e. Press F7or click the Compilebutton in the toolbar to compile the code.

    f. Press F5or click the Go button in the toolbar to run the job.

    g. Click on Yes/No/Cancelto close the window.

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    11/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 9 of 23

    Exercise 8

    Create an Infolog Tree

    ScenarioIsaac has been asked to create an Infolog Tree that has an Info message, a Warning message and an Error message.

    Create a tree of Infolog messages using a job in Microsoft Dynamics AX. The tree should have a root node which

    states: "This is the Infolog Tree lab." The tree should contain at least one of each type of message (Info, Warning

    and Error).

    Tasks Detailed Steps

    Complete the followingtasks on:

    AX2012R3

    1. Create an Infolog

    Tree.

    a.

    Open the AOT.b. Right-click the Jobsnode and select New Job.

    c. Rename the job.

    d. Add the following code.

    setPrefix("This is the Infolog Tree lab");

    info("Info message");

    warning("Warning message");

    error("Error message");

    e. Press F7or click the Compilebutton in the toolbar to compile the code.

    f.

    Press F5or click the Go button in the toolbar to run the job.

    g. Close the Infolog dialog.

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    12/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 10 of 23

    Exercise 9

    Create a Dialog Box

    ScenarioIsaac has been asked to create a dialog box that will prompt the user to enter a customer account number and a

    sales order ID.

    Create a dialog box that lets the user select a customer account number and a sales order ID. Once the user clicks

    OK, show the values of each selection using the Infolog.

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1. Create a Dialog Box.

    a. Open the AOT.

    b. Right-click the Jobsnode and select New Job.

    c.

    Rename the job.

    d. Add the following code.

    Dialog dialog;

    DialogGroup dialogGroup;

    DialogField dialogFieldCustAccount;

    DialogField dialogFieldSalesId;

    dialog = new Dialog("Simple Dialog");

    dialogGroup = dialog.addGroup("Customer");

    dialogFieldCustAccount = dialog.addField(extendedTypeStr(CustAccount));

    dialogFieldSalesId = dialog.addField(extendedTypeStr(SalesId));

    if(dialog.run())

    {

    info(dialogFieldCustAccount.value());

    info(dialogFieldSalesId.value());

    }

    e. Press F7or click the Compilebutton in the toolbar to compile the code.

    f. Press F5or click the Go button in the toolbar to run the job.

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    13/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 11 of 23

    Exercise 10

    Use X++ Control Statements

    ScenarioIsaac has been asked to create a job that will ask the user if he or she wants to display his or her date of birth. If

    the user chooses Yes, the system should print the user's name and date of birth. If the user chooses No, the

    system should only print the user's name.

    Create a dialog for the user that determines, based on input, what appears on a screen.

    Create a new job in the AOT and declare and initialize a DialogButton object variable and a container data

    type variable

    The three elements of the container are your first name, last name, and birth date

    The Dialogbutton should be a Yes Nobox with the text "Do you want to display your birth date?" with

    Yesbeing the default button

    If Yesis clicked - first name, last name, and birth date appear

    If Nois clicked - only first and last name appear

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1. Use X++ Control

    Statements

    a. Open the AOT.

    b. Right-click the Jobsnode and select New Job.

    c. Rename the job.

    d. Add the following code:

    DialogButton dialogButton;

    container nameAndDOB;

    nameAndDOB = ["John","Doe",mkDate(28,9,71)];

    dialogButton = Box::yesNo("Do you want to see the birthdate?",

    DialogButton::Yes);

    if (dialogButton == DialogButton::Yes)

    {

    Box::info(strFmt("%1 %2, %3", conPeek(nameAndDOB,1),

    conPeek(nameAndDOB,2), date2str(conPeek(nameAndDOB,3),-1,-1,-1,-1,-1,-1)));

    }

    else

    {

    Box::info(strFmt("%1 %2",conPeek(nameAndDOB,1), conPeek(nameAndDOB,2)));

    }

    e. Press F7 or click the Compilebutton in the toolbar to compile the code.

    f. Press F5or click the Gobutton in the toolbar to run the job.

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    14/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 12 of 23

    Exercise 11

    Create a New Class

    ScenarioYou have been asked to create a class that will print your name to the screen.

    Create a new class that has one class variable that can hold your name. Create a method that will set the value of

    the variable to be your name. Create another new method that can print the value of the variable.

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1. Create a New Class.

    a. Open the AOT.

    b. Right-click the Classes node, and then select New Class.

    c. Rename the class to PrintMyName.

    d. Expand the node for the newly created class.

    e. Double-click classDeclaration(usr).

    f. Declare a variable of type String, called "myName" as the below code:

    class PrintMyName

    {

    str 20 myName;

    }

    g. Press F8 to compile and save classDeclaration.

    h. Right-click the PrintMyName(usr) class and select New > Method.

    i. Rename the method to "setMyName".

    j.

    Enter the code in the setMyNamemethod shown below into your method:

    private void setMyName()

    {

    myName = "Isaac";

    }

    k. Press F8to compile and save the method.

    l. Right-click the PrintMyName(usr) class and select New > Method.

    m.

    Rename the method to "printMyName".

    n. Enter the code in the printMyNamemethod show below into your method.

    private void printMyName()

    {

    print MyName;

    }

    o. Press F8to compile and save the method.

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    15/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 13 of 23

    Exercise 12

    Allow Access to Methods

    ScenarioAllow the methods created in the previous lab to be called by code anywhere in the application.

    The methods in the class created in the previous lab are marked private by default and so they cannot be accessed

    by other application elements. You have been asked to allow the method to be accessed by other application

    elements

    Change the modifier of each method to Public.

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1. Allow Access to

    Methods

    a. Open the AOT.

    b. Find the PrintMyName(usr) class created in the previous lab.

    c.

    Double-click each method in the class.

    d. Change the method modifier from privateto Public.

    e. Save the changes.

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    16/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 14 of 23

    Exercise 13

    Instantiating a Class

    ScenarioYou have been asked to execute the code written in the previous lab.

    Create a job that instantiates the class written in the previous lab, and executes the two methods you created.

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1.

    Instantiating a Class.

    a. Open the AOT.

    b. Right-click the Jobsnode, and then select New Job.

    c. Type the following code into the job:

    PrintMyName PrintMyName;

    PrintMyName = new PrintMyName();PrintMyName.setMyName();

    PrintMyName.printMyName();

    pause;

    d. Press F7 or click the Compilebutton in the toolbar to compile the code.

    e. PressF5to compile and run the job.

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    17/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 15 of 23

    Exercise 14

    Use Method Parameters

    ScenarioIsaac has been asked to write a method that accepts a parameter.

    Modify your class to print your name, by allowing the method that sets the name variable to accept a parameter

    and set the name variable from that parameter. You will also need to modify the job so that you pass your name

    into the method when you run it.

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1.

    Use Method

    Parameters.

    a.

    Open the AOT.

    b. Expand Classes.

    c. Find the PrintMyName(usr)class created earlier in this lesson that prints your

    name.

    d. Double-click the method setMyName(usr).

    e. Modify the code so the method looks the same as shown in the following

    example:

    public void setMyName(str _myName)

    {

    myName = _myName;

    }

    f. Press F8 to save and compile the code.

    g. In the AOT, expand Jobs.

    h.

    Find the Job1(usr)job that is created in the Instantiating a Classexercise toexecute the class.

    i. Modify the job so that the line that calls the setMyName()method is called

    with your name.

    static public void executePrintMyName(Args _args)

    {

    PrintMyName PrintMyName;

    PrintMyName = new PrintMyName();

    PrintMyName.setMyName("Isaac");

    PrintMyName.printMyName();

    pause;

    }

    j. Press F5 to save and run the job.

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    18/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 16 of 23

    Exercise 15

    Create a Run Method

    ScenarioIsaac is required to create a method that calls other methods in the same class.

    Add a method named run to the class that prints your name. From this method call the two methods that set and

    print the value. Modify the job that executes the class to call the new run method.

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1. Create a Run

    Method.

    a. Open the AOT.

    b. Expand Classes.

    c. Find the PrintMyName(usr) class created earlier in this lesson that prints your

    name.

    d.

    Right-click the class and select New >Method.

    e. Modify the code so the method looks the same as shown in the following

    example.

    public void run()

    {

    this.setMyName("Isaac");

    this.printMyName();

    }

    f.

    Press F8 to save and compile the code.

    g. Expand Jobs in the AOT.

    h.

    Find the executePrintMyName(usr)job in the AOT that executes the class.i. Modify the job to the code shown in the following example.

    static void executePrintMyName(Args _args)

    {

    PrintMyName PrintMyName;

    PrintMyName = new PrintMyName();

    PrintMyName.run();

    pause;

    }j. Press F5to save and run the job.

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    19/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 17 of 23

    Exercise 16

    Create a Calculator Class

    ScenarioYou have been asked to create a class than can do simple calculations on two numbers.

    Create a class that has two class variables that are set using accessor methods. These variables are to hold 2

    numbers. Add four methods that will add, subtract multiply and divide these 2 numbers, and then return the

    calculated value. Write a job to test your class.

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1.

    Create a Calculator

    Class.

    a.

    Open the AOT.

    b. Right-click the Classesnode and select New Class.

    c. Rename the class to Calculator.

    d.

    Expand the node for the newly created class.

    e. Double-click the classDeclaration(usr).

    f. Modify the class to the code shown in the following example:

    public class Calculator

    {

    real value1;

    real value2;

    }

    g.

    Press the F8 key to save and complie the code.

    h. In AOT, right-click the Calculator(usr) class, and then click New > Method.

    i.

    Modify the method to the code shown in the following example:

    public real parmValue1(real _value1 = value1)

    {

    value1 = _value1;

    return value1;

    }

    j. Click New to create new method.

    k. Modify the method to the code shown in the following example:

    public real parmValue2(real _value2 = value2)

    {

    value2 = _value2;

    return value2;

    }

    l. Click New to create new method.

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    20/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 18 of 23

    Tasks Detailed Steps

    m. Modify the method to the code shown in the following example:

    public real add()

    {

    return value1 + value2;

    }

    n. Click New to create new method.

    o. Modify the method to the code shown in the following example:

    public real subtract()

    {

    return value1 - value2;

    }

    p. Click New to create new method.

    q. Modify the method to the code shown in the following example:

    public real multiple()

    {

    return value1 * value2;

    }

    r. Click New to create new method.

    s. Modify the method to the code shown in the following example:

    public real divide()

    {

    return value1 / value2;

    }

    t. Press F8to save and compile the code.

    u.

    Open the AOT.

    v. Right-click the Jobsnode and select New Job.

    w. Rename the job to Calculatorand add below code to the code shown in the

    following example:

    static void calculator(Args _args)

    {

    Calculator calc = new Calculator();

    calc.parmValue1(10.00);

    calc.parmValue2(2.00);

    info(strFmt(" Add = %1",calc.add()));

    info(strFmt(" Sub = %1",calc.subtract()));info(strFmt(" Mul = %1",calc.multiple()));

    info(strFmt(" Devide = %1",calc.divide()));

    }

    x. Press F5to save and run the job.

    y. Close the Infolog form.

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    21/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 19 of 23

    Exercise 17

    Retrieving Data

    ScenarioIsaac has been asked to produce a list of customer account numbers and names sorted by name.

    Create a job that will retrieve all customer accounts and print the account number and name. The list should be

    sorted by name. Note that the customer name is not stored in the customer table; it is stored in the DirPartyTable,

    which is related to the customer table through the CustTable.Party field and the DirPartyTable.RecId field.

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1.

    Retrieving Data.

    a.

    Open the AOT.

    b. Right-click the Jobsnode and select New Job.

    c. Add the following code:

    CustTable custTable;

    DirPartyTable dirPartyTable;

    while select dirPartyTable order by Name

    join custTable

    where custTable.Party == dirPartyTable.RecId

    {

    info(dirPartyTable.Name+', '+ custTable.AccountNum);

    }

    d. Press F5to save and run the job.

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    22/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 20 of 23

    Exercise 18

    Update

    ScenarioThe item table needs to be updated so that all items in the item group TV&Video have a purchase tolerance of 2

    percent. Isaac has been asked to write a job that will achieve this.

    Write a job that will find all items in the Television item group, and set the price tolerance group to 2 percent.

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1. Update.

    a. Open the AOT.

    b. Right-click the Jobsnode and select New Job.

    c. Add the following code:

    InventTable inventTable;

    InventItemGroupItem inventItemGroupItem;

    ttsbegin;

    while select forupdate inventTable

    exists join inventItemGroupItem

    where inventItemGroupItem.ItemId == inventTable.ItemId

    && inventItemGroupItem.ItemGroupId == 'TV&Video'

    {

    inventTable.ItemPriceToleranceGroupId ="2%";

    inventTable.update();

    }

    ttscommit;

    d. Press F5to save and run the job.

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    23/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 21 of 23

    Exercise 19

    Create a Query Using X++

    ScenarioIsaac has been asked to provide a list of vendors of packaging materials.

    Write a job that will produce a list of vendors. The job should build a query using X++, and the query should have a

    range that limits the list to vendors in vendor group "10", and sort by account number.

    Add code that will allow the user to modify the query ranges at run time.

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1.

    Create a Query

    Using X++.

    a. Open the AOT.

    b. Right-click the Jobsnode and select New Job.

    c. Add the following code.

    Query query;

    QueryRun queryRun;

    QueryBuildDataSource qbds;

    QueryBuildRange qbr;

    VendTable vendTable;

    query = new Query();

    qbds = query.addDataSource(TableNum(VendTable));

    qbr = qbds.addRange(FieldNum(VendTable,VendGroup));

    qbr.value('10');

    qbds.addSortField(FieldNum(VendTable,AccountNum));

    queryRun = new QueryRun(query);

    if (queryRun.prompt())

    {

    while (queryRun.next())

    {

    vendTable = queryRun.get(tableNum(VendTable));

    info(vendTable.Name());

    }

    }

    d. Press F5to save and run the code.

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    24/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Page 22 of 23

    Exercise 20

    Handle an Exception

    ScenarioIn this practice, you will use the exception handling features that are available in X++.

    The Accounts Receivable Department has asked for a basic form that will create a new customer. They have

    requested that the user is prompted for the account number, name and all mandatory fields. If the user does not

    enter all the required data, the system must report which fields are missing, show a message that states "Please

    enter all required fields," and not commit any data to the database. If any error occurs the system will display a

    message that states "An error occurred. Please try again."

    Create a new class that will prompt the user for a new customer account number, the name and all fields that are

    mandatory for the customer records. Use existing methods to validate the record before inserting the record into

    the database. In the case that not all data was entered, throw an error exception that has the error message:

    "Please enter all required fields." The code to create the record must be inside a try/catch statement. This mustinclude a catch for the error exception type and display the message: "An error occurred. Please try again.".

    Tasks Detailed Steps

    Complete the following

    tasks on:

    AX2012R3

    1. Handle an

    Exception.

    a. Open the AOT.

    b. Find the Classesnode.

    c. Right-click and select New Class.

    d. Expand the Class1(usr) node for the newly created class.

    e. Double-click the classDeclaration(usr).

    f. Rename the class to CustCreateCustomeras the below example:

    class CustCreateCustomer

    {

    }

    g. Press F8 to save and complie classDeclaration.

    h.

    Right-click the CustCreateCustomer(usr) class in the AOT.

    i. Click New > Method.

    j. Modify the method to the code shown in the following example:

    void run()

    {

    Dialog dlg = new Dialog("Create new customer");

    DialogField dlgCust;DialogField dlgGrp;

    DialogField dlgCur;

    CustTable custTable;

    dlgCust = dlg.addField(extendedTypeStr(CustVendAc), "Customer account");

    dlgGrp = dlg.addField(extendedTypeStr(CustGroupId));

    dlgCur = dlg.addField(extendedTypeStr(CurrencyCode));

  • 7/24/2019 80671AE Development II in Microsoft Dynamics AX 2012 R3 CU8 HOL

    25/25

    80671AE: Development II in Microsoft Dynamics AX 2012 R3 CU8

    Tasks Detailed Steps

    if (dlg.run())

    {

    try

    {

    custTable.AccountNum = dlgCust.value();

    custTable.CustGroup = dlgGrp.value();

    custTable.Currency = dlgCur.value();

    if (!custTable.validateWrite())

    {

    throw error("Please enter all required fields.");

    }

    else

    {

    custTable.insert();

    }

    }

    catch (Exception::Error)

    {

    error("An error occurred. Please try again");

    }

    }

    }

    k.

    Click New to create another method.

    l. Modify the method to the code shown in the following example:

    static void main(Args _args)

    {

    CustCreateCustomer custCreateCustomer = new CustCreateCustomer();

    custCreateCustomer.run();

    }

    m. Press F8 to save and complie the method.

    n. Right-click the CustCreateCustomer(usr) class in AOT, and then click Open.