Wednesday, April 4, 2018

Get next Month in x++

 

How to get next month date in x++

 static void mthName(Args _arg)
{
      Transdate s; ;

      s = nextMth(today());
   print "next month date is " + s;
 
   pause;
} 
 
Happy Daxing! 
 

Monday, April 2, 2018

How to get month Number in x++

How to get month of year in x++

 
static void mthYear(Args _arg)
{
    int s;
    ;
     s = mthOfYr(today());  
    print "Month of year is " + s;
 
    pause;
}

Get month name in X++

How to get name of month in x++

 
static void mthName(Args _arg)
{
    str s;
    ;
 
    s = mthName(8);  
    info(strfmt("Month name is %1" , s));
 
}
 
Happy Daxing! 

Tuesday, June 27, 2017

Run code based on configuration key in dynamics AX 2012

Sometimes you need to run code based on your configuration keys 

Code:


  if  (isConfigurationKeyEnabled (configurationkeynum(keyname)))
   {
                     // Code goes here..
   }
   else
   {
                // do something else
    }



Happing daxing

Facing error while deploying from Visual studio

You get the error when trying to deploy a report either by using VS2013 reporting tool or through AOT


The "GenerateRdlTask" task failed unexpectedly.
System.IO.FileLoadException: Loading this assembly would produce a different grant set from other instances. (Exception from HRESULT: 0x80131401)

Resolution:
1. Go to Control Panel\System and Security\System
2. Open Advanced system settings and open Environment Variables
3. Create a New Variable
4.Variable name: COMPLUS_LoaderOptimization ; Variable value: 1
5. Click OK
6. Logout from the server and logon again.
7. Open the report using Visual Studio 2013 and deploy the AX report.
8. The report can also be deployed from AOT

Deploy SSRS Report through Power Shell

Here is the command to deploy SSRS Report Through Power Shell

Deploy Single Report

 To deploy a specific report, enter the name of the report. For example, to deploy the CustTransList report, enter  the following command:
Command : Publish-AXReport -ReportName CustTransList

Deploy Multiple Report

To deploy two or more specific reports, enter the names of the reports. For example, to deploy the CustTransList and CustTransOpenPerDate reports, enter the following command:
Command : Publish-AXReport -ReportName CustTransList, CustTransOpenPerDate

Deploy All Report

To deploy all reports, enter the following command:
Command  : Publish-AXReport –ReportName 

Create financial dimension through x++

Here is the code to create/find financial dimension in x++

static RecId findCreateDimension(str _project, str _department)
{
    RecId                               ret;

    DimensionAttributeValueSetStorage   dimStorage;
    DimensionAttributeValue             dimensionAttributeValue;
    DimensionAttribute                  dimensionAttribute;
    ;
    if (_project || _department)
    {
        dimStorage              = new DimensionAttributeValueSetStorage();
        if (_project)
        {
            dimensionAttribute      = AxdDimensionUtil::validateFinancialDimension("project");
            dimensionAttributeValue = AxdDimensionUtil::validateFinancialDimensionValue(dimensionAttribute, _project);
            dimStorage.addItem(dimensionAttributeValue);
        }
        if (_department)
        {
            dimensionAttribute      = AxdDimensionUtil::validateFinancialDimension("Department");
            dimensionAttributeValue =    AxdDimensionUtil::validateFinancialDimensionValue(dimensionAttribute,    _department);
            dimStorage.addItem(dimensionAttributeValue);
        }
        ret = dimStorage.save();
    }
    return ret;
}

Deploy Report through x++ code

Here is the code to deploy SSRS report in Microsoft Dynamics AX.

static void deployReport(Args _args)
{
    SRSReportManager srsRptMgr = new SRSReportManager();
    SSRSReportConceptNode node = new SSRSReportConceptNode();

    node = TreeNode::findNode(@"\\SSRS Reports\Reports\SalesInvoice");

    srsRptMgr.deploymentStart();
    srsRptMgr.deployReport(node);
    srsRptMgr.deploymentEnd();
}

Refresh,Reread,Research,ExecuteQuery in dynamics AX

Refresh,Reread,Research,ExecuteQuery in dynamics AX
1. Refresh()
refreshes the user view with whats stored in the caches. This does not touch the DB.
Use this after any form change has been made through code.
2. ReRead()
 fetches only the current record from database and does not re read the complete datasource.
Use this when you need to update only the current record after modifying any value.
3 . ReSearch() 
  will execute the same query again and fetch the results from the database.
Use this if you need to get the current most data from database.
4 . ExecuteQuery()
 will run the query again just like research does but it will also take any query changes into account.
Use this is you have modified the query on run-time and need the updated results according to the new query.

Difference among Research() and Research(true) in AX 2012

Difference among Research() and Research(true) in AX 2012

Research
Calling research() will return the existing form query against the database, therefore updating the list with new/removed records as well as updating all existing rows. This will honor any existing filters and sorting on the form, that were set by the user.
Research(true)
The research method starting with AX 2009 accepts an optional boolean argument_retainPosition. If you call research(true), the cursor position in the grid will be preserved after the data has been refreshed. This is an extremely useful addition, which solves most of the problems with cursor positioning (findRecord method is the alternative, but this method is very slow).

Unit of Work Framework in Microsoft Dynamics AX 2012

Unit of Work Framework in Microsoft Dynamics AX 2012

Unit of Work Framework is use to commit number of records in a single Transaction. The main reason for introducing unit of work is that it is impossible to insert the lines table record before its header table because there is a relation between the header table and the lines table. The “Recid” of the header table is comes as a foreign key in the child table.

Let’s took an example as you can see in the Microsoft dynamics ax there are two tables i.e. MTQUnitOfWorkTable and MTQUnitOfWorkLine. These two tables are related with each other “MTQUnitOfWorkTable” is the parent table and “MTQUnitOfWorkLine” is the child table.

As you can see in the below image about these two tables and there relations.




CreateNAvigationPropertyMethods“should be set to Yes         
Then create Class name as “MTQDemoClass” and then add Method, and write following piece of code.


Now Create New Job, call “demo” method.

Result: You can see that the records are crated in the child table having the id of the parent table

References:

https://msdn.microsoft.com/en-us/library/gg846338.aspx

Create Purchase Requisition through x++

Here is the code for creating purchase requisition through x++

public static void createPurchaseReq (Args _args)
{
    PurchReqTable   purchReqTable;
    PurchReqLine    purchReqLine.;
 
    purchReqTable.clear();
    purchReqTable.initValue();

    purchReqTable.PurchReqId    =                                NumberSeq::newGetNum(PurchReqTable::numRefPurchReqId()).num();
    purchReqTable.PurchReqName  = 'Requisition';

    purchReqTable.insert();

    purchReqLine.clear();
    purchReqLine.initValue();

    purchReqLine.InventDimId = 'Dim-0001';
    purchReqLine.LineNum     = 1;

    purchReqLine.initFromPurchReqTable(purchReqTable);

    purchReqLine.ItemId              = 'Test-001';
    purchReqLine.BuyingLegalEntity   = CompanyInfo::find().RecId;
    purchReqLine.InventDimIdDataArea = curext();
    purchReqLine.PurchQty            = 2;

    purchReqLine.modifiedField(fieldNum(purchReqLine,ItemId));
 
    purchReqLine.insert();
 
}

Error 1 Loading this assembly would produce a different grant set from other instances. (Exception from HRESULT: 0x80131401)

You get the error when trying to deploy a report either by using VS2013 reporting tool or through AOT


The "GenerateRdlTask" task failed unexpectedly.
System.IO.FileLoadException: Loading this assembly would produce a different grant set from other instances. (Exception from HRESULT: 0x80131401)

Resolution:
1. Go to Control Panel\System and Security\System
2. Open Advanced system settings and open Environment Variables
3. Create a New Variable
4.Variable name: COMPLUS_LoaderOptimization ; Variable value: 1
5. Click OK
6. Logout from the server and logon again.
7. Open the report using Visual Studio 2013 and deploy the AX report.
8. The report can also be deployed from AOT


Create Sales Order through code

I found few good sample code for creating sales order in a book and I was able to compile a code for posting sales order invoice. I thought I should share on my blog. It might be useful for some one.
Code for creating Sales order
SalesTableType and SalesLinetype. Insert() should be called for creating the sales order. Example is as follows. I am currently looking for code that can contains example code of creating invoice from scratch. I will send you the code in an hour.
static void createSalesTable(CustAccount _custAccount)
{
SalesTable salesTable;
NumberSeq NumberSeq;
;
NumberSeq =
NumberSeq::newGetNumFromCode(SalesParameters::numRefSalesId
().numberSequence);
salesTable.SalesId = NumberSeq.num();
salesTable.initValue();
salesTable.CustAccount = _custAccount;
salesTable.initFromCustTable();
salesTable.insert();
}
Example: Create a Sales Line
static void createSalesLine(SalesId _salesId, ItemId _itemId)
{
SalesLine salesLine;
;
salesLine.clear();
salesLine.SalesId = _salesId;
salesLine.ItemId = _itemId;
salesLine.createLine(NoYes::Yes, // Validate
NoYes::Yes, // initFromSalesTable
NoYes::Yes, // initFromInventTable
NoYes::Yes, // calcInventQty
NoYes::Yes, // searchMarkup
NoYes::Yes); // searchPrice
}
Code for posting Sales order Invoice
static void createSalesOrder(Args _args)
{
SalesFormLetter formLetterObj;
formLetterObj = SalesFormLetter::construct(DocumentStatus::Invoice);
formLetterObj.update(SalesTable::find(“SO-101248”));
}

