Skip to main content

Notifications

Announcements

No record found.

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

Modified method not working as expected in a certain scenario

(2) ShareShare
ReportReport
Posted on by 1,249
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:
  • Bharani Preetham Peraka Profile Picture
    Bharani Preetham Pe... 3,587 Super User 2024 Season 1 on at
    Modified method not working as expected in a certain scenario
    When you select "Codex1", since it is having 2 records with it, it is just selecting the first record which is a ideal solution. We can think on a different solution.
     
    Can you confirm this that we will always have multiple records like for each codex?
     
    Id1 Codex1
    Id2 Codex1
     
    Something like above?
     
     
  • Martin Dráb Profile Picture
    Martin Dráb 230,458 Most Valuable Professional on at
    Modified method not working as expected in a certain scenario
    No, I don't. I was talking about unique indexes in both cases. The surrogate unique key is the record ID generated under the hood. A natural unique key is a key consisting of fields with business data (e.g. a customer account ID).
     
    You didn't mention before that Number had Allow Duplicate = No. But anyway, if Number is unique, using it is your other option.
  • .. Profile Picture
    .. 1,249 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
  • Martin Dráb Profile Picture
    Martin Dráb 230,458 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,249 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
    Martin Dráb 230,458 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,249 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
    Martin Dráb 230,458 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,249 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
     
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,458 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.

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans