web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

Modified method not working as expected in a certain scenario

(2) ShareShare
ReportReport
Posted on by 1,907
Hi,

I have a custom table1 with the following fields
Id         name
id1        name1
id2        name2     

and another custom table2, the relation between table1 and tabl2 is Id
Number    Id       codeX      codeY      Field1
1               id1     codex1     codey1      fieldz
2               id2     codex1     codey1      fieldz
 
 
and in SalesLine, i added a field called codeX and Id  (relation between salesLine and table2 is codeX, and relation between SalesLine and table1 is Id)
and i overrided the lookup of this field codeX in salesLine, by creating a view that has table2 and table1, where it returns the following:
CodeX, name, Field1

so now when i click on the dropdown of this field I'm seeing this:
CodeX      name     Field1
codex1     name1   fieldz
codex1     name2   fieldz


my issue is if i choose in the lookup codex1 with name2, it actually takes codex1 with name1 instead.

 
[ExtensionOf(FormDataFieldStr(SalesTable, SalesLine, CodeX))]
final class SalesTableFormSalesLineDS_CodeX_Extension
{
    public void modified()
    {
        FormDataSource          formDataSource      = element.datasource();
        SalesLine               salesLine           = formDataSource.cursor();
        CodeX                   codeX               = salesLine.CodeX;

        next modified();

        Table1  table1;
        Table2  table2;

        select firstOnly CodeX, Id from table2
            where table2.CodeX == salesLine.CodeX
        join Id from table1
            where table1.Id == table2.Id


        salesLine.CodeX   = codeX;
        salesLine.Id      = table2.Id;

    }

}

 
Categories:
I have the same question (0)
  • Martin Dráb Profile Picture
    236,513 Most Valuable Professional on at
    Modified method not working as expected in a certain scenario
    It's not clear what you mean by "it takes" (a more detailed description would help), but do you realize that your code completely ignores the new value and it change salesLine.CodeX to the previous value? Honestly, I have no idea what you're trying to do in your modified() method. You've given us no explanation either; you talked just about lookup(), not modified().
  • .. Profile Picture
    1,907 on at
    Modified method not working as expected in a certain scenario
    Hi Martin,

    I mean, when i click on the field SalesLine.CodeX to choose a value, then I want SalesLine.Id to be populated based on the value that I chose.

    So as I said, when i click on the field, the lookup returns this:
    CodeX      name     Field1
    -------      -------    -----
    codex1     name1   fieldz
    codex1     name2   fieldz
     
     
    now if i choose from the lookup, codex1 with name2, modified field method is called and then I'm expecting SalesLine.Id to be populated with Id2 but it's being populated with Id1 instead.

    ** even if i move the line of code u mentioned after the super, still same issue
     
  • Suggested answer
    Martin Dráb Profile Picture
    236,513 Most Valuable Professional on at
    Modified method not working as expected in a certain scenario
    All right, so your requirement is about changing a value, not about a lookup, so let's stop talking about lookups. Using a lookup form is just one of ways how to change the value. You can simply type it in without using a lookup.
     
    If your requirement is to change the value of Id field, your code changing CodeX is a bug, because you have no such a requirement. Throw it away.
     
    If you want this logic in all forms, using modified() method on a form is wrong. You should use modifiedField() method on the table (or the corresponding event). In SalesLine, the best place is modifyField() method, called from modifiedField().
     
    In your select statement, you don't need any table1 buffer at all. Simply find Id in table2 by CodeX:
    public void modifyField(FieldId _fieldId, boolean _userInput, boolean _uiEnabled)
    {
        next modifyField(_fieldId);
    
        switch (_fieldId)
        {
            case fieldNum(SalesLine, CodeX):
                Table2 table2;
                select firstOnly Id from table2
                    where table2.CodeX == this.CodeX;
                this.Id = table2.Id;
                break;
    }
    It assumes that there can't be two rows in Table2 with the same CodeX; I'm not sure if it's the case.
     
    If something doesn't work as expected, use the debugger to find the place where things go wrong.
  • .. Profile Picture
    1,907 on at
    Modified method not working as expected in a certain scenario
    Hi Martin,

    yes that's the issue codex1 can be found twice in table2. As I showed you:

    This is tabe2:

    Number    Id       codeX      codeY      Field1
    -------       ----    -------      -------      ------
    1               id1     codex1     codey1      fieldz
    2               id2     codex1     codey1      fieldz
     
    now from the lookup field in salesLine, i chose codex1 with id2, but it populated  salesLine.Id with Id1 instead.

    At first i made the relation between salesLine and table2 as codeX only, but now i changed the relation to be:
    salesLine.Id == table2.Id &&
    salesLine.CodeX= table2.CodeX

    but still the same issue

    and the relation between salesLine and table1 is
    salesLine.Id = table1.Id

    here's table1:
    Id         name
    ---        ------
    id1        name1
    id2        name2     
     
     
    1. Do i need to make the relation between salesLine and table2 as a reference group for it to work??

    2. If i remove this line of code:
    salesLine.CodeX   = codeX;
     then the salesLine.CodeX stays empty after i choose a value, why?
     
    [ExtensionOf(FormDataFieldStr(SalesTable, SalesLine, CodeX))]
    final class SalesTableFormSalesLineDS_CodeX_Extension
    {
        public void modified()
        {
            FormDataSource          formDataSource      = element.datasource();
            SalesLine               salesLine           = formDataSource.cursor();
    
            next modified();
            
            CodeX                   codeX               = salesLine.CodeX;
    
            Table1  table1;
            Table2  table2;
    
            select firstOnly CodeX, Id from table2
                where table2.CodeX == salesLine.CodeX
            join Id from table1
                where table1.Id == table2.Id
    
    
            //salesLine.CodeX   = codeX;
            salesLine.Id      = table2.Id;
    
        }
    
    }


    note: i can move the code to table level later, but i want this one to work first
     
  • Martin Dráb Profile Picture
    236,513 Most Valuable Professional on at
    Modified method not working as expected in a certain scenario
    You can't write code if you have no idea how it should work.
     
    Finding arecord by CodeX and Id makes no sense if your goal is finding Id. If you already know it, you don't need to find it, and if you don't know it, you can't find records by it.
     
    You need to think again about what you're trying to achieve and then how you can find the record in Table2, or maybe the whole idea is wrong, because you don't have the information needed. In general, if you want to find a unique record in a table, you need to use fields that form a unique index. Therefore you need to find out unique index(es) of Table2.
     
    If you need our help with designing the solution, please explain the actual business requirement to us. Talking about things like Table1 and Id isn't good enough for that purpose.
  • .. Profile Picture
    1,907 on at
    Modified method not working as expected in a certain scenario
    Hi Martin,

    What I want is that I want to add a field in SalesLine (CodeX), when i fill this field, i need to populate salesLine.ItemId and salesLine.Id
    That's why I added this code on the modifiedField of CodeX
    [ExtensionOf(FormDataFieldStr(SalesTable, SalesLine, CodeX))]
    final class SalesTableFormSalesLineDS_CodeX_Extension
    {
        public void modified()
        {
            FormDataSource          formDataSource      = element.datasource();
            SalesLine               salesLine           = formDataSource.cursor();
    
            next modified();
            
            CodeX                   codeX               = salesLine.CodeX;
    
            Table1  table1;
            Table2  table2;
    
            select firstOnly CodeX, Id from table2
                where table2.CodeX == salesLine.CodeX
            join Id from table1
                where table1.Id == table2.Id
           
            salesLine.ItemId = table2.CodeY;
            formDataSource.object(fieldNum(SalesLine, ItemId)).modified();
    
    
            //salesLine.CodeX   = codeX;
            salesLine.Id      = table2.Id;
    
        }
    
    }

    And as  I said, I have these two custom tables:
    Table1
    Id         name
    --         -------
    id1        name1
    id2        name2     
     
    And another custom table2, the relation between table1 and tabl2 is Id
    Number    Id       codeX      codeY      Field1
    --------     ----     -------       -----       -------
    1               id1     codex1     codey1      fieldz
    2               id2     codex1     codey1      fieldz

    And i made the relation between SalesLine and table2 as follows
    salesLine.Id == table2.Id &&
    salesLine.CodeX == table2.CodeX

    Now, When I click on the drop down of CodeX in salesLine i see the following result
    CodeX      name     Field1
    ------       ------      ------
    codex1     name1   fieldz
    codex1     name2   fieldz
     
    1. so if I click on codex1 with name2, the modified method will be called and i expect it to populate salesLine.ItemId with codey1 and salesLine.Id with Id2 but currently it's populating salesLine.Id with Id1 instead, do i need to make the relation between salesLine and table2 as reference group?
     
    2.  If i remove this line of code from the modified method
    salesLine.CodeX   = codeX;
    then the salesLine.CodeX stays empty after i choose a value, why?
  • Martin Dráb Profile Picture
    236,513 Most Valuable Professional on at
    Modified method not working as expected in a certain scenario
    The problem is in your expectation. You use a lookup to select a value of CodeX field. So if you select codex1, all you have is the string codex1 assigned to CodeX field. There is zero information about the Id field. Therefore your implementation doesn't meet your requirements.
     
    As already discussed, just knowing CodeX isn't sufficient to uniquely select a record from Table2. Maybe you should use a surrogate key of Table2, or you need to rethink the whole design.
  • .. Profile Picture
    1,907 on at
    Modified method not working as expected in a certain scenario
    Hi Martin,

    so you mean i need to make the relation between the two tables based on RecId?

    and what other solution can i make?
  • Martin Dráb Profile Picture
    236,513 Most Valuable Professional on at
    Modified method not working as expected in a certain scenario
    RecId is the only certain unique key. Your table should also have a natural unique key, but you didn't provide this information.
     
    Your other options depend on the your business scenario, which is unknown to me.
  • .. Profile Picture
    1,907 on at
    Modified method not working as expected in a certain scenario
    Hi Martin,

    by natural key, you mean the index with duplicate No?

    i mentioned that Table2 has the field "Number" as allow duplicate no

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 1,850

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 795 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 519 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans