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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Finance forum / Creating customers wit...
Finance forum

Creating customers with X++

(0) ShareShare
ReportReport
Posted on by

Hello guys, 

I'm following a tutorial here. I have made some minor changes and still have the code like this. 

public static void JobCreateCustomer (Args _args)
    {
        CustTable                    custTable;
        NumberSeq                    numberSeq;
        Name                         name ='Financial Derivative';

        DirParty                        dirParty;
        DirPartyPostalAddressView       dirPartyPostalAddressView;
        DirPartyContactInfoView         dirPartyContactInfo;
        ;

        /* Marks the beginning of a transaction.
        Necessary to utilize the method numRefCustAccount() */
        ttsBegin;
        custTable.initValue();

        try
        {
            //CustTable
            //numberSeq               = NumberSeq::newGetNum(CustParameters::numRefCustAccount());
            //custTable.AccountNum    = numberSeq.num();
            custTable.CustGroup     ='030';
            custTable.Currency      ='NGN';
            custTable.PaymTermId    ='10AE';
            custTable.PaymMode      ='CHEQUE-03';

            custTable.insert(DirPartyType::Organization, name);

            //DirParty

            /* Creates a new instance of the DirParty class from an address book entity
            that is represented by the custTable parameter. */
            dirParty = DirParty::constructFromCommon(custTable);

            dirPartyPostalAddressView.LocationName      ='Headquarters';
            dirPartyPostalAddressView.City              ='Victoria Island';
            dirPartyPostalAddressView.Street            ='Idejo';
            dirPartyPostalAddressView.StreetNumber      ='15';
            dirPartyPostalAddressView.CountryRegionId   ='NG';
            dirPartyPostalAddressView.State             ='LOS';

            // Fill address
            dirParty.createOrUpdatePostalAddress(dirPartyPostalAddressView);

            dirPartyContactInfo.LocationName    ='Financial Derivative Phone Contact';
            dirPartyContactInfo.Locator         ='551291165837';
            dirPartyContactInfo.Type            = LogisticsElectronicAddressMethodType::Phone;
            dirPartyContactInfo.IsPrimary       = NoYes::Yes;

            // Fill Contacts
            dirParty.createOrUpdateContactInfo(dirPartyContactInfo);

            // Marks the end of transaction.
            ttsCommit;
        }
        catch(Exception::Error)
        {
            ttsAbort;
            throw Exception::Error;
        }
        info("Customer has been created");
    }


I don't know why I get the following error even though the record was not previously created in the table. Here is the error:

Microsoft.Dynamics.Ax.Xpp.DuplicateKeyException was unhandled by user code
Message: An exception of type 'Microsoft.Dynamics.Ax.Xpp.DuplicateKeyException' occurred in aoskernel.dll but was not handled in user code
Additional information: Cannot create a record in Customers (CustTable). Name: Financial Derivative.
The record already exists.

What could I be doing wrongly?

I have the same question (0)
  • Sohaib Cheema Profile Picture
    49,668 Super User 2026 Season 2 on at

    Have you observed the line of code where it was extracting the next number sequence for CustAccount and you have commended that..?

    On top of that i cannot see if anywhere you are assigning the value for custTable.AccountNum

  • Suggested answer
    Rustem Galiamov Profile Picture
    8,072 on at

    Hi Samseen!

    Try to remove // from this lines:

    //numberSeq               = NumberSeq::newGetNum(CustParameters::numRefCustAccount());
    //custTable.AccountNum    = numberSeq.num();

    This lines generate AccountNum value.

  • Community Member Profile Picture
    on at

    @Sohaib Cheema and @Rustem Galiamov here's the code I ran:

    public static void JobCreateCustomer (Args _args)
    
       {
    
           CustTable                    custTable;
    
           NumberSeq                    numberSeq;
    
           Name                         name ='Chicken Republic';
    
           DirParty                        dirParty;
    
           DirPartyPostalAddressView       dirPartyPostalAddressView;
    
           DirPartyContactInfoView         dirPartyContactInfo;
    
           ;
    
           /* Marks the beginning of a transaction.
    
           Necessary to utilize the method numRefCustAccount() */
    
           ttsBegin;
    
           custTable.initValue();
    
           try
    
           {
    
               //CustTable
    
               numberSeq               = NumberSeq::newGetNum(CustParameters::numRefCustAccount());
    
               custTable.AccountNum    = numberSeq.num();
    
               custTable.CustGroup     ='030';
    
               custTable.Currency      ='NGN';
    
               custTable.PaymTermId    ='10AE';
    
               custTable.PaymMode      ='CHEQUE-03';
    
               custTable.insert(DirPartyType::Organization, name);
    
               //DirParty
    
               /* Creates a new instance of the DirParty class from an address book entity
    
               //that is represented by the custTable parameter. */
    
               dirParty = DirParty::constructFromCommon(custTable);
    
               dirPartyPostalAddressView.LocationName      ='Headquarters';
    
               dirPartyPostalAddressView.City              ='Victoria Island';
    
               dirPartyPostalAddressView.Street            ='Adeola Hopewell';
    
               dirPartyPostalAddressView.StreetNumber      ='14';
    
               dirPartyPostalAddressView.CountryRegionId   ='NG';
    
               dirPartyPostalAddressView.State             ='LOS';
    
               // Fill address
    
               dirParty.createOrUpdatePostalAddress(dirPartyPostalAddressView);
    
               dirPartyContactInfo.LocationName    ='Chicken Republic Phone Contact';
    
               dirPartyContactInfo.Locator         ='551291165449';
    
               dirPartyContactInfo.Type            = LogisticsElectronicAddressMethodType::Phone;
    
               dirPartyContactInfo.IsPrimary       = NoYes::Yes;
    
               // Fill Contacts
    
               dirParty.createOrUpdateContactInfo(dirPartyContactInfo);
    
               // Marks the end of transaction.
    
               ttsCommit;
    
           }
    
           catch(Exception::Error)
    
           {
    
               ttsAbort;
    
               throw Exception::Error;
    
           }
    
           info("Customer has been created");
    
       }

    And I still get the error:

    Microsoft.Dynamics.Ax.Xpp.DuplicateKeyException was unhandled by user code
    Message: An exception of type 'Microsoft.Dynamics.Ax.Xpp.DuplicateKeyException' occurred in aoskernel.dll but was not handled in user code
    Additional information: Cannot create a record in Global address book (DirPartyTable). Party ID: 000028006, Chicken Republic.
    The record already exists.
  • Suggested answer
    Rustem Galiamov Profile Picture
    8,072 on at

    So, now the error of DirPartyTable table. Try to debug the code and check how the Party ID generated.

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

    You can also try to create the customer using Customers entity. This way you don't have to worry about Party or Location, instead the entity handles it for you.

    In this article (chapter Testing data entities) you can find an example on how to insert data in x++ using a data entity: docs.microsoft.com/.../build-consuming-data-entities

    In your case you would have to use CustCustomersV3 entity (or V2, depending on your D365FO version), of course.

  • Suggested answer
    Sheikh Sohail Profile Picture
    6,125 on at

    Have you debug that number sequence is generating properly? I think Number sequence is not generating correctly that's why you are facing error

    numberSeq               = NumberSeq::newGetNum(CustParameters::numRefCustAccount());
    custTable.AccountNum    = numberSeq.num();
  • Rustem Galiamov Profile Picture
    8,072 on at

    Hi Samson Akanbi!

    Did you solve the issue?

  • Community Member Profile Picture
    on at

    Not yet @Rustem, I've figured out that the problem is with me now setting dirParty.parmPartyNumber. I think I need to use a number sequence. Or is there a way I can bypass this?

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

    You can bypass it by using the data entity approach. Did you already look into it? I think it's the simplest solution for your requirement.

  • Community Member Profile Picture
    on at

    @Nikolaos... Hmm... Just scrolling down to the end and saw it there. Seems it might work. I'll give it a try and revert. Thanks in advance.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Finance

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans