Search This Blog

Monday, July 13, 2015

Ax2012 Useful functions 2

Show viewhistory on form datasources if you have ValidaTimeState Enabled DateTime enabled on the tables of that form. 
create this method in Global class so you can call whereever you want in form button click as below.

void clicked()

{
    buttonHistoryClick(element, this);
    Super();
}

static public void buttonHistoryClick(FormRun _formRun, FormButtonControl _fbc)
{
    void changeDataSources(ValidTimeStateAutoQuery _from, ValidTimeStateAutoQuery _to, boolean _allowDelete)
    {
        Counter         dataSourceNo;
        FormDataSource  formDataSource;

        for (dataSourceNo=1;_formRun.dataSourceCount()>=dataSourceNo;dataSourceNo++)
        {
            formDataSource = _formRun.dataSource(dataSourceNo) as FormDataSource;
            if (formDataSource.validTimeStateAutoQuery() == _from && new DictTable(formDataSource.table()).isValidTimeStateTable())
            {
                formDataSource.validTimeStateAutoQuery(_to);
                switch (_to)
                {
                    case ValidTimeStateAutoQuery::AsOfDate:
                        formDataSource.validTimeStateAutoQuery(ValidTimeStateAutoQuery::AsOfDate);
                        formDataSource.query().resetValidTimeStateQueryType();
                        formDataSource.allowDelete(_allowDelete);
                        break;
                    case ValidTimeStateAutoQuery::DateRange:
                        formDataSource.validTimeStateAutoQuery(ValidTimeStateAutoQuery::DateRange);
                        formDataSource.query().validTimeStateDateTimeRange(DateTimeUtil::minValue(), DateTimeUtil::maxValue());
                        formDataSource.allowDelete(_allowDelete);
                        break;
                }
                formDataSource.executeQuery();
            }
        }
    }

    if (_fbc.labelText() == "@SYS110266")
    {
        changeDataSources(ValidTimeStateAutoQuery::AsOfDate, ValidTimeStateAutoQuery::DateRange, false);
        _fbc.text("Stop viewing History");
        _fbc.normalImage("10006");
    }
    else
    {
        changeDataSources(ValidTimeStateAutoQuery::DateRange, ValidTimeStateAutoQuery::AsOfDate, true);
        _fbc.Text("@SYS110266");
        _fbc.normalImage("10007");
    }

}
// this method is used to create filenameTimeStamp.

static FileName createFilenameTimeStamp()
{
    FileName    ret;
    Microsoft.Dynamics.IntegrationFramework.Adapter.FileSystem       fileSystem;
    #Aif

    fileSystem      = AifUtil::getClrObject(#FileSystemProgId);

    ret = fileSystem.GetCurrentTimestamp();

    return ret;
}


// This method is used to split the string, and returns the conatiner with the position defined in the parameter
public static str rsaStrSplit(str _splitString,str _splitchar,int _pos)
{
    List strlist=new List(Types::String);
    ListIterator    iterator;
    container       packedList;
    ;
    strlist=strSplit(_splitString,_splitchar);
    iterator = new ListIterator(strlist);
    while(iterator.more())
    {
        packedList += iterator.value();
        iterator.next();
    }
    return conPeek(packedList,_pos);

}

public static str encrypt(str _input, str _salt = '')
{
    System.Security.Cryptography.SHA512Managed  sha512managed = new System.Security.Cryptography.SHA512Managed();
    System.Text.Encoding                        encoding = System.Text.Encoding::get_UTF8();

    System.Byte[]                               inputBytes;
    System.Byte[]                               resultBytes;

    int                                         i;
    str                                         returnString;
    ;

    new InteropPermission(InteropKind::ClrInterop).assert();

    inputBytes = encoding.GetBytes(strLwr(_salt) + _input); // Convert lower case salt + input into byte array

    // The input is hashed 1024 times for attack resiliency
    for (i = 0; i < 1024; i++)
    {
        resultBytes = resultBytes ? resultBytes : inputBytes; // First loop uses input for hashing
        resultBytes = sha512managed.ComputeHash(resultBytes);
    }

    returnString = System.Convert::ToBase64String(resultBytes);

    CodeAccessPermission::revertAssert();

    return returnString;
}

private static Map fileGetList(FilePath            _filePathArchive)

{
 
    Map                 mapFiles;

    InteropPermission   interopPermission = new InteropPermission(InteropKind::ClrInterop);
    Set                 interopPermissionSet = new Set(Types::Class);
    System.Array        arrayFiles;

    int                 i;
    ;

    // Granting file permission rights
    interopPermissionSet.add(interopPermission);
    CodeAccessPermission::assertMultiple(interopPermissionSet);

    mapFiles = new Map(Types::String, Types::String); // Key = return file | Value = archive path

        if (!System.IO.Directory::Exists(_filePathArchive))
        {
             throw  error("Path doesnt exist");
        }

        arrayFiles = System.IO.Directory::GetFiles(_filePathArchive);

        // CLRInterop::getAnyTypeForObject method is used to handle difference in AX and System types (e.g. System.Int32 != int)

        for (i = 0; i < CLRInterop::getAnyTypeForObject(arrayFiles.get_Length()); i++)
        {
       mapFiles.insert(CLRInterop::getAnyTypeForObject(arrayFiles.GetValue(i)), _filePathArchive);
        }
    }

    // Reverting file permission rights
    CodeAccessPermission::revertAssert();

    return mapFiles;
}


