How to Import Records from Excel Using X++ Code in D365FO

  How to Import Records from Excel Using X++ Code in D365FO   Include the below namespace in X++ code it will requires to import Excel. Using System.IO; Using OfficeOpenXml; Using OfficeOpenXml.ExcelPackage; Using OfficeOpenXml.ExcelRange;   In addition to the basic reference, include (Directory and DirectoryUpgrade Packages) X++ Code: Using System.IO; Using OfficeOpenXml; Using OfficeOpenXml.ExcelPackage; Using OfficeOpenXml.ExcelRange; class ReadExcel {   public static void main(Args _args)    {     Int id;     Str Name;      System.IO.Stream stream;       ExcelSpreadsheetName sheet;              FileUploadBuild fileUpload,fileUploadBuild;       DialogGroup dialogUploadGroup;       FormBuildControl formBuildControl;   ...

Create sales order using x++


Create sales order using x++:


Following is the code to create and post confirmation , packing slip and invoice sales order using x++ programming language:

class CreatePostSalesOrder
{
    /// <summary>
    /// Create sales order and posting sales order
    /// </summary>

    public static void main(Args _args)
    {       
        SalesTable salesTable;
        SalesLine salesLine;
        InventTable inventTable;
        InventDim inventDim;
        CustTable custTable;
        CustAccount custAccount;
        NumberSeq numberSeq;
        SalesId salesID;
        str warehouse;
salesFormLetter      salesFormLetter ;
        try
        {
            ttsbegin;
            salesTable.clear();

            //Number sequence automatically gets the next number as per system's configuration
            //SalesId is mandatory to create sales order
            numberSeq = NumberSeq::newGetNum(SalesParameters::numRefSalesId());
            numberSeq.used();
            salesTable.SalesId = numberSeq.num();
            salesID = salesTable.SalesId;
            salesTable.initValue();
           
            //Set the warehouse as per your scenario
            //It will be used to set the Site & Warehouse and create inventory dimension id
            warehouse = "11";

            //Give CustAccount as per your scenario
            //It is mandatory to create sales order
            custAccount = "US-001";
            if(CustTable::find(custAccount))
            {
                salesTable.CustAccount = custAccount;
            }
            else
            {
                info(strFmt("Customer account %1 doesn't exist.", custAccount));
            }

            //Initializing the sales order from customer
            salesTable.initFromCustTable();
            if(InventLocation::find(warehouse).InventLocationId != "")
            {
                salesTable.InventSiteId = InventLocation::find(warehouse).InventSiteId;
                salesTable.InventLocationId = inventlocation::find(warehouse).InventLocationId;
            }
            salesTable.insert();
            try
            {
                inventTable.clear();
                inventDim.clear();
                salesLine.clear();

                //Give ItemId as per your scenario
                //It is mandatory to create sales line
                select * from inventTable
                    where inventTable.itemId == "A0001";
                   
                salesLine.clear();
                salesLine.SalesId = salesID;
                salesLine.ItemId = inventTable.ItemId;
                salesLine.itemIdChanged();

                //Initializing the sales line from inventory
                salesLine.initFromInventTable(InventTable::find(salesLine.ItemId));

                //Setting and creating inventory dimensions
                //I have given the warehouse in
                if(Inventlocation::find(warehouse).InventLocationId != "")
                {
                    inventdim.InventSiteId = InventLocation::find(warehouse).InventSiteId;
                    inventdim.InventLocationId = Inventlocation::find(warehouse).InventLocationId;
                }
                salesLine.InventDimId = InventdIm::findOrCreate(inventDim).inventDimId;

                salesLine.createLine(NoYes::Yes, // Validate
                NoYes::Yes, // initFromSalesTable
                NoYes::No, // initFromInventTable
                NoYes::Yes, // calcInventQty
                NoYes::Yes, // searchMarkup
                NoYes::Yes); //

                //Set the values as per your scenario
                salesLine.SalesPrice = 250;
                salesLine.SalesQty = 3;
                salesLine.LineDisc = 10;
                salesLine.LineAmount= salesLine.calcLineAmount();
                salesLine.update();
                ttscommit;
            }
            catch(Exception::Error)
            {
                ttsabort;
            }
                //
                // Confirmation
                salesFormLetter = SalesFormLetter::construct(DocumentStatus::Confirmation);
                salesFormLetter.update(SalesTable::find(salesID));
                info(strFmt("Sales %1 created and confirmed.", salesTable.SalesId));

              //Packing slip
               salesFormLetter=SalesFormLetter::construct(DocumentStatus::PackingSlip);
               salesFormLetter.update(SalesTable::find(salesID));                                salesFormLetter.update(salesTable,systemDateGet(),SalesUpdate::All,AccountOrder::Non e, NoYes::No,NoYes::Yes);
                info("Sales Order Status is Delivered");

                //Invoicing the sales order
                 SalesFormLetter formLetterObj;
                 formLetterObj = SalesFormLetter::construct(DocumentStatus::Invoice);
                 formLetterObj.update(SalesTable::find(salesID));
            
               info(strFmt("Sales order created with Sales ID: %1",salesID));
        }
        catch(Exception::Error)
        {
            ttsabort;
        }
    }
}

Comments

Popular posts from this blog

D365 FO: REFRESH CALLER FORM DATA SOURCE FROM A CLASS X++

Passing Args from one Form to another Form in dynamics AX

CREATE AND POSTING PURCHASE ORDER THROUGH X++ DYNAMICS AX