Friday 27 November 2015

Attaching Ax32Server.exe process to Visual Studio


Last week I needed to debug a batch job. In AX2012 all the batch jobs run in IL. To debug these jobs you need to use Visual Studio and attach the AOS process. When I tried to attach Ax32Serve.exe, the process was disabled in the window as show below

Attach to process window
This made me wonder what is stopping the process from being available to attach. After a little investigation I realized that we are using Debug Diagnostic tool to create the dumps in case the AOS crashes and one of the crash rules to monitor the AOS was active. After de-activating the rule the AOS process was available to be attached for debugging purpose. So apparently only one application can monitor a running process.

Assign roles from one user to another


Sometimes while testing the security roles we need to have exactly the same roles a user have. The following piece of code will assign the security roles of a user to another (while keeping the existing roles intact).

static void CopyRolesFromUserToAnotherUser(Args _args)  
{
SecurityRole securityRole;
SecurityUserRole securityUserRoleSource;
SecurityUserRole securityUserRoleDestination;
SecurityUserRole securityUserRoleDestinationNew;
boolean added;
UserId sourceUserId = 'User 1';
UserId destinationUserId = 'User 2';

str getFormattedRoleName(str _roleName)
{
return SysLabel::labelId2String2(_roleName, CompanyInfo::languageId());
}

while select securityUserRoleSource
where securityUserRoleSource.User == sourceUserId
join securityRole
where securityUserRoleSource.SecurityRole == securityRole.RecId
notexists join securityUserRoleDestination
where securityUserRoleDestination.SecurityRole == securityRole.RecId
&& securityUserRoleDestination.User == destinationUserId
{
info(strFmt('Adding role %1 to the user %2.', getFormattedRoleName(securityRole.Name), destinationUserId));

securityUserRoleDestinationNew.clear();
securityUserRoleDestinationNew.User = destinationUserId;
securityUserRoleDestinationNew.SecurityRole = securityRole.RecId;
securityUserRoleDestinationNew.AssignmentMode = RoleAssignmentMode::Manual;
securityUserRoleDestinationNew.AssignmentStatus = RoleAssignmentStatus::Enabled;

SecuritySegregationOfDuties::assignUserToRole(securityUserRoleDestinationNew, null);
}
}