Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Answered

Event Handler Does Not Add Record In Other Table

Posted on by 2,296

Hi Everyone

I have a requirement from customer to assign specific address book to all newly created customers. To achieve this, I added a post event on Insert method and am trying to assign the address book in Event handler class method. 

pastedimage1602508008236v1.png

public static void postInsertRecordHandler(XppPrePostArgs _args)
{
    CustTable                   custTable;
    CustTableEventHandler       custTableEventHandler;

    custTable = _args.getThis();

    custTableEventHandler = new CustTableEventHandler(custTable);

    custTableEventHandler.assignAddressBook();
}


public void assignAddressBook()
{
    container   addressBooks = ["ALL"];

    DirAddressBookParty::createPartyRelationsByName(custTable.Party, addressBooks);

    info(strFmt('assignAddressBook() - All addressbook assigned to %1-%2-%3-%4', custTable.AccountNum, custTable.Party, custTable.dataAreaId, custTable.RecId));
}

The code is running successfully but it is not inserting record in DirAddressbookParty table. 

Will appreciate if anyone of you can let me know if I am missing anything for achieving the requirement.

Thanks in advance.

Haroon 

  • nmaenpaa Profile Picture
    nmaenpaa 101,148 on at
    RE: Event Handler Does Not Add Record In Other Table

    So is it now clear, do you still have open issues?

  • Suggested answer
    haroonattari Profile Picture
    haroonattari 2,296 on at
    RE: Event Handler Does Not Add Record In Other Table

    writeAddressBookParty method of DirPartyFormHandler class flushes all existing address book(s) assigned means it is also deleting the record added by event handler class.

  • Verified answer
    nmaenpaa Profile Picture
    nmaenpaa 101,148 on at
    RE: Event Handler Does Not Add Record In Other Table

    I agree. But to find out why it is missing, you need to debug.

    We have asked you to debug quite many times. Do you know how to debug? Have you debugged it? Why can't you share the findings with us? You really need to know how to debug in order to develop code.

  • haroonattari Profile Picture
    haroonattari 2,296 on at
    RE: Event Handler Does Not Add Record In Other Table

    The only missing thing is the record in DirAddressBookParty which was not added even it showed RecId (5637147599) in infolog.

  • nmaenpaa Profile Picture
    nmaenpaa 101,148 on at
    RE: Event Handler Does Not Add Record In Other Table

    Thanks! Now the only missing thing is the description about what you found out when debugging the code that doesn't work as expected.

  • Verified answer
    haroonattari Profile Picture
    haroonattari 2,296 on at
    RE: Event Handler Does Not Add Record In Other Table

    There is no error while creating customer record so I presume it is a successful execution. Regarding, your second question I also tried executing this code and it successfully created record in a custom table. 

    public void createBusinessLine()
    {
        BusinessLine    businessLine;
    
        businessLine.clear();
        businessLine.BusinessLineId = custTable.AccountNum;
        businessLine.Name           = custTable.name();
        businessLine.insert();
    }

    Regarding table browser image sort by recid descending, I have updated in my original post. 

  • nmaenpaa Profile Picture
    nmaenpaa 101,148 on at
    RE: Event Handler Does Not Add Record In Other Table

    Are these screenshots from the succesful or unsuccesful execution? Also what do you mean by "I tried creating record in another custom table and it went well"? Is it in any way related to the current problem that we are discussing?

    The table browser is not sorted by RecId descending so we can't see if the new RecId 5637147599 is there or not.

  • haroonattari Profile Picture
    haroonattari 2,296 on at
    RE: Event Handler Does Not Add Record In Other Table

    Hi Nikolaos

    I tried creating record in another custom table and it went well but in case of DirAddressBookParty (DABP) it is not going through. I also tried running this class separately in a job and that successfully created record in DABP table. 

    Here are the screenshot you require:

    pastedimage1602579193786v1.png

    pastedimage1602580113955v1.png

  • nmaenpaa Profile Picture
    nmaenpaa 101,148 on at
    RE: Event Handler Does Not Add Record In Other Table

    Hi,

    thanks for this info. Could you also share with us all the learnings from your debugging? Thanks!

    And, could you share screenshots where we see the infolog from the second piece of code (displaying RecId of the created DirAddressBookparty) and a screenshot from Table browser, where we see latest records in that table, sorted by RecId descending. Thanks!

  • haroonattari Profile Picture
    haroonattari 2,296 on at
    RE: Event Handler Does Not Add Record In Other Table

    I tried these two methods one by one but neither worked.

    public void assignAddressBookV1()
    {
        container   addressBooks = ["ALL"];
    
        DirAddressBookParty::createPartyRelationsByName(custTable.Party, addressBooks);
    
        info(strFmt('assignAddressBook() - All addressbook assigned to %1-%2-%3-%4', 
            custTable.AccountNum, 
            custTable.Party, 
            custTable.dataAreaId, 
            custTable.RecId));
    }

    public void assignAddressBookV2()
    {
        DirAddressBook          dirAddressBook;
        DirAddressBookParty     dirAddressBookParty;
        DirAddressBookName      addressBookName;
    
        addressBookName = 'ALL';
    
        dirAddressBook = DirAddressBook::find(addressBookName);
    
        if (custTable.RecId && dirAddressBook.RecId)
        {
            try
            {
                ttsBegin;
    
                dirAddressBookParty.clear();
                dirAddressBookParty.initValue();
                dirAddressBookParty.AddressBook = dirAddressBook.RecId;
                dirAddressBookParty.Party       = custTable.Party;
                dirAddressBookParty.insert();
    
                ttsCommit;
                info(strFmt('assignAddressBookNew() - All addressbook assigned to %1-%2-%3-%4-%5', 
                    custTable.AccountNum, 
                    custTable.Party, 
                    custTable.dataAreaId, 
                    custTable.RecId, 
                    dirAddressBookParty.RecId));
            }
            catch
            {
    
            }
        }
    }

    In fact, the infolog in 2nd method is also showing RecId for DirAddressBookParty table. 

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,835 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,526 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans