Sunday, March 26, 2017

Get Modified object or attributed user list by layer wise for specify AX table and Method

//Get Modified object or attributed user list by layer wise for specify AX table and Method

//SQL query

-- start Query

SELECT p.[Name] AS Element
    ,c.[Name] AS ChildNode
    ,d.[MODIFIEDDATETIME]
    ,d.[MODIFIEDBY]
    ,d.[ModelId]
    ,m.[Name] AS modelName
  FROM [AX1QA_model].[dbo].[ModelElement] p
  JOIN [AX1QA_model].[dbo].[ModelElement] c ON c.ParentHandle = p.ElementHandle
  JOIN [AX1QA_model].[dbo].[ModelElementData] d ON d.ElementHandle = c.ElementHandle
   --and d.LayerId = 10 //Var
  JOIN [AX1QA_model].[dbo].[ModelManifest] m ON m.ModelId = d.ModelId
  WHERE p.AxId = 177 --Table id in AX table
  AND c.Name = 'costValue' --Sample - Method which you want to track
  ORDER BY d.LayerId

--End Code

Layer Id
-- Id Name
-- 0 SYS
-- 1 SYP
-- 2 GLS
-- 3 GLP
-- 4 FPK
-- 5 FPP
-- 6 SLN
-- 7 SLP
-- 8 ISV
-- 9 ISP
--10 VAR
--11 VAP
--12 CUS
--13 CUP
--14 USR
--15 USP

System Best Practice Check class

//System Best Practice Check class

Class -> SysBPCheck
AOT Class -> SysBPCheck*

//Macro
\Macros\SysBPCheck
\Macros\SysBPCheckIngore

Preventing duplicate RecId - Copying version control from Source to Target (Version Control Data) AX

//Preventing duplicate RecId - Copying version control from Source to Target (Version Control Data) - BCP

//Destination DB
SELECT MIN(RecId) AS DestMinRecId FROM SysVersionControlMorphXRevisionTable
//Source DB
SELECT MAX(RecId) AS SourceMaxRecId From SysVersionControlMorphXRevisionTable
valueToAdd = (maxSourceRecId – minTargetRecId) + 1
//Update SQL Query in Destination DB
UPDATE SysVersionControlMorphXRevisionTable
SET RecId = RecId + valueToAdd
UPDATE SystemSequences
SET NEXTVAL = (SELECT MAX(RecId) + 1 FROM SYSVERSIONCONTROLMORPHXREVISIONTABLE) WHERE tabid = 2543
//BCP - bulk Copy Data
//BCP syntax :  bcp <table_name> <direction> <file_name> <options>
options
[-n native type] [-c character type] [-S server name] [-U username]
[-P password] [-T trusted connection]
bcp dbo.testdb.SysVersionControlMorphxRevisionTable out C:\Data\testdata1.dat -T -d sourceDatabase -S sourceDBServer -N

bcp dbo.testdb2.SysVersionControlMorphxRevisionTable in C:\Data\testdata2.dat -T -d targetDatabase -S targetDBServer -N

Rapid start service problem Resolution

//Rapid start service problem

http://axhelper.com/?p=5009

Create Ledger Dimension from Existing ledger dimension with different mainaccount

//  Create Ledger Dimension from Existing ledger dimension with different mainaccount

//Another way to create or merging Ledger dimension

