Thursday, June 21, 2018

SysOperation framework

Finally found a good article on SysOperation framework with detail explaination on http://devexpp.blogspot.com/2013/11/introduction-to-sysoperation-framework.html

SysOperation framework (formerly known as the Business Operation framework, or BOF) when you extend Microsoft Dynamics AX by adding new functionality that may require batch processing.

The SysOperation framework replaces the RunBase Framework and provides infrastructure for creating user interaction dialog boxes and integration with the batch server for batch processing.

The purpose of the SysOperation framework is to provide the same capabilities as the RunBase framework but with base implementations for common overrides.

 The SysOperation framework handles

  • basic user interface creation 
  • parameter serialization 
  • routing to the CLR execution environment. 
 SysOperation framework works in a  Model-View-Controller (MVC) pattern. 

SysOperation framework Classes:
Contract class     ---------- SysOperationDataContractBase 
UI Builder class  ---------- SysOperationAutomaticUIBuilder
Controller            ---------- SysOperationServiceController
Service                 ---------- SysOperationServiceBase


• Service: Service class extends from the SysOperationServiceBase class and contains the business logic for the batch operation. Developers often tend to add the business logic in controller classes, which violates the Single responsibility principle.
• Data Contract: Data contract class is the model class defining attributes needed for batch operations. These attributes are provided by the user, in a dialog. DataContractAttribute attribute is needed for the class and the properties methods requires DataMemberAttribute attribute.
• Controller: Controller class extends from the SysOperationServiceController class. It holds information about the batch operation like execution mode, show dialog or progress bar etc. and directs the batch operation.
• UI Builder: UI Builder class extends from SysOperationAutomaticUIBuilder class and is used for adding custom behavior to dialog / dialog fields dynamically constructed by the SysOperation framework.

Data Contract:
The data contract is the model class in which we define which attributes we need in our operation, commonly set as parameters by the user in a dialog. 
It's nothing more than a model class with a few attributes in it. 

We can define a SysOperation Data Contract class simply by adding the DataContractAttribute attribute to its declaraion. 

Additionally, if we want a set of methods to be available to us, we can also extend the SysOperationDataContractBase base class. With this class, we can define how our basic dialog will look like to the user. 

We can define labels, groups, sizes and types of the parameters.


UI Builder:
The UI builder class is actually an optional class for the SysOperation framework, which kind of acts as the view part of the pattern. 

You should only use it if you want to add some extra behavior to the dialog that AX constructs dynamically for you.  

To create a UI Builder, you should extend the SysOperationAutomaticUIBuilder class. It will provide you a set of methods to work with the dialog's design, but we usually add extra behavior or customize a lookup inside the postBuild method.


Controller:
The controller class has greater responsibility than the others. As the name suggests, it is the class that orchestrates the whole operation. 

The controller class also holds information about the operation, such as if it should show a progress form, if it should show the dialog, and its execution mode - asynchronous or not. 

To create a controller class you should extend the SysOperationServiceController, which will give you all of the methods you need.

Service:
You can create a service class! The only thing you have to do is extend the SysOperationServiceBase class and you're good to go. 

This class is the one that should contain all the business logic

When constructing your controller, you will indicate which class holds the operation that the controller will trigger.

Difference between temp table and container in AX

  • Data in containers are stored and retrieved sequentially, but a temporary table enables you to define indexes to speed up data retrieval.

  • Containers provide slower data access if you are working with many records. However, if you are working with only a few records, use a container.

  • Another important difference between temporary tables and containers is how they are used in method calls.

When you pass a temporary table into a method call, it is passed by reference. 
Containers are passed by value. 

When a variable is passed by reference, only a pointer to the object is passed into the method. 

When a variable is passed by value, a new copy of the variable is passed into the method. If the computer has a limited amount of memory, it might start swapping memory to disk, slowing down application execution. 

When you pass a variable into a method, a temporary table may provide better performance than a container

Wednesday, June 20, 2018

Powershell script to import AX Users with roles (Without CSV file)

Import-AXModule "AxUtilLib" $false $true
Import-AXModule "AxUtilLib.PowerShell" $true $false
Import-AXModule "Microsoft.Dynamics.Administration" $false $false
Import-AXModule "Microsoft.Dynamics.AX.Framework.Management" $false $false

$AXUsers = Get-AXUser | where {$_.AXUserId -and $_.UserName}
$AXUserId = $null
$Users = "eshant.k","Manikanta.m"

