Wednesday, August 31, 2022

Module has invalid reference to module ... Resolving a missing reference during Package deployment


While working on a project and moved to another Development environment , I imported an Model (D365) that caused me to have the following error when I tried to build the models.

The reason is when one model is dependent on a second model and the second model is dependent on the first model.It means one model contains a reference of second model and second model also contains the reference of first model.


Resolution

1. Go to the AOSService folder located on the main drive.
2. In the AOSService folder open the PackagesLocalDirectory folder.
3. Now in this folder, you’ll find all the models. Locate your model and open the folder.
4. In this folder locate to Descriptor Folder.
5. Copy this file to any location of your choice and open it in any text editor.
6. Remove the tag that contains the model which is not present and is the source of the error.
7. Finally copy this file back to the original location and replace the file in the destination folder.
8. Refresh models in Visual Studio and you’re good to go.


 





Sunday, August 28, 2022

How to get calling menu name in extensions Dynamics 365 for finance and operations

 Today is small tip, Many times, we have to took decision based on the name of menu  on which form is called. By getting menu name we can use same form for different purposes.  We can write logic based on current form called from which menu item.

Following is the code snippet helps you to achieve this.

[FormEventHandler(formStr(LogisticsContactInfoGrid), FormEventType::Initialized)]
    public static void LogisticsContactInfoGrid_OnInitialized(xFormRun sender, FormEventArgs e)
    {
        FormRun formRun = sender;
        
        if (formRun.args().menuItemName() == "CustomerLogisticsContactInfoGrid")
        {
            FormDataSource      LogisticsElectronicAddress_ds =formRun.dataSource("LogisticsElectronicAddress");
            LogisticsElectronicAddress_ds.InsertIfEmpty(true);
            LogisticsElectronicAddress_ds.object(fieldNum(LogisticsElectronicAddress, Locator)).mandatory(true);
        }
}
Ref

Sunday, August 7, 2022

D365 FO - Code to Import data from excel file

 D365 FO - Import data from excel file 

Create Table ImportProductTable with 2 Fields 



Create xlsx excel file with 2 fields ItemID , ItemName


using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.ExcelPackage;
using OfficeOpenXml.ExcelRange;
public static class EcoResProductListPage_Extension
{
 [FormControlEventHandler(formControlStr(EcoResProductListPage, UploadProductButtonControl), FormControlEventType::Clicked)]
    public static void UploadProductButtonControl_OnClicked(FormControl sender, FormControlEventArgs e)
    {

        System.IO.Stream                     stream;
        ExcelSpreadsheetName            sheeet;
        FileUploadBuild                       fileUpload;
        DialogGroup                             dlgUploadGroup;
        FileUploadBuild                       fileUploadBuild;
        FormBuildControl                    formBuildControl;
        ImportProductTable _ImportProductTable;

        COMVariantType                     type;
        Dialog                      dialog =    new Dialog('Import Products');
        int rowCount2=10;
  
        dlgUploadGroup          = dialog.addGroup('@SYS54759');
        formBuildControl        = dialog.formBuildDesign().control(dlgUploadGroup.name());
        fileUploadBuild           = formBuildControl.addControlEx(classstr(FileUpload), 'Upload');
        fileUploadBuild.style(FileUploadStyle::MinimalWithFilename);
        fileUploadBuild.fileTypesAccepted('.xlsx');
         
        str COMVariant2Str(COMVariant _cv)
        {
            switch (_cv.variantType())

            {
                case COMVariantType::VT_BSTR:
                    return _cv.bStr();
                case COMVariantType::VT_EMPTY:
                    return'';
                default:
                    throw error(strfmt('@SYS26908', _cv.variantType()));
            }

        }

        if (dialog.run() && dialog.closedOk())
        {
        
            FileUpload fileUploadControl     = dialog.formRun().control(dialog.formRun().controlId('Upload'));
            FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();
            if (fileUploadResult != null && fileUploadResult.getUploadStatus())

            {
                stream = fileUploadResult.openResult();
                using (ExcelPackage Package = new ExcelPackage(stream))

                {
          
                    int                         rowCount, i,columncount,j;
                    RecId  _RecId;
                    Package.Load(stream);
                    ExcelWorksheet   worksheet   = package.get_Workbook().get_Worksheets().get_Item(1);
                    OfficeOpenXml.ExcelRange    range       = worksheet.Cells;
                    rowCount           = (worksheet.Dimension.End.Row) - (worksheet.Dimension.Start.Row)  + 1;
                    columncount      = (worksheet.Dimension.End.Column);

                    ttsbegin;
                    delete_from _ImportProductTable;
                    ttscommit;

                    for (i = 2; i<= rowCount; i++)

                    {
                        _ImportProductTable.ItemId  =   range.get_Item(i, 1).value ;
                        _ImportProductTable.ItemName  =   range.get_Item(i, 2).value ;
                        _ImportProductTable.insert();
                    }
                    info(strFmt("%1 rows inserted.",rowCount));
                }
            }

            else
            {
                error('Error here');
            }

        }
        
    }
}




Wednesday, August 3, 2022

D365FO - Customer Address book relation for channel Database

 Scenario:

Most of our D365FO Fellow are working with Retail where we sync customer with our channel database, and sometime we need to update the customer from POS as well, Sometime channel DB throw exception when while updating the customer information if AddressBook of the customer  customer is not properly set.

Its happens when we create customer using AX client or import them via DMF and didn't set the address book value.


So no need to worry using below code & query you can update the customers address book in bulk.

Image:




Code:

public void AddrssBookRelation()
    {
        DirAddressBookParty       dirAddressBookParty;
        DirAddressBook                   dirAddressBook;
        CustTable                              custTable;


        select RecId,Party from custTable
            where custTable.AccountNum==this.CustomerAccount;

        if(custTable)
        {
            select RecId from dirAddressBook
            where dirAddressBook.Name==this.CountryCode;

            if(dirAddressBook)
            {
                select RecId from dirAddressBookParty
                    where dirAddressBookParty.Party==custTable.Party && dirAddressBookParty.AddressBook==dirAddressBook.RecId;

                if(dirAddressBookParty.RecId==0)
                {
                    dirAddressBookParty.clear();
                    dirAddressBookParty.Party=custTable.Party;
                    dirAddressBookParty.AddressBook=dirAddressBook.RecId;
                    dirAddressBookParty.insert();
                }

            }
        }

    }



SQL Query:

INSERT INTO DirAddressBookParty (PARTY,ADDRESSBOOK,PARTITION)
select DISTINCT DirPartyLocation.PARTY,DirAddressBook.RECID,5637144576 from LogisticsPostalAddress
join DirPartyLocation on DirPartyLocation.LOCATION=LogisticsPostalAddress.LOCATION
Join LOGISTICSLOCATION on LOGISTICSLOCATION.RECID=LogisticsPostalAddress.LOCATION
join DirAddressBook on DirAddressBook.NAME=LogisticsPostalAddress.COUNTRYREGIONID
where DirPartyLocation.ISPRIMARY=1 and party in (select party from CUSTTABLE)
and LOGISTICSLOCATION.ISPOSTALADDRESS=1 and DirPartyLocation.ISPOSTALADDRESS=1 and DirPartyLocation.POSTALADDRESSROLES='Business'
and not exists (select 1 from DirAddressBookParty where DirAddressBookParty.PARTY=DirPartyLocation.PARTY and DirAddressBookParty.ADDRESSBOOK=DirAddressBook.RECID)
 and LogisticsPostalAddress.VALIDTO >=GETDate()


Reference