ledgerDimensionAccount buildDefaultAndLedgerDimension(LedgerDimensionAccount   _ledger,MainAccountNum _account = "")
    {
        DimensionAttributeValueSetStorage   dimensionAttributeValueSetStorage;
        DimensionAttributeValue             dimensionAttributeValue;
        DimensionDefault                    dimensionDefault;
        LedgerDimensionAccount              ledgerDimensionAccount;
        DimensionAttributeLevelValueAllView dimAttrValueallview;
        dimensionAttributeValueSetStorage = new DimensionAttributeValueSetStorage();
        while select dimAttrValueallview where dimAttrValueallview.ValueCombinationRecId == _ledger
        {
            if(DimensionAttribute::find(dimAttrValueallview.DimensionAttribute).Name != "MainAccount")
            {
                    dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(
                                    DimensionAttribute::find(dimAttrValueallview.DimensionAttribute),
                                    dimAttrValueallview.DisplayValue, false, true);
                    dimensionAttributeValueSetStorage.addItem(dimensionAttributeValue);
 
                dimensionDefault = dimensionAttributeValueSetStorage.save();
            }
        }
       
        ledgerDimensionAccount = DimensionDefaultingService::serviceCreateLedgerDimension(DimensionStorage::getDefaultAccountForMainAccountNum(_account),
                                                                                            dimensionDefault);
       
        return ledgerDimensionAccount;
    }
    ;

Get workflow reviewer Name - Display Method Ax 2012

public display name reviewerName() // get workflow reviewer Name
{
    PurchReqTable                   purchReqTable;
    PurchReqTableHistory            purchReqTableHistory;
    WorkflowTrackingStatusTable     workflowTrackingStatus;
    WorkflowTrackingTable           workflowTracking;
    utcdatetime                     validDate               = DateTimeUtil::utcNow();
    WorkflowStepName                stepName;
    TransDate                       stepSinceDate;
    PurchReqRequisitionStatus       status;
    TransDate                       statusSinceDate;
    RefRecId                        workflowTrackingStatusRecId;
    RefRecId                        workflowStepRecId;
    UserInfo                        userInfo;
    WorkflowTrackingWorkItem        workflowTrackingWorkItem;
    WorkflowWorkItemTable           workflowWorkItem;
    WorkflowWorkItemQueueAssignee   workItemQueueAssignee;
    PurchReqReviewStatus            reviewStatus;
    Name                            reviewer;
    select firstonly validTimeState(validDate)
        CreatedDateTime, RequisitionStatus from purchReqTable
            where purchReqTable.RecId == this.RecId
        outer join CreatedDateTime from purchReqTableHistory
            where purchReqTableHistory.PurchReqTable == this.RecId
        outer join RecId from workflowTrackingStatus
            order by workflowTrackingStatus.CreatedDateTime desc
            where workflowTrackingStatus.ContextTableId == this.TableId &&
                  workflowTrackingStatus.ContextRecId   == this.RecId;
    select firstonly RecId, Name, CreatedDateTime, TrackingContext, TrackingType from workflowTracking
        order by workflowTracking.CreatedDateTime desc
        where workflowTracking.WorkflowTrackingStatusTable == workflowTrackingStatus.RecId;
    if (workflowTracking.TrackingContext != WorkflowTrackingContext::LineItemElement ||
        workflowTracking.TrackingType    != WorkflowTrackingType::Creation)
    {
        select firstonly RecId, Name, CreatedDateTime, WorkflowStepTable
            from workflowTracking
            order by workflowTracking.CreatedDateTime desc
                where workflowTracking.WorkflowTrackingStatusTable == workflowTrackingStatus.RecId &&
                      workflowTracking.TrackingContext             == WorkflowTrackingContext::Step &&
                      workflowTracking.TrackingType                == WorkflowTrackingType::Creation;
    }
    workflowTrackingStatusRecId = workflowTrackingStatus.RecId;
    workflowStepRecId           = workflowTracking.WorkflowStepTable;

    select firstonly RecId, TrackingContext, TrackingType from workflowTracking
        order by CreatedDateTime desc
        where workflowTracking.WorkflowTrackingStatusTable == workflowTrackingStatusRecId;
    if (workflowTracking.TrackingContext != WorkflowTrackingContext::LineItemElement ||
        workflowTracking.TrackingType    != WorkflowTrackingType::Creation)
    {
        select firstonly RecId, Type, UserId, CreatedDateTime from workflowWorkItem
            order by workflowWorkItem.CreatedDateTime desc
                where workflowWorkItem.Status == WorkflowWorkItemStatus::Pending
            exists join workflowTrackingWorkItem
                where workflowTrackingWorkItem.WorkflowWorkItemTable == workflowWorkItem.RecId
            join workflowTracking
                where workflowTracking.RecId                       == workflowTrackingWorkItem.WorkflowTrackingTable &&
                      workflowTracking.TrackingContext             == WorkflowTrackingContext::WorkItem &&
                      workflowTracking.TrackingType                == WorkflowTrackingType::Creation &&
                      workflowTracking.WorkflowTrackingStatusTable == workflowTrackingStatusRecId;
        if (workflowWorkItem && (workflowWorkItem.Type == WorkflowWorkItemType::RequestChange ||
                                 workflowWorkItem.Type == WorkflowWorkItemType::Return))
        {
            select  userInfo where userInfo.id == workflowWorkItem.UserId;
            reviewer = userInfo.name;
        }
        else
        {
            select RecId, Status, Queue, IsClaimed, UserId, CreatedDateTime from workflowWorkItem order by RecId desc
                exists join workflowTrackingWorkItem
                    where workflowTrackingWorkItem.WorkflowWorkItemTable == workflowWorkItem.RecId
                join workflowTracking
                    where workflowTracking.RecId                       == workflowTrackingWorkItem.WorkflowTrackingTable &&
                          workflowTracking.TrackingContext             == WorkflowTrackingContext::WorkItem &&
                          workflowTracking.TrackingType                == WorkflowTrackingType::Creation &&
                          workflowTracking.WorkflowStepTable           == workflowStepRecId &&
                          workflowTracking.WorkflowTrackingStatusTable == workflowTrackingStatusRecId;
                if (workflowWorkItem.Status == WorkflowWorkItemStatus::Completed)
                {
                    reviewStatus = PurchReqReviewStatus::Approved;
                }
                else
                {
                    reviewStatus = PurchReqReviewStatus::Pending;
                }
                if (workflowWorkItem.Queue && workflowWorkItem.IsClaimed == NoYes::No)
                {
                    select User from workItemQueueAssignee
                        where workItemQueueAssignee.WorkitemQueue == workflowWorkItem.Queue;
                    select  userInfo where userInfo.id == workflowWorkItem.UserId;
                    reviewer = userInfo.name;
                }
                else
                {
                    select  userInfo where userInfo.id == workflowWorkItem.UserId;
                    reviewer = userInfo.name;
                }
        }
    }
    return reviewer;
}

Purchase requisition fields to policy for workflow validation

//Purchase requisition fields to policy for workflow validation
//Add

PurchReqControlSubmissionParameters

Method//Initilaze Records/

//Added new line below/

PurchReqControlSubmissionParameter::insertRecord(_purchReqControlRuleRecId, fieldNum(PurchReqLine,DepartmentId));

//Form
Procurement/Setup/Policy/purchasing Policy

Entity Store in AX 2012 R3 CU11



//Entity Store

https://1drv.ms/u/s!Asi_SJ2dXQ3Bk2RryNqktuuBXVLL

Reference Link:
https://mbs2.microsoft.com/Knowledgebase/KBDisplay.aspx?scid=kb;EN-US;3147499 

Building Default dimension from existing Default dimension

//Building Default dimension  from existing Default dimension

//Instead of struct (keyword) - Another way to merge dimension

//Merge Dimension functionality will work only when merging dimension value is empty. But in my case, I need to update if it is exist also.
  
DimensionDefault buildDefaultDimension(dimensionDefault _defaultDimension,Description255 _Custom)
    {
        DefaultDimensionView    dimensionView;
        DimensionAttributeValueSetStorage dimensionAttributeValueSetStorage;
        DimensionAttribute dimensionAttribute;
        DimensionAttributeValue dimensionAttributeValue;
        DimensionDefault dimensionDefault;
        Boolean     check = false;
        boolean     customtrue = false;
        #Define.custom("Custom")
        ;
        dimensionAttributeValueSetStorage = new DimensionAttributeValueSetStorage();
        if(_defaultDimension != 0)
        {
            while select dimensionView
                    where dimensionView.DefaultDimension == _defaultDimension
            {
                check = true;
                dimensionAttribute = DimensionAttribute::find(dimensionView.DimensionAttributeId);
                if(dimensionAttribute && dimensionView.DisplayValue)
                {
                    if(dimensionAttribute.Name != #custom)
                    {
                        dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute, dimensionView.DisplayValue, false, true);
                        dimensionAttributeValueSetStorage.addItem(dimensionAttributeValue);
                        dimensionDefault = dimensionAttributeValueSetStorage.save();
                    }
                    else
                    {
                        customtrue =true;
                        dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,_Custom, false, true);
                        dimensionAttributeValueSetStorage.addItem(dimensionAttributeValue);
                        dimensionDefault = dimensionAttributeValueSetStorage.save();
                    }
                }
            }
            if(customtrue == false && _custom)
            {
                dimensionAttribute = DimensionAttribute::findByName(#Custom);
                dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,_custom, false, true);
                dimensionAttributeValueSetStorage.addItem(dimensionAttributeValue);
                dimensionDefault = dimensionAttributeValueSetStorage.save();
            }
        }
        else
        {
            check = true;
            if(_Custom)
            {
                dimensionAttribute = DimensionAttribute::findByName(#Custom);
                dimensionAttributeValue = DimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,_Custom, false, true);
                dimensionAttributeValueSetStorage.addItem(dimensionAttributeValue);
                dimensionDefault = dimensionAttributeValueSetStorage.save();
            }
        }
        if(check == true)
        {
            return dimensionDefault;
        }
        else
        {
            return 0;
        }
    }

