X++ code to create CustVendExternalItem from csv import in Microsoft Dynmics AX 2012 R3

X++ code to create CustVendExternalItem from csv import in Microsoft Dynmics AX 2012 R3


 static void  RB_ExternalItemImport(Args _args)
{
    #File
    IO                                          iO;
    FilenameOpen                       filename = "c:\\ExternalItem.csv";//file name here
    CustVendExternalItem          CustVendExternalItem;
    InventTable                           inventTable;
    VendTable                             vendTable;
    VendAccount                         supplierId;
    ItemId                                    itemId;
    ExternalItemId                      ExternalItemId;
    Container                              con;
    boolean                                 first = true;
    FileIoPermission                  perm;
    int                                          created,error;
    InventDim                            inventDim = InventDim::find('AllBlank');


    iO = new CommaTextIo(filename,#IO_Read);

    if (! iO || iO.status() != IO_Status::Ok)
    {
        throw error("@SYS19358");
    }


    while (iO.status() == IO_Status::Ok)
    {
        con = iO.read();//  read file
        if (con)
        {
            if (first)  //skip header
            {
                first = false;
            }
            else
            {
                supplierId          =   conPeek(con,1);
                ItemId                =   conPeek(con,2);
                ExternalItemId  =   conPeek(con,3);

                inventTable = inventTable::find(ItemId);
                VendTable   = VendTable::find(supplierId);

                if (inventTable &&  VendTable)
                {
                    CustVendExternalItem.clear();
                    CustVendExternalItem.initValue();
                    CustVendExternalItem.ABCCategory = ABC::None;
                    CustVendExternalItem.CustVendRelation = supplierId;
                    CustVendExternalItem.ExternalItemId = ExternalItemId;
                    CustVendExternalItem.ItemId       = inventTable.ItemId;
                    CustVendExternalItem.InventDimId  = inventDim.inventdimid;
                    CustVendExternalItem.ModuleType = ModuleInventPurchSalesVendCustGroup::Vend;
                    CustVendExternalItem.insert();
                    created++;
                }
                else
                {
                    error++;
                    info(strFmt("%1,%2 not created",  ItemId, supplierId));
                }
            }
        }
    }

    info("Mission Accomplished");
}

X++ code to move the file in batch process - Microsoft Dynamics AX2012 R3

X++ code to move the file in batch process - Microsoft Dynamics AX2012 R3


public server static void moveFile(FileName  fileName,FileName  destinationPath)
{

    FileName            newFileName;
    Filename            fPath;
    Filename            fName;
    Filename            fType;
    System.Exception    exception;

 
    #File

    try
    {

        [fPath, fName, fType] = fileNameSplit(fileName);

        newFileName = strFmt("%1%2%3", destinationPath, fName, fType);

 
        if (!System.IO.File::Exists(newFileName))
        {
            System.IO.File::Move(fileName, newFileName);
        }
        else
        {
            System.IO.File::Delete(fileName);
        }

    }
    catch (Exception::CLRError)
    {

        exception = ClrInterop::getLastException();
        if (exception != null)
        {
            exception = exception.get_InnerException();
            if (exception != null)
            {
                error(exception.ToString());
            }
        }
    }
    catch (Exception::Error)
    {
        info(strFmt("Errored",exception.ToString()));
        retry;
    }

}

X++ Code To Generate Ledger Dimension Based On The Company Accounting Structure.

X++ Code To Generate Ledger Dimension Based On The Company Accounting Structure.



public static RefRecId  generateLedgerDimension(MainAccountNum  _mainAccountId,
DimensionValue _costCentre,DimensionValue _division,DimensionValue _location,
DimensionValue _supplier, DimensionValue _products,DimensionValue _customer,
DimensionValue _interCompany)
{
    container                                                conData;
    int                                                           hierarchyCount;
    int                                                           hierarchyIdx;
    LedgerRecId                                          ledgerRecId;
    MainAccount                                         mainAccount;
    RefRecId                                                recordvalue;
    DimensionAttribute                               dimensionAttribute;
    DimensionAttributeValue                      dimensionAttributeValue;
    DimensionSetSegmentName                  DimensionSet;
    DimensionStorage                                  dimStorage;
    DimensionAttributeValueContract         ValueContract;
    LedgerAccountContract                          LedgerAccountContract;
    DimensionAttributeValueCombination  dimensionAttributeValueCombination;
    List                                                          valueContracts;

    #define.MainAccount('MainAccount')
    #define.Division('Division')
    #define.CostCentre('CostCentre')
    #define.Location('Location')
    #define.Supplier('Supplier')
    #define.Products('Products')
    #define.Intercompany('Intercompany')
    #define.Customer('Customer')
    #define.Language('en-gb')


    LedgerAccountContract   = new LedgerAccountContract();
    valueContracts          = new List(Types::Class);

    conData =[_mainAccountId];


    mainAccount     =   MainAccount::findByMainAccountId(_mainAccountId);
    recordvalue     =       DimensionHierarchy::getAccountStructure(mainAccount.RecId,Ledger::current());
    hierarchyCount  =   DimensionHierarchy::getLevelCount(recordvalue);
    DimensionSet    =   DimensionHierarchyLevel::getDimensionHierarchyLevelNames(recordvalue);

    if (recordvalue)
    {
        for(hierarchyIdx = 1;hierarchyIdx<=hierarchyCount;hierarchyIdx++)
        {

            if(hierarchyIdx == 1)
            continue;

            dimensionAttribute = DimensionAttribute::findByLocalizedName(DimensionSet[hierarchyIdx],false,#Language);

            if (dimensionAttribute.Name == #Division)
            {
                conData += [_division];
            }
            if (dimensionAttribute.Name == #CostCentre)
            {
                conData += [_costCentre];
            }
            if (dimensionAttribute.Name == #Supplier)
            {
                conData += [_supplier];
            }
            if (dimensionAttribute.Name == #Location)
            {
                conData += [_location];
            }
            if (dimensionAttribute.Name == #Products)
            {
                conData += [_products];
            }
            if (dimensionAttribute.Name == #Intercompany)
            {
                conData += [_interCompany];
            }
            if (dimensionAttribute.Name == #Customer)
            {
                conData += [_customer];
            }

            if(dimensionAttribute)
            {
                dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,conPeek(conData,hierarchyIdx));

                if(dimensionAttributeValue)
                {

                    ValueContract = new DimensionAttributeValueContract();
                    ValueContract.parmName(dimensionAttribute.Name) ;
                    ValueContract.parmValue(dimensionAttributeValue.CachedDisplayValue);
                    valueContracts.addEnd(ValueContract);
                }
            }

        }

        LedgerAccountContract.parmMainAccount(_mainAccountId);
        LedgerAccountContract.parmValues(valueContracts);

        dimStorage = DimensionServiceProvider::buildDimensionStorageForLedgerAccount(LedgerAccountContract);
        dimensionAttributeValueCombination = DimensionAttributeValueCombination::find(dimStorage.save());
        ledgerRecId = dimensionAttributeValueCombination.RecId;
    }
    else
    {
        warning (strfmt("Ledger dimension Issue for main account %1", _mainAccountId));
    }

    return  ledgerRecId;

}

Install and Configure Zpl Printer on D365 F&O

  Setup Zpl Printer On D365 F&O Posted on Recently, I have had an opportunity review the possibilities to print license plates within D3...