Create/Post Sales order Invoice through x++

Hi,

Today i am going to show you how to create/post sales order invoice in x++

static void createPostSalesOrderInvoice(Args _args)
{
        SalesFormLetter salesFormLetter;
        salesFormLetter=SalesFormLetter::construct(DocumentStatus::Invoice);
       salesFormLetter.update(SalesTable::find('SO-ASC0001'));
       info(strFmt("%1 Sales Order Posted and Final Status is Invoiced",salesTable.SalesId));
}

now run this job

Confirm Sales Order through code

Hi,

Today i am going to show you how to conform sales order through code

static void confirmSalesOrder (Args _args)
{
        SalesTable salesTable;
        SalesFormLetter salesFormLetter ;
     
        SalesTable = SalesTable::find('SO-ASC0001');

        salesFormLetter = SalesFormLetter::construct(DocumentStatus::Confirmation);

        salesFormLetter.update(salesTable);

}

now run this code,

Post/Create Sales order Packing slip x++

Hi Folks,

Today i am going to show you code how to create and post Packing slip through x++;

static void createPostSalesPackingSlip(Args _args)
{
    SalesTable salesTable;
    SalesFormLetter salesFormLetter;

    salesTable = SalesTable::find('SO-ASC001');
      //Posting Sales Order 
    salesFormLetter=SalesFormLetter::construct(DocumentStatus::PackingSlip);
    salesFormLetter.update(SalesTable::find(sid));
       salesFormLetter.update(salesTable,systemDateGet(),SalesUpdate::All,AccountOrder::Non e, NoYes::No,NoYes::Yes);
    info("Sales Order Status is Delivered");
}

run this job.

Create Sales Order in x++

Hi,
Today i am going to show you code how to create sales order in x++.
static void CreateSalesOrder(Args _args)
{
//declaring variables
SalesTable salesTable;
SalesLine salesLine;
NumberSeq numberSeq;
ttsBegin;
//creating sales order header
//getting sales order id from number sequence
numberSeq = NumberSeq::newGetNum(SalesParameters::numRefSalesId());
numberSeq.used();
salesTable.SalesId = numberSeq.num();
salesTable.initValue();
salesTable.CustAccount = ‘Demo-001′;
salesTable.initFromCustTable();
//validate
if (!salesTable.validateWrite()) {
throw Exception::Error;
}
salesTable.insert();
//creating sales order line
salesLine.SalesId = salesTable.SalesId;
salesLine.ItemId = ‘DM0012′;
salesLine.SalesQty = 2;
salesLine.LinePercent = 1;
salesLine.createLine(true, // Validate
true, // initFromSalesTable
true, // initFromInventTable
true, // calcInventQty
true, // searchMarkup
true  // searchPrice
);
ttsCommit;

//displaying sales order id
info(salesTable.SalesId);
}
And THEN run this job

X++ code to create Purchase Order Packing slip

X++ Code to Post the Purchase Order Packing Slip.

Following Job post the Packing Slip by using PurchFormLetter class.

static void CreatePOPackingSlip(Args _args)
{

    PurchFormLetter purchFormLetter;
    PurchTable          PurchTable;
    ttsbegin;
   PurchTable = PurchTable::find('PO-Demo');
   purchFormLetter = purchFormLetter::construct(DocumentStatus::PackingSlip);
   purchFormLetter.update(purchtable, // Purchase record Buffer
                           "Inv_"+purchTable.PurchId, // Invoice Number
                             systemdateget()); // Transaction date
ttscommit;
if (PurchTable::find(purchTable.PurchId).DocumentStatus ==           DocumentStatus::PackingSlip)
{
    info(strfmt("Posted Packing Slip for purchase order %1",purchTable.PurchId));
}
}

Now run the CreatePOPackingSlip job.

X++ code to create Purchase Order Invoice

X++ Code to Post the Purchase Order Invoice.

 

Following Job post the invoice by using PurchFormLetter class.

static void CreatePOInvoice(Args _args)
{

    PurchFormLetter purchFormLetter;
    PurchTable          PurchTable;
    ttsbegin;
   PurchTable = PurchTable::find('PO-Demo');
   purchFormLetter = purchFormLetter::construct(DocumentStatus::Invoice);
   purchFormLetter.update(purchtable, // Purchase record Buffer
                           "Inv_"+purchTable.PurchId, // Invoice Number
                             systemdateget()); // Transaction date
ttscommit;
if (PurchTable::find(purchTable.PurchId).DocumentStatus ==           DocumentStatus::Invoice)
{
    info(strfmt("Posted invoiced journal for purchase order %1",purchTable.PurchId));
}
}

Now run the CreatePOInvoice job.