Mail Merge

//Mail Merge


//Reference Link

http://files.clickdimensions.com/junctionsolutionscom-alrj3/documents/microsoft_office_word_add_ins_-_microsoft_dynamics_ax_2012_-_whitepaper.pdf?_cldee=Z2Nhc2V5QGp1bmN0aW9uc29sdXRpb25zLmNvbQ%3D%3D


https://sandeepchaudhury.wordpress.com/2013/08/18/integration-of-microsoft-dynamics-ax-with-microsoft-office-word/

Get workflow is Active or not in AX 2012

//Get workflow is Active or not in AX 2012

!WorkflowTable::findActiveTemplate(workFlowTypeStr(#workflowTypeName))

Try Catch in ax 2012 X++

//Try Catch in ax 2012 X++

-->getting info in Messsage log

str msg;
int currentLine;
//Info log collection meesage
for (currentLine = startLine + 1; currentLine <= infolog.line(); currentLine++)
{
    msg += infolog.text(currentLine);
}

-->To get only the last message you can use a code like this:
str msg;
if (infolog.line() > startLine)
{
    msg = infolog.text();
}


static void TryCatchJob(Args _args)
{
    System.Exception ex;
    ;
    try
    {
        info("Check Catch block");
        throw Global::error("Written to the Infolog.");
    }
    catch (Exception::Error)
    {
        ex = CLRInterop::getLastException();
        //ex.get_Message(); // Whole Message collection
        info(strFmt("Exception message %1",ex.ToString()));
 //infolog.text(infologLine());
    }
    //like Warning/CLR error/ Dead lock/Update conflict/
}

https://msdn.microsoft.com/en-us/library/aa893385.aspx
https://community.dynamics.com/ax/b/klaasdeforche/archive/2011/08/09/try-catch-example-code

Convert Call stack to readable format in D365FO X++

//Input --container _xppCallStack = xSession::xppCallStack();  Public static str POL_formatXppCallStack(container _xppCallStack, int _skipFr...