Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Event Handler Does Not Add Record In Other Table

(0) ShareShare
ReportReport
Posted on by 2,325

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
    101,158 Moderator 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
    2,325 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
    101,158 Moderator 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
    2,325 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
    101,158 Moderator 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
    2,325 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
    101,158 Moderator 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
    2,325 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
    101,158 Moderator 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
    2,325 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. 

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,079 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,860 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans