Friday, December 30, 2016

Restriction of updation when modifying field value in Header(Purchase/Sales)

//Restriction of updation when modifying field value in Header(Purchase/Sales)'

Requirement: Need to restrict Warehouse updation from Header to line level

SalesPurchTableToLineUpdatePrompt - Dialog(method)
intiailize variable in method
update = false;
code:
Standard: line num -51
if (salesPurchTableToLineField.parmTableToLineUpdate() == TradeTable2LineUpdate::Prompt)
{
                refreshable = true;
Replace with :
if (salesPurchTableToLineField.parmTableToLineUpdate() == TradeTable2LineUpdate::Prompt
&& salesPurchTableToLineField.parmFieldId() != fieldNum("Field id "))
{
update = true;
refreshable = true;
}

Code:
Standard: Line Num(97)
return dialog;
Replace with:
if(update)
{
return dialog;
}
else
{
return null;   
}
//Related class list
//Common for purch and Sales
SalesPurchTableToLineUpdatePrompt
SalesPurchTableToLineField
SalesPurchTableToLineParametersForm
//Purchase
PurchTable2LineField
PurchTable2LineParametersForm
PurchTable2LineUpdate
PurchTable2LineUpdatePrompt
//Sales
SalesTable2LineField
SalesTable2LineParametersForm
SalesTable2LineUpdate
SalesTable2LineUpdatePrompt

Validate Email Id in ax 2012

static boolean validateEmail(str _email)
{
    str emailAddressFilter = @"^[^@]+@[^@]+\.[^@]+$";
    System.Text.RegularExpressions.Regex regEx;
    System.Text.RegularExpressions.Match regMatch;
    str email;
    List emailAddresses;
    ListEnumerator enum;
    boolean retVal;
    regEx = new System.Text.RegularExpressions.Regex(emailAddressFilter);
    emailAddresses = SysEmailDistributor::splitEmail(_email);
    enum = emailAddresses.getEnumerator();
    while(enum.moveNext())
    {
        email = enum.current();
        if (email)
        {
            regMatch = regEx.Match(email);
            retVal = regMatch.get_Success();
        }
        else
            retVal = false;
        if(!retVal)
            break;
    }
    return retVal;
}

Rounding in SSRS Expression based on count of Decimal place ax 2012

=iif(Fields!RoundingDecimals.value=0,FormatNumber(Fields!Fieldname.Value,3),FormatNumber(Fields!FieldName.value,fields!RoundingDecimals.Value))

Save and get last value object in ax 2012

xSysLastvalue::getLast(this);

Get Trial Balance report by Query Ax 2012

static void CheckGLEntry(Args _args)
{
    GeneralJournalEntry                 generalJournalEntry;
    GeneralJournalAccountEntry          generalJournalAccountEntry;
    FiscalCalendarPeriod                fiscalCalendarPeriod;
    TransDate             _startDate = mkDate(31,12,2015);
    TransDate             _endDate = mkDate(31,12,2016);
    Fcc_BIData          bidata;
    DimensionAttributeValueCombination  dimValueCombi;
    MainAccount     mainAccount;
    MainAccountCategory mainAccountCategory;
    DimensionAttributeLevelValueAllView dimAllviewchk;
    //Find Dimension value - function
    Description255 getDimensionValue(RecId  _recId,Name _DimensionName)
    {
        DimensionAttributeLevelValueAllView dimAllview;
        DimensionAttribute          dimAttr;
        ;
        dimAllview.clear();
        select firstOnly dimAllview
            where dimAllview.ValueCombinationRecId == _recId
                join dimAttr
                    where dimAttr.RecId == dimAllview.DimensionAttribute
                            && dimAttr.Name == _DimensionName;
        return dimAllview.DisplayValue;
    }
    //Get Voucher type
    Fcc_BIType  GetBIType(Voucher   _Voucher,Name   _LE)
    {
        Fcc_BIType  Fcc_BIType;
        ;
        changeCompany(_LE)
        {
            if(_Voucher && CustTrans::findByVoucher(_Voucher))
            {
                return Fcc_BIType::Cust;
            }
            else if(_Voucher && VendTrans::findByVoucher(_Voucher))
            {
                return Fcc_BIType::Vend;
            }
            else
            {
                return Fcc_BIType::Ledger;
            }
        }
    }
    //Get Account num Cust/Vend
    AccountNum  GetAccountNumBI(Voucher   _Voucher,Name _LE)
    {
        ;
        changeCompany(_LE)
        {
            if(_Voucher && CustTrans::findByVoucher(_Voucher))
            {
                return CustTrans::findByVoucher(_Voucher).AccountNum;
            }
            else if(_Voucher && VendTrans::findByVoucher(_Voucher))
            {
                return VendTrans::findByVoucher(_Voucher).AccountNum;
            }
            else
            {
                return "";
            }
        }
    }
    //End
    ;
    delete_from bidata;
    while select generalJournalAccountEntry
            //where generalJournalAccountEntry.LedgerAccount like  mainAccount.MainAccountId //For other than expenses and sales Account category
        join generalJournalEntry  order by AccountingDate Asc
            where generalJournalEntry.RecId == generalJournalAccountEntry.GeneralJournalEntry
                    && generalJournalEntry.AccountingDate >= _startDate
                        && generalJournalEntry.AccountingDate <= _endDate
                            && generalJournalEntry.SubledgerVoucherDataAreaId != ""
                            //&& generalJournalEntry.Ledger == Ledger::current()
        join dimAllviewchk
            where dimAllviewchk.ValueCombinationRecId == generalJournalAccountEntry.LedgerDimension
                    && dimAllviewchk.DimensionAttribute == DimensionAttribute::findByName("MainAccount",false).RecId
            join mainAccount
                where  mainAccount.MainAccountId == dimAllviewchk.DisplayValue
                            && mainAccount.MainAccountId != "9999999"
                    join mainAccountCategory
                        where mainAccountCategory.AccountCategoryRef == mainAccount.AccountCategoryRef
                                && (mainAccountCategory.AccountCategory == "INCOME" ||
                                    mainAccountCategory.AccountCategory == "MTCOGS" ||
                                    mainAccountCategory.AccountCategory == "SRCOGS" ||
                                    mainAccountCategory.AccountCategory == "EMPEXP" ||
                                    mainAccountCategory.AccountCategory == "RENTEX" ||
                                    mainAccountCategory.AccountCategory == "OTHEXP" ||
                                    mainAccountCategory.AccountCategory == "DEPEXP"
                                    )
    {
        bidata.clear();
        select dimValueCombi
            where dimValueCombi.DisplayValue == generalJournalAccountEntry.LedgerAccount;
        bidata.MainAccountNum = getDimensionValue(dimValueCombi.RecId,"MainAccount");
        bidata.MainAccountName = MainAccount::findByMainAccountId(bidata.MainAccountNum).Name;
        bidata.AccountCatgeory = MainAccountCategory::findAccountCategoryRef(MainAccount::findByMainAccountId(bidata.MainAccountNum).AccountCategoryRef).AccountCategory;
        bidata.Opening = 0;//num2str(_amount,8,3,0,0,)
        if(generalJournalAccountEntry.IsCredit == NoYes::Yes)
        {
            bidata.Credit = generalJournalAccountEntry.AccountingCurrencyAmount *-1; //For summary AmountCredit
            bidata.Debit = 0; //For summary Amount Debit
        }
        else
        {
            bidata.Credit = 0;
            bidata.Debit = generalJournalAccountEntry.AccountingCurrencyAmount; //For summary Amount Debit
        }
        bidata.Closing = bidata.Debit - bidata.Credit; //For summary endingbalacne
        bidata.TransDate = generalJournalEntry.AccountingDate;
        bidata.month = mthOfYr(generalJournalEntry.AccountingDate);
        bidata.Year = year(generalJournalEntry.AccountingDate);
        bidata.BU = getDimensionValue(dimValueCombi.RecId,"BusinessUnit");
        bidata.Dept = getDimensionValue(dimValueCombi.RecId,"Department");
        bidata.Project = getDimensionValue(dimValueCombi.RecId,"Project");
        bidata.Worker = getDimensionValue(dimValueCombi.RecId,"Worker");
        bidata.LegalEntity = getDimensionValue(dimValueCombi.RecId,"LegalEntity");
        bidata.DateAreaVoucher  = curext();
        bidata.SubledgerVoucher = generalJournalEntry.SubledgerVoucher;
        bidata.Fcc_BIType = GetBIType(generalJournalEntry.SubledgerVoucher,curext());
        bidata.AccountNum = GetAccountNumBI(generalJournalEntry.SubledgerVoucher,curext());
        bidata.insert();
    }
    info("Data got refreshed");
}

Get Lastest Purchase price of an item ax 2012

private Price GetLatestPurchPrice(ItemId _item)
{
    InventItemPrice invItemprice;
    ;
    select firstOnly invItemprice order by RecId desc
        where invItemprice.ItemId == _item
            && invItemprice.PriceType == CostingVersionPriceType::Purch
                && invItemprice.CostingType == InventItemCostingType::Last;
   
    return  invItemprice.Price;
}

SQL Store Procedure sample

ALTER PROCEDURE Proc_Test
       -- Add the parameters for the stored procedure here
       @name varchar(50),
       @trns_dt datetime
AS
BEGIN
       -- SET NOCOUNT ON added to prevent extra result sets from
       -- interfering with SELECT statements.
       SET NOCOUNT ON;
    -- Insert statements for procedure here
       SELECT * from AdventureWorks2012.Sales.SalesOrderDetail D
       print @name
       --where convert(varchar(10),D.ModifiedDate,120)=convert(varchar(10),@trns_dt,120)
END
GO
--Select getdate()
--Select convert(varchar(10),GETDATE(),105)

Connecting SQL Store procedure from VS with parameter Ax 2012

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace SangeetDemo
{
    public partial class Form1 : Form
    {
        SqlConnection con;
        SqlCommand command;
        string sqlcon = @"Server=Sangeeth-PC\DEV01;Database=AdventureWorks2012;User ID=sa; Password=1234";
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            con = new SqlConnection();
            con.ConnectionString = sqlcon;
            try
            {
                con.Open();
                command = new SqlCommand();
                command.Connection = con;
                command.CommandText = "Proc_Test";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@name", "abc");
                command.Parameters.AddWithValue("@trns_dt", DateTime.UtcNow);
                SqlDataReader reader = command.ExecuteReader();
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        MessageBox.Show(reader["SalesOrderID"].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}

Deploy SSRS Report in X++ ax 2012

static void DeploySSRSreportsAx2012(Args _args)
{
    #AOT   
    TreeNodeIterator        reportIterator = TreeNode::findNode(#SSRSReportsPath).AOTiterator();
    SRSReportManager        srsReportManager = new SRSReportManager();
    SSRSReportConceptNode   ssrsReportConceptNode;
   
    if (!reportIterator)
        return;
   
    ssrsReportConceptNode = reportIterator.next();
    while (ssrsReportConceptNode)
    {
        try
        {
            srsReportManager.deploymentStart();
            srsReportManager.deployReport(ssrsReportConceptNode);
            srsReportManager.deploymentEnd();
        }
        catch
        {
            exceptionTextFallThrough();
        }
       
        ssrsReportConceptNode = reportIterator.next();
    }
}

Upload data from Excel in D365FO X++

 Action Menu Item: SAN_UploadExcelData Object type: Class Object: <Controller class name> Label: <> Class: Controller class clas...