/// <summary>
///  Gets the SenderID from AIF xml .
/// </summary>
/// <param name="messagePartsXml">
/// An <c>AifXml</c> value.
/// </param>
/// <returns>
/// An instance of the <c>str document Namespace</c> class.
/// </returns>
public static str getSenderIDValue(AifXml messagePartsXml)
{
     XmlTextReader               xmlReader;
    str value,currentElement,pureElement;

;
#Aif
    xmlReader = XmlTextReader::newXml(messagePartsXml);
    while (xmlReader.Read())
    {
        switch (xmlReader.nodeType())
        {
             case XmlNodeType::Element:
                  currentElement = xmlReader.name();
                 break;
             case XmlNodeType::Text:
                  pureElement = subStr(currentElement,strFind(currentElement,':',1,256)+1,256);
                    switch (pureElement)
                    {
                       case "SenderId":
                        {
                            value=xmlReader.value();
                            return value;
                        }
                        break;
                    }
              break;
        }
    }

    return value;
}


AX2012 Replacing Company element value in AIF using .net dll and QueryService

Scenario:
In AIF we have some data coming from externalSystem, which externalSystem doesnt have anyinformation to which company it should load, as ExternalSystem knows the information about coRegNum as we have to find the right company in AX by using coRegNum.
Solution:
so I build a .net component where we use the dll in transformation to replace the companyelement in the header to pass the file into right company.
I am using QueryService which I querying the companyInfoTable with the range coRegNum and getting the right Company and replacing in the sourceXML in transfromation in headersection.

sample xml section below.

<ns0:Envelope xmlns:ns0="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<ns0:Header>
<ns0:MessageId>354bcae9-45a5-411e-953f-e2920a6c0229</ns0:MessageId>
<ns0:Company>123456</ns0:Company>
<ns0:Action>http://schemas.microsoft.com/dynamics/2008/01/services/GeneralJournalService/create</ns0:Action>
</ns0:Header>
<ns0:Body>
<ns0:MessageParts>
<ns0:LedgerGeneralJournal xmlns:ns0="http://schemas.microsoft.com/dynamics/2008/01/documents/LedgerGeneralJournal" xmlns:st="http://schemas.microsoft.com/dynamics/2008/01/sharedtypes">

Before writing this class add serviceReference QueryService from AX standardservice into your solution that will create app.config as below

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="QueryServiceEndpoint" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    transactionFlow="false" transferMode="Streamed" transactionProtocol="OleTransactions"
                    hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                    maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
                    maxReceivedMessageSize="65536">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Transport">
                        <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                        <message clientCredentialType="Windows" />
                    </security>
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://KrishhDax:8201/DynamicsAx/Services/QueryService"
                binding="netTcpBinding" bindingConfiguration="QueryServiceEndpoint"
                contract="AXQueryService.IQueryService" name="QueryServiceEndpoint">
                <identity>
                    <servicePrincipalName value="host/KrishhDax.adep01.nordic.rsa-ins.com" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>

</configuration>

Create a tranform class which extends from Itransform.
I am using xml.linq to read the element value in the class, so I referenced System.Xml.Linq;

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Dynamics.IntegrationFramework.Transform;
using System.Xml;
using System.Xml.Linq;
using KrishhIbanToCompanyTrans.AXQueryService;
using System.ServiceModel;
using System.Data;
using System.Net;


