Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Answered

how to filled fields in a new form

Posted on by 5

I have a Kalibrace2 button on the InventTable form.

pastedimage1627483549596v2.png

If I click on this button, a new form should open with  filled fields Cislo Polozky(ItemID) and Nazev Polozky (ItemName). But only Cislo Polozky(ItemId) field looks like filled.

pastedimage1627483558299v3.png

 

Here is my project tree

pastedimage1627483567005v4.png

 

Here is the content of the InitValue method. But this.CisloPolozky has an empty value.

 pastedimage1627483576372v5.png

 

 Could anybody help how to reach my goal to have a filled fields CisloPolozky(ItemId) and NazevPolozky(Itemname) in my form Kalibrace?

Is it better to use methods or make relationship between Kalibrace and InventTable?

The relationship InventTalbe.ItemId=Kalibrace.CisloPolozky and InventTable.ItemName=Kalibrace.NazevPolozky is working but I think that it is not the correct way.

Thanks in advance.

Vladimír

 

  • Martin Dráb Profile Picture
    Martin Dráb 225,588 Super User on at
    RE: how to filled fields in a new form

    The ideal solution would be throwing away NazevPolozky, because it violates data normalization rules. Then you'll add relations to both tables and leave the initialization to AX. It doesn't require even a single line of code. Therefore you're dealing with problems that you caused to you by yourself.

    If you want to find the problem in your code, please use the debugger, as I mentioned in the previous reply. This is a crucial tool - the sooner you stop ignoring it, the better for you. Run your code in the debugger, check where it fails and share your findings with us.

  • VladaVlada Profile Picture
    VladaVlada 5 on at
    RE: how to filled fields in a new form


    What if I have the same button Kalibrace2 on the form InventOnHandItem?


    I added a simmilar code, but it doesn't work properly. It works only when I create a new record in opened kalibrace form but not when I run the form from inventOnHandItem.(not during the initialization)


    public void initValue()
    {
    InventTable   inventTableForm = element.args().record();
    InventSum  inventSumForm = element.args().record();
    
    super();
    
    if (inventTableForm)
    {
     Kalibrace.CisloPolozky = inventTableForm.ItemId;
     Kalibrace.NazevPolozky = inventTableForm.ItemName;
    }
    
    if (inventSumForm)
    {
    Kalibrace.CisloPolozky = inventSumForm.ItemId;
    Kalibrace.NazevPolozky = inventSumForm.itemName();
    
    }
    
    info(Kalibrace.CisloPolozky   Kalibrace.NazevPolozky);
    
    }

  • Martin Dráb Profile Picture
    Martin Dráb 225,588 Super User on at
    RE: how to filled fields in a new form

    You're welcome. :)

    Having relations has many other benefits, therefore trying to avoid them isn't a particularly good idea.

    For example, they automatically join form data sources, allow easily setting join conditions in queries, lookup forms are generated from relations, data consistency can be checked based on relations and so on.

  • VladaVlada Profile Picture
    VladaVlada 5 on at
    RE: how to filled fields in a new form

    The Kalibrace datasource is the right place for InitValue() method. I placed it there and it works how I need.

    Kalibrace2.jpg

    Now I don't need any relations between tables and have only kalibrace datasource. Exactly how I wanted.

    Thank you so much.

  • Martin Dráb Profile Picture
    Martin Dráb 225,588 Super User on at
    RE: how to filled fields in a new form

    As I said, that's a wrong place. It's called just once, not every time when you create a record. Also, it happens before the query gets executed and records are loaded from database. That's a good thing in this case, because your code will just have no effect - otherwise you would be  overwritting values of the currently loaded record. That would be a critical bug.

    It seems to me that you aren't using the debugger, otherwise you would have a better idea about where your code gets called and what it does. You should start using it. Also, don't forget about the product documentation, such as Event Method Sequences in Form Scenarios.

  • VladaVlada Profile Picture
    VladaVlada 5 on at
    RE: how to filled fields in a new form

    Please see below where is the init method defined

    Kalibrace.jpg

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 225,588 Super User on at
    RE: how to filled fields in a new form

    First of all, on which object is the init() method defined? It's most likely a wrong place. Both form's and data source's init() method is called just once, when the form is being loaded. The right method for initializing every new record is initValue().

    By the way, please always use Insert > Insert Code (in the rich formatting view) to paste source code:

    public void initValue()
    {
    	InventTable inventTableFrom = element.args().record();
    
    	super();
    
    	if (inventTableFrom)
    	{
    	    kalibrace.CisloPolozky = inventTableFrom.ItemId;
    	    kalibrace.NazevPolozky = inventTableFrom.ItemName;
    	}
    
    	info(kalibrace.CisloPolozky   kalibrace.NazevPolozky);
    }

  • VladaVlada Profile Picture
    VladaVlada 5 on at
    RE: how to filled fields in a new form

    Hello Martin,

    Thanks a lot.

    I did the init method on Kalibrace form according to your advice and it works.

    The code looks like:

    public void init()
    {

    InventTable inventTableFromForm;
    ;
    super();

    if (element.args().record())
    {
    inventTableFromForm = element.args().record();
    Kalibrace.CisloPolozky = inventTableFromForm.ItemId;
    Kalibrace.NazevPolozky = inventTableFromForm.ItemName;

    }

    checkFailed(strfmt(Kalibrace.CisloPolozky + Kalibrace.NazevPolozky)); // here I can see the values from inventTable 

    }

    But I am not able to put that values into the fields in Kalibrace table.

  • ergun sahin Profile Picture
    ergun sahin 8,812 Super User on at
    RE: how to filled fields in a new form

    I agree that it must be in English, in its simplest form I just experienced it. I got lost in the words while I was examining. I didn't realize that InventTable was empty because I was thinking which was Item and which was ItemName

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 225,588 Super User on at
    RE: how to filled fields in a new form

    According to your code, you should never get any value, because you're getting ItemId from a newly declared variable (at line 4) that you never populate with anything.

    I guess that your code has no effect at all and the value gets populated based on the relation defined between tables. The correct relation is just through ItemId, because that's the primary key of InventTable. If you have a good reason for duplicating ItemName in your table, initialize it below super() in initValue().

    If you wanted to set ItemId in code, you would need to get the caller InventTable record:

    InventTable inventTable = element.args().record();

    Just make sure that you call the form through a menu item. It's also good to explicitly set the DataSource property of the menu item button.

    By the way, you should always create element names in English. Maybe you don't see the need right now, but you can easily get into a situation when using other language causes problems. For example, your company will expand and people from other countries will need to work with your objects. Or maybe you'll want to outsource some work to another country. Or you'll decide to sell your solution to another company...

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,986 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,588 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans