Create a customer/vendor using x++

Create a customer/vendor using x++

In this article, We will see how to create a customer and vendor record using X/

Below is the code snippet for creating a customer record in X++.

public class CustomerCreateDemo
{
private void new()
{
}
public static CustomerCreateDemo construct()
{
    return new CustomerCreateDemo();
}

public void createCustomer()
{
    CustTable                    custTable;
    NumberSeq                    numberSeq;
    Name                         name ='Tesla';

    DirParty                        dirParty;
    DirPartyPostalAddressView       dirPartyPostalAddressView;
    DirPartyContactInfoView         dirPartyContactInfo;
    ;

    ttsBegin;
    custTable.initValue();

    try
    {
        //CustTable
        numberSeq              = NumberSeq::newGetNum(CustParameters::numRefCustAccount());
        custTable.AccountNum    = numberSeq.num();
        custTable.CustGroup     ='10';
        custTable.Currency      ='EUR';
        custTable.PaymTermId    ='USD10';
        custTable.PaymMode      ='CK';
        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      ='Tesla, USA ';
        dirPartyPostalAddressView.City              ='USA';
        dirPartyPostalAddressView.Street            ='Test';
        dirPartyPostalAddressView.StreetNumber      ='101';
        dirPartyPostalAddressView.CountryRegionId   ='USA';
        dirPartyPostalAddressView.State             ='NY';
        dirPartyPostalAddressView.IsPrimary             = NoYes::Yes;
        // Fill address
        dirParty.createOrUpdatePostalAddress(dirPartyPostalAddressView);


        dirPartyContactInfo.LocationName    ='Email Address';
        dirPartyContactInfo.Locator         ='test@gmail.com';
        dirPartyContactInfo.Type            = LogisticsElectronicAddressMethodType::Email;
        dirPartyContactInfo.IsPrimary       = NoYes::Yes;

        // Fill Contacts
        dirParty.createOrUpdateContactInfo(dirPartyContactInfo);


        dirPartyContactInfo.LocationName    ='Mobile Number';
        dirPartyContactInfo.Locator         ='1234567889';
        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;
    }
}
}

Below is the code snippet for creating a vendor record in X++.

Public class VendorCreateDemo
{
    private void new()
    {
    }

    public static VendorCreateDemo construct()
    {
        return new VendorCreateDemo();
    }

    public void createVendor()
    {
        VendTable VendTable;
		NumberSeq numberSeq;
		Name name = 'Tesla';
	 
		DirParty dirParty;
		DirPartyPostalAddressView dirPartyPostalAddressView;
		DirPartyContactInfoView dirPartyContactInfo;
	 
		DirPartyTable dirPartyTable;
        ;
        
        ttsBegin;
        vendTable.initValue();

        try
        {
            //vendTable
            numberSeq               = NumberSeq::newGetNum(VendParameters::numRefVendAccount());
            vendTable.AccountNum    = numberSeq.num();
            vendTable.VendGroup     ='10';
            vendTable.Currency      ='EUR';
            vendTable.PaymTermId    ='USD10';
            vendTable.PaymMode      ='CK';
            vendTable.insert(DirPartyType::Organization, name);

            //DirParty

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

            dirPartyPostalAddressView.LocationName      ='Tesla, USA ';
            dirPartyPostalAddressView.City              ='USA';
            dirPartyPostalAddressView.Street            ='Test';
            dirPartyPostalAddressView.StreetNumber      ='101';
            dirPartyPostalAddressView.CountryRegionId   ='USA';
            dirPartyPostalAddressView.State             ='NY';
            dirPartyPostalAddressView.IsPrimary             = NoYes::Yes;
            // Fill address
            dirParty.createOrUpdatePostalAddress(dirPartyPostalAddressView);


            dirPartyContactInfo.LocationName    ='Email Address';
            dirPartyContactInfo.Locator         ='test@gmail.com';
            dirPartyContactInfo.Type            = LogisticsElectronicAddressMethodType::Email;
            dirPartyContactInfo.IsPrimary       = NoYes::Yes;

            // Fill Contacts
            dirParty.createOrUpdateContactInfo(dirPartyContactInfo);


            dirPartyContactInfo.LocationName    ='Mobile Number';
            dirPartyContactInfo.Locator         ='1234567889';
            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;
        }
    }
}

If you like this article, feel free to share it with others who might find it helpful! If you have any questions, feel free to reach out to me.

Parag Chapre

Parag Chapre is a Microsoft MVP in the fields of Dynamics 365 Finance & Operations, Human Resources, and Power Platform, recognized for his outstanding contributions to the Microsoft Dynamics community.

With over 15 years of hands-on expertise in various Microsoft Dynamics 365 areas, Parag has designed and delivered complex, innovative solutions for customers across industries and geographies. He has also provided leadership and technical guidance to project teams, managed offshore and onshore resources, and worked closely with Microsoft Product teams. Parag is passionate about sharing his knowledge and insights through his personal website, blog posts, articles, and community events. He is a member of the Microsoft Biz Apps Community Advisory Board, a Dynamics 365 Human Resource Community star, a Dynamics 365 Community contributor, and a Dynamics 365 Community Spotlight honoree.

Leave a Reply

Your email address will not be published. Required fields are marked *