Sunday, July 30, 2017

X++ Code To Adding Days and Months With From Date

Date Addition Jobs


    info(strFmt("Today is: %1 ",today()));
   
    //Add days
    info(strFmt("Add 5 days to today: %1 ",today()+5));
    info(strFmt("Add 10 days to today: %1 ",systemdateget()+10));

    //Add months
    info(strFmt("Add 12 months to today: %1 ",dateMthFwd(systemdateget(),12)));  //Adds 1 year
    info(strFmt("Add 24 months to today: %1 ",dateMthFwd(systemdateget(),24)));  // add 2 year

Thursday, July 27, 2017

X++ code for Import Fixed Asset

static void FIxedAssetImport(Args _args)
{
    SysExcelApplication    Application;
    SysExcelWorkbooks    workbooks;
    SysExcelWorkbook     workbook;
    SysExcelWorkSheets   worksheets;
    SysExcelWorksheet    worksheet;
    SysExcelCells            cells;
    COMVariantType       type;
    Name                         Descriptions;
    //Filename                 fileName;
    AssetTable                 AssetTable;
    Dialog                        dialog;
    DialogField                dialogField;
    FilenameOpen            filename;
    int row =1 ;
    str AssetCategory,AssetId,AssetSubCategory,SerialNum,Status,AcquisitionYear,AssetLife,DepriciableDays;
    real NetBook,originalAssetCost,Yearly,AccumulatedDepriciation;
    int i=0 ;
    //  COMVariantType type;
    date DateOfIstallations,DateOfPurchase;
    FileIOPermission        permission;
    #File

    str COMVariant2Str(COMVariant _cv, int _decimals = 0, int _characters = 0, int _separator1 = 0, int _separator2 = 0)
    {
    switch (_cv.variantType())
    {
    case (COMVariantType::VT_BSTR):
    return _cv.bStr();
    case (COMVariantType::VT_R4):
    return num2str(_cv.float(),_characters,_decimals,_separator1,_separator2);
    case (COMVariantType::VT_R8):
    return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);
    case (COMVariantType::VT_DECIMAL):
    return num2str(_cv.decimal(),_characters,_decimals,_separator1,_separator2);
    case (COMVariantType::VT_DATE):
    return date2str(_cv.date(),123,2,1,2,1,4);
    case (COMVariantType::VT_EMPTY):
    return "";
    default:
    throw error(strfmt("@SYS26908", _cv.variantType()));
    }
    return "";
    }

    //importing from excel
    dialog = new Dialog("FixedAsset Import");
    dialogField = dialog.addField(ExtendedTypeStr("FilenameOpen"),"Source file");

    if (dialog.run())
    {
    filename = dialogField.value();
    permission = new fileIOpermission(filename,"r");
    permission.assert();
    Application = SysExcelApplication::construct();
    workbooks   = Application.workbooks();

    try
    {
        workbooks.open(fileName);
    }
    catch
    {
        throw error("File cannot be opened");
    }

    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.cells();
    do
    {
        i++;
        row++;
        AcquisitionYear        = COMVariant2str(cells.item(row,2).value());
        AssetId                = cells.item(row,3).value().bStr();
        AssetCategory          = cells.item(row,4).value().bStr();
        AssetSubCategory       = cells.item(row,5).value().bStr();
        Descriptions           = cells.item(row,6).value().bStr();
        SerialNum              = cells.item(row,7).value().bStr();
        DateOfIstallations     = cells.item(row,8).value().date();
        DateOfPurchase         = cells.item(row,9).value().date();
        originalAssetCost      = cells.item(row,10).value().double();
        AssetLife              = COMVariant2str(cells.item(row,11).value());
        Yearly                 = cells.item(row,12).value().double();
        NetBook                = cells.item(row,13).value().double();
        Status                 = cells.item(row,14).value().bStr();
        DepriciableDays        = COMVariant2str(cells.item(row,16).value());
        AccumulatedDepriciation= cells.item(row,17).value().double();


        AssetTable.AcquisitionYear                 = AcquisitionYear  ;
        AssetTable.AssetId                               = AssetId  ;
        AssetTable.AssetCategory                    = AssetCategory  ;
        AssetTable.AssetSubCategory              = AssetSubCategory  ;
        AssetTable.Name                                  = Descriptions  ;
        AssetTable.SerialNum                          = SerialNum  ;
        AssetTable.DateOfInstallations            = DateOfIstallations  ;
        AssetTable.DateOfPurchase                 = DateOfPurchase  ;
        AssetTable.OriginalAssetCost              = originalAssetCost  ;
        AssetTable.AssetLife                            = AssetLife  ;
        AssetTable.YearlyDepriciation             = Yearly  ;
        AssetTable.NetBookValueason             = NetBook  ;
        AssetTable.Status                                  = Status  ;
        AssetTable.DepriciableDays                 = DepriciableDays;
        AssetTable.AccumulatedDepriciations = AccumulatedDepriciation;
        AssetTable.insert();
         //info(strFmt("DATAiMPORTED"));

         type = cells.item(row+1,2).value().variantType();
    }

    while(type != COMVariantType::VT_EMPTY);
    Application.quit();
    //info(strFmt("%1",i));

}
}

Wednesday, July 19, 2017

X++ Code To Update Customer Details In Ax 2012

X++ Code to Update R3 data for Existing Customers..


static void CustomerUpdate(Args _args)
{


CustTable                                 custTable;
DirParty                                   dirParty;
DirPartyRecId                          partyRecId;
LogisticsPostalAddress            address;
DirPartyPostalAddressView    addressView;
DirPartyTable                          dirPartyTable;
LogisticsPostalAddress           logisticsPostalAddress;
LogisticsElectronicAddress    logisticsElectronicAddress;
DirPartyPostalAddressView   dirPartyPostalAddressView;
DirPartyContactInfoView       dirPartyContactInfo;


Name                     name                  = "Test";
AccountNum         accountNum      = "DE-001";
CustGroupId          custGroupId      = "20";
CurrencyCode       currencyCode    = "INR";
PaymTermId          paymTermId     = "Net45";
DlvModeId            dlvModeId        = "30";
str                          country              = "India";
str                          State                  = "TamilNadu";
str                          city                    = "madurai";
str                          ZipCode            = "625007";
str                         street                  = "K.pudur";
str                         phone                 = "8220308543";
str                         Fax                     = "123";
str                         Email                 = "j.jackrider@gmail.com";
 
ttsBegin;
select forUpdate custTable where custtable.AccountNum == accountNum ;

if(custTable)
{

    if(name)
    {
    partyRecId = DirPartyTable::createNew( DirPartyType::Organization, name).RecId;
    custTable.Party      = partyRecId;
    }

    if(custGroupId)
    {
    custTable.CustGroup = custGroupId;
    }

    if(currencyCode)
    {
    custTable.Currency = currencyCode;
    }

    if(paymTermId)
    {
    custTable.PaymTermId = paymTermId;
    }

    if(dlvModeId)
    {
    custTable.DlvMode = dlvModeId;
    }

    if(country)
    {
        partyRecId = DirPartyTable::createNew( DirPartyType::Organization, name).RecId;
        custTable.Party      = partyRecId;

        DirParty = DirParty::constructFromPartyRecId(partyRecId );
        address.clear();

        address.CountryRegionId = strLRTrim(country);
        dirParty = DirParty::constructFromCommon(custTable);

        if( address.CountryRegionId != "")
        {
        address.State   = State;
        address.City    = City;
        address.ZipCode = ZipCode;
        address.Street  = Street;
        addressView.LocationName = "Address";
        addressView.IsPrimary = NoYes::Yes;
        addressView.Party = partyRecId;
        addressview.initFromPostalAddress(address);

        DirParty = DirParty::constructFromPartyRecId(addressView.Party );
        DirParty.createOrUpdatePostalAddress(addressView);
        }

    }

    if(phone)
    {
    dirPartyContactInfo.LocationName = "Phone Number";
    dirPartyContactInfo.Locator      = strLRTrim(phone);
    dirPartyContactInfo.Type         = LogisticsElectronicAddressMethodType::Phone;
    dirPartyContactInfo.Party        = partyRecId;
    dirPartyContactInfo.IsPrimary    = NoYes::Yes;
    dirParty.createOrUpdateContactInfo(dirPartyContactInfo);
    }

    if(Fax)
    {
    dirPartyContactInfo.LocationName = "Fax Number";
    dirPartyContactInfo.Locator      = strLRTrim(Fax);
    dirPartyContactInfo.Type         = LogisticsElectronicAddressMethodType::Fax;
    dirPartyContactInfo.Party        = partyRecId;
    dirPartyContactInfo.IsPrimary    = NoYes::Yes;
    dirParty.createOrUpdateContactInfo(dirPartyContactInfo);
    }

    if(Email)
    {
    dirPartyContactInfo.LocationName = "Email";
    dirPartyContactInfo.Locator      = strLRTrim(Email);
    dirPartyContactInfo.Type         = LogisticsElectronicAddressMethodType::Email;
    dirPartyContactInfo.Party        = partyRecId;
    dirPartyContactInfo.IsPrimary    = NoYes::Yes;
    dirParty.createOrUpdateContactInfo(dirPartyContactInfo);
    }

    custTable.update();
    info("Customer Updated");
}

ttsCommit;


}