foreach($user in $users)
{


    if ($user.length -gt 8)
    {
        $AXUserId = ($user.SubString(0,7) + $user.substring($user.length -1))
        Write-Verbose "Altered AXUserId from $($user) to $AXUserId in order to fit length requirements."
     
    }

    else
    {
        $AXUserId = $user
     
    }

    New-AXUser -AccountType WindowsUser -AXUserId $AXUserId -username $user -UserDomain diplit.local
    Add-AXSecurityRoleMember -AxUserID $AXUserId -AOTName "-SYSADMIN-"

 
}

Friday, June 15, 2018

Exporting AX users with roles using Powershell script

<#
.Synopsis
Uses AX PowerShell cmdlets to Export AX user data.
.Description
Uses AX PowerShell cmdlets to Export AX user data. This script works with the corresponding Import-AXUser script.
.Parameter CsvFile
The path to the CSV file to export user data to.
.Example
.\Export-AXUser.ps1 -CsvFile .\Users.csv
Export users to a CSV file called Users.csv in the current directory.
.Notes
Name : Export-AXUser.ps1
Author: David Green
.Link
http://www.tookitaway.co.uk
https://github.com/davegreen/miscellaneous.git
#>
# The Setup-Management and Import-AXModule portaions of theis script are from a script originally by Vishy (http://vgrandhi.wordpress.com).
# (http://vgrandhi.wordpress.com/2014/04/22/ax-2012-powershell-script-to-export-users-and-roles/)
[CmdletBinding()]
Param
( [Parameter(Mandatory=$false,
ParameterSetName='CSV')]
[string]$CsvFile
)
Function Import-AXModule($axModuleName, $disableNameChecking, $isFile)
{
Try
{
$outputmessage = "Importing " + $axModuleName
Write-Verbose $outputmessage
if($isFile)
{
$dynamicsSetupRegKey = Get-Item "HKLM:\SOFTWARE\Microsoft\Dynamics\6.0\Setup"
$sourceDir = $dynamicsSetupRegKey.GetValue("InstallDir")
$axModuleName = "ManagementUtilities\" + $axModuleName + ".dll"
$axModuleName = Join-Path $sourceDir $axModuleName
}
if($disableNameChecking)
{
Import-Module $axModuleName -DisableNameChecking
}
else
{
Import-Module $axModuleName
}
}
Catch
{
$outputmessage = "Could not load file " + $axModuleName
Write-Error $outputmessage
}
}
Function Setup-Management()
{
$dynamicsSetupRegKey = Get-Item "HKLM:\SOFTWARE\Microsoft\Dynamics\6.0\Setup"
$sourceDir = $dynamicsSetupRegKey.GetValue("InstallDir")
$dynamicsAXModulesPath = join-path $sourceDir "ManagementUtilities\Modules"
$env:PSModulePath = $env:PSModulePath + ";" + $dynamicsAXModulesPath
Import-AXModule "AxUtilLib" $false $true
Import-AXModule "AxUtilLib.PowerShell" $true $false
Import-AXModule "Microsoft.Dynamics.Administration" $false $false
Import-AXModule "Microsoft.Dynamics.AX.Framework.Management" $false $false
}
Setup-Management
$OutputObj = @()
Write-Verbose "Exporting AX Accounts of type `"WindowsUser`"."
foreach ($user in Get-AxUser | Where-Object {$_.AccountType -eq "WindowsUser"})
{
if ($user.Name -and $user.UserName)
{
Write-Verbose "Exporting User $($user.UserName) ($($user.Name))."
$AXUser = New-Object PSObject
$AXUser | Add-Member NoteProperty -Name "Name" -Value $user.Name
$AXUser | Add-Member NoteProperty -Name "UserName" -Value $User.UserName
$AXUser | Add-Member NoteProperty -Name "AccountType" -Value $User.AccountType
$AXUser | Add-Member NoteProperty -Name "Enabled" -Value $user.Enabled
$AXUser | Add-Member NoteProperty -Name "Company" -Value $user.Company
$AXUser | Add-Member NoteProperty -Name "Role" -Value ((Get-AXSecurityRole -AxUserId $User.AxUserId | Select-Object -ExpandProperty AOTName) -join ",")
$OutputObj += $AXUser
}
}
if ($CsvFile)
{
Write-Verbose "Writing to CSV."
$OutputObj | Export-Csv $CsvFile -NoTypeInformation -NoClobber
}
else
{
Write-Output $OutputObj
} Source:https://github.com/davegreen/miscellaneous/blob/master/PowerShell/DynamicsAX/Export-AXUser.ps1

How to enable the dimension fields based on the Item selected on the form.

[Form] public class KMTShipFromWarehouses extends FormRun {     InventDimCtrl_Frm_EditDimensions        inventDimFormSetup;     /// &l...