namespace KrishhIbanToCompanyTrans
{
    public class IBANToCompanyTrans : ITransform
    {
        public void Transform(System.IO.Stream input, System.IO.Stream output, string config)
        {
            XDocument inputXml = XDocument.Load(input);

            string accStr = inputXml.Root.Descendants().Where(e => e.Name.LocalName == "Company").First().Value;
           
            string[] serviceParams = config.Split(';');

            if (serviceParams.Count() != 1 && serviceParams.Count() != 4)
                throw new ArgumentException("Invalid configuration passed. Expecting single endpoint address or endpoint address with username, password and domain, separated by ;");

            NetTcpBinding binding1 = new NetTcpBinding();
            EndpointAddress epa = new EndpointAddress(serviceParams[0]);

            binding1.Name = "QueryServiceEndpoint";
            binding1.TransactionProtocol = TransactionProtocol.OleTransactions;
            binding1.TransferMode = TransferMode.Streamed;
            binding1.Security.Mode = SecurityMode.Transport;
            binding1.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            binding1.Security.Transport.ClientCredentialType =  TcpClientCredentialType.Windows;
            binding1.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
            binding1.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
            binding1.ReliableSession.Enabled = false;

            QueryServiceClient serviceClient = new QueryServiceClient(binding1, epa);

            if (serviceParams.Count() == 4)
            {
                serviceClient.ClientCredentials.Windows.ClientCredential.UserName = serviceParams[1];
                serviceClient.ClientCredentials.Windows.ClientCredential.Password = serviceParams[2];
                serviceClient.ClientCredentials.Windows.ClientCredential.Domain = serviceParams[3];
            }

            QueryMetadata query = new QueryMetadata();
            query.DataSources = new QueryDataSourceMetadata[1];
            query.Name = "CompanyInfo";
            QueryDataSourceMetadata queryDS = new QueryDataSourceMetadata();
            queryDS.Name = "CompanyInfo";
            queryDS.Table = "CompanyInfo";
            queryDS.Enabled = true;
            query.DataSources[0] = queryDS;
            queryDS.DynamicFieldList = false;
            queryDS.Fields = new QueryDataFieldMetadata[2];
            QueryDataFieldMetadata fieldId = new QueryDataFieldMetadata();
            fieldId.FieldName = "DataArea";
            fieldId.SelectionField = SelectionField.Database;
            queryDS.Fields[0] = fieldId;
            QueryDataFieldMetadata fieldName = new QueryDataFieldMetadata();
            fieldName.FieldName = "CoRegNum";
            fieldName.SelectionField = SelectionField.Database;
            queryDS.Fields[1] = fieldName;
            queryDS.Ranges = new QueryDataRangeMetadata[] { new QueryDataRangeMetadata() { TableName = "CompanyInfo", FieldName = "CoRegNum", Value = accStr, Enabled = true } };

            Paging paging = new ValueBasedPaging() { RecordLimit = 25 };



            DataSet dataset = serviceClient.ExecuteQuery(query, ref paging);
            Console.Write(dataset.GetXml());

            if (dataset != null)
            {
                if (dataset.Tables.Count > 2)
                {
                    if (dataset.Tables[3].Rows.Count > 0)
                    {
                        DataRow dr = dataset.Tables[3].Rows[0];

                        var element = inputXml.Root.Descendants().Where(e => e.Name.LocalName == "Company").First();
                        if (element != null)
                            element.Value = dr["DataArea"].ToString();
                    }
                }
            }
            inputXml.Save(output);

        }

This method is used to test the above code.
/*
        static void Main(string[] args)
        {
            IBANToCompanyTrans transformClass = new IBANToCompanyTrans();

            FileStream input, output;

            // Create the two streams to pass into the assembly. You will need to change these
            // to your file locations.
            input = new FileStream("C:\\Temp\\922\\922.xml", FileMode.Open);
            output = new FileStream("C:\\Temp\\922\\922_c.xml", FileMode.OpenOrCreate);

            string paramString = "net.tcp://hssdas115:8201/DynamicsAx/Services/QueryService";
            // Passes in the customers CSV file and gets back an XML file.
            transformClass.Transform(input, output, paramString);

            // Displays the XML to the console.
            //            StreamReader sreader = new StreamReader(new FileStream("c:\\temp\\testtmpn.xml", FileMode.Open));
            //            Console.Write(sreader.ReadToEnd());
            //            Console.ReadLine();

        } */
    }
}

After building this component, go to the inboundPort and click transformation button and go to transformations and create the .net dll  transformation and provide the dll and select the class in the dropdown.
in Config file provide the app.config file for this transformation.