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

Notifications

Announcements

No record found.

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,346

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 

I have the same question (0)
  • nmaenpaa Profile Picture
    101,162 Moderator on at

    Let's first separate your problem in two parts.

    1) When customer is inserted, run your event handler code

    2) Assign address books to customer in code

    It seems to me that part 1 is working.

    But part 2 is not working as expected.

    Did you debug your code? Do you have some value in CustTable.Party in line 18 of the code that you shared?

    Do you have address book in your system, that have name "ALL"?

  • haroonattari Profile Picture
    2,346 on at

    Hi Nikolas

    I have an address book with id 'ALL' in system and also the infolog in assignAddressBook method is showing value for custTable.Party field.

  • nmaenpaa Profile Picture
    101,162 Moderator on at

    Did you debug the DirAddressBookParty::createPartyRelationsByName method? If not, could you do that and let us know where it goes wrong?

  • haroonattari Profile Picture
    2,346 on at

    I used this method earlier in a job to assign 'ALL' address book to all customer and it went well. I also tried creating record directly in DirAddressbookParty using following code but that did not work either.

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

    Surprisingly the infolog in this method is also showing RecId for DirAddressbookParty table but when I am querying the same from SQL it is not available there. 

  • Suggested answer
    nmaenpaa Profile Picture
    101,162 Moderator on at

    If you see RecId for DirAddressbookParty table in the infolog, can you find the same record with Table browser?

    SQL Server data is not always 100% corresponding with the application because of AOS caching and other things.

    Anyway, you should develop code that works reliably for assigning an address book for the customer. Once it's working, call it from your event handler code and you're done.

  • haroonattari Profile Picture
    2,346 on at

    The code I have shared is from the job which I developed to assign the addressbook to whole cutomer list. Also I tried looking for the record from table browser but no success.

  • nmaenpaa Profile Picture
    101,162 Moderator on at

    The code is not from a job, it's from a method.

    Is your code running in a transaction? If yes, perhaps something in the process fails after the infolog is printed, rolling back the transaction. This would explain why you don't see the value in table browser or SQL.

    But, if you run the code in a job, the record should definetely be visible in table browser after the code is executed.

  • haroonattari Profile Picture
    2,346 on at

    I mean the code I have copied from the job which was running successfully. Also I tried running code in CustTableEventHandler class in a job and it also assigned the address book successfully.

    static void HAR_Update_AddressBook(Args _args)
    {
        CustTable                   custTable;
        CustTableEventHandler       custTableEventHandler;
    
        custTable = CustTable::find(CustNum);
    
        custTableEventHandler = new CustTableEventHandler(custTable);
    
        custTableEventHandler.assignAddressBookNew();
        //custTableEventHandler.createCustomTableRecord();
    }

    To further debug, I tried creating a record in custom table and the record was successfully created.

  • nmaenpaa Profile Picture
    101,162 Moderator on at

    So everything works now? 

  • haroonattari Profile Picture
    2,346 on at

    Not 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

Responsible AI policies

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

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 420 Most Valuable Professional

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 241 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans