Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

Creating 3 rows with default values on Form (Route)

Posted on by 5

I've created a CommandButton (New) and I'd like to set the button to create 3 records in the form with different default values.

I'm using the InitValue() against the correct datasource (ROUTE)

currently my code looks like this - however it only returns one record!

public void initValue()
{

    super();
  
    //Set default values for the 1st record
    Route.OprId = 'Kitting';
    Route.OprNum = 5;
    Route.insert();
    
    //Set default values for the 2nd record
    Route.OprId = 'ASSY005';
    Route.OprNum = 10;
    Route.insert();
    
    //Set default values for the 3rd record
    Route.OprId = 'ASSY010';
    Route.OprNum = 15;
    Route.insert(); 
}

I would also like this update to only happen when I press the commandbutton in question (I will need to create another command button that should operate as per AX 2012 standard functionality)

Could anyone please advise?

Thank you

  • RFuller Profile Picture
    RFuller 5 on at
    RE: Creating 3 rows with default values on Form (Route)

    Thanks Martin,

    I'll start to do some research on how to do the above.

    Thank you for your input

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,188 Most Valuable Professional on at
    RE: Creating 3 rows with default values on Form (Route)

    In that case, you'd quickly see the flaw in your code, because you're trying to add those three records every time when a new record is created, i.e. every time when an engineer tries to add a new operation. Clearly running your code on record creation is a mistake. Use a regular button, override its clicked() button (if it's a standard form, use an event handler or a control extension) and call a method with your code from there. Don't use any command button.

    By the way, think about what will happen if the button is pressed for second time. Maybe you want to check whether the records exists before creating them.

  • RFuller Profile Picture
    RFuller 5 on at
    RE: Creating 3 rows with default values on Form (Route)

    Hi Martin,

    Thanks for the response.

    The scenario I have is that users need to create a new route in AX, but they also need to add/delete operations on that route. So for instance an engineer would create a new blank route, for this the first 3 records will always be the same with default values. However they still need the option to add more operations to that route so they will need to keep the functionality of creating new operations. I just want to save them some time by having a button on the form that if they press, it will populate the first 3 rows and save them time.

    Does that make sense?

    I'm fairly new to the development side of things so any pointers on what methods to use or overwrite would be very much appreciated.

    Thank you

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,188 Most Valuable Professional on at
    RE: Creating 3 rows with default values on Form (Route)

    Your current code is definitely wrong. initValue() method is called when a new record is being initalized and its purpose is initializing default values of fields. The record may later saved by user (if its validated successfully) and the work is done by write() method. Your code doesn't do what initValue() is for and therefore it doesn't belong there. If users can create new records, these three records would try to be created every time.

    Another problem is in the you create records, but you don't load them to the form.

    I don't thing that you should be using any command button at all. You should disable creation of new records (AllowCreate=No), use a regular button (not a command button) and call your code from there. After inserting records, call research() method of the data source.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans