Monday, May 17, 2021

Send Email from D365FO using X++

Send Email from D365FO using X++.


    /// <summary>

    /// SenMail alert

    /// </summary>

    /// <param name = "_emailTo">To email id</param>

    /// <param name = "_Subject">email subject line</param>

    /// <param name = "_body">email body text</param>

    public void SendEmail(Description _emailTo, Description _Subject, ErErrorDescription _body)

    {

        SysMailerSMTP   mailer = new SysMailerSMTP();

        SysMailerMessageBuilder builder = new SysMailerMessageBuilder();

        SysEmailParameters parameters = SysEmailParameters::find();

        ;

        //you will have to setup the below parameters in sysadmin module

        if (parameters.SMTPRelayServerName)

        {

            mailer.SMTPRelayServer(parameters.SMTPRelayServerName,

                parameters.SMTPPortNumber,

                parameters.SMTPUserName,

                SysEmailParameters::password(),

                parameters.SMTPUseNTLM);

        }

        else

        {

            warning("SERVER NOT FOUND");

        }



        builder.setFrom(SysEmailParameters::find().SMTPUserName);

        builder.addTo(_emailTo);

        builder.setSubject(_Subject);

        builder.setBody(_body);

        SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(builder.getMessage());


    }

 

Create sales Order through data entity in D365FO using X++

We can create a sales order by calling the insert method after entering values into the sales table buffer. Instead of that, we can directly use a standard entity.

This process I have used in integrations. I get the data by 3rd application and I insert that data into the new table. For this table, I created a new entity and I gave that entity name to the customer.  After I get the data I have written a batch job with the following code.

    // Sales order header.

SalesOrderHeaderV2Entity headerEntity; Parameters parameters = Parameters::find(); CustTable custTable = CustTable::find(parameters.CustAccount);testTable TestTable testTable; ; ttsbegin; headerEntity.clear(); headerEntity.initValue(); headerEntity.InvoiceCustomerAccountNumber = parameters.CustAccount; headerEntity.OrderingCustomerAccountNumber = parameters.CustAccount; headerEntity.SalesOrderOriginCode = parameters.SalesOriginId; headerEntity.CustomersOrderReference = OrderCode; headerEntity.SalesOrderName = custTable.name(); headerEntity.DeliveryAddressDescription = custTable.name(); headerEntity.DeliveryAddressName = custTable.name(); headerEntity.DeliveryAddressDescription = custTable.name(); headerEntity.DeliveryAddressCountryRegionId = custTable.countryRegionId(); headerEntity.DeliveryAddressCity = TownCity; headerEntity.DeliveryAddressZipCode = PostCode; headerEntity.DeliveryAddressStreet = strFmt('%1 %2 %3 %4 %5 %6 %7 Phone: %8' ,
                                                            testTable.RecipientTitle,
                                                            testTable.RecipientFirstName,
                                                            testTable.RecipientLastName,
                                                            testTable.Address1, testTable.Address2,
                                                            testTable.Suburb, testTable.Region,
                                                            testTable.Phone); headerEntity.DefaultShippingWarehouseId = parameters.DefaultWarehouseId; headerEntity.DefaultShippingSiteId = InventLocation::find(
                                                        parameters.DefaultWarehouseId).inventSiteId; headerEntity.DeliveryModeCode = parameters.DlvModeId; headerEntity.InclTax = NoYes::Yes; headerEntity.insert(); // Sales line. SalesOrderLineV2Entity SalesOrderLineV2Entity; InventDimCombination inventDimCombination; CustVendExternalItem custVendExternalItem; ExtCodeValueTable codeValueTable; InventTable inventtable; CustTable custTableLoc = custTable::find(_customerAccount); Parameters Parameters = Parameters::find(); ; if (testTable.VariantDetails) { codeValueTable = ExtCodeValueTable::findValue(tableNum(InventDimCombination),
                                                        ExtCodeSubModule::None, Parameters.ExtCodeId,
                                                        testTable.SupplierSKU); inventDimCombination = InventDimCombination::findRecId(codeValueTable.ExtCodeRelationRecId); inventtable = InventTable::find(inventDimCombination.ItemId); if (!inventDimCombination) { throw error("Product code not found"); } } else { custVendExternalItem = CustVendExternalItem::findExternalItemId(
                                                    ModuleInventPurchSalesVendCustGroup::Cust,
                                                    flyBuyparameters.CustAccount,
                                                    testTable.SupplierSKU); if (!custVendExternalItem) { throw error("Product code not found"); } inventtable = InventTable::find(custVendExternalItem.ItemId); select inventDimCombination     where inventDimCombination.ItemId == inventtable.ItemId; } SalesOrderLineV2Entity = null; SalesOrderLineV2Entity.SalesOrderNumber = _salesId; SalesOrderLineV2Entity.ItemNumber = inventtable.ItemId; SalesOrderLineV2Entity.OrderedSalesQuantity = testTable.Quantity; SalesOrderLineV2Entity.SalesPrice = testTable.UnitCostPrice; SalesOrderLineV2Entity.LineAmount = testTable.TotalCostPrice; //defaulting inventDimId starts SalesTable salesTableLoc = SalesTable::find(_salesId); if (salesTableLoc.InventLocationId) { SalesOrderLineV2Entity.ShippingSiteId = salesTableLoc.InventSiteId; SalesOrderLineV2Entity.ShippingWarehouseId = salesTableLoc.InventLocationId; } else { SalesOrderLineV2Entity.ShippingSiteId = custTableLoc.InventSiteId; SalesOrderLineV2Entity.ShippingWarehouseId = custTableLoc.InventLocation; } if (inventDimCombination) { SalesOrderLineV2Entity.retailVariantId = inventDimCombination.RetailVariantId; SalesOrderLineV2Entity.ProductColorId = inventDimCombination.inventDim().InventColorId; SalesOrderLineV2Entity.ProductSizeId = inventDimCombination.inventDim().InventSizeId; SalesOrderLineV2Entity.ProductStyleId = inventDimCombination.inventDim().InventStyleId; } if (SalesOrderLineV2Entity.validateWrite()) { SalesOrderLineV2Entity.insert(); }

Keep daxing!!

Sunday, May 16, 2021

Cancel a specific Sales Line, using X++ in d365Fo

Cancel a specific Sales Line, using X++ in d365Fo.

check below.


ItemID        ItemId=' ';

SalesId        SalesId = ' ';

boolean      check;

   select salesline

    where salesline.SalesId == SalesId

        && salesline.ItemId == ItemId;

        if(salesline)

        {

            ttsbegin;

           check =  SalesUpdateRemain::updateDeliveryRemainder(salesline, 0, 0); 

           ttscommit;

    }


Keep Daxing!!