void UserRoleAssignExcel()
{
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
FilenameOpen filename;
Dialog dialog;
DialogField dialogFilename;
System.Exception ex;
// Variable Declaration
int row = 1; // if the excel has the header
str 500 _userAlias, _compId, _roleName, _resultMsg;
str COMVariant2Str(COMVariant _cv, int _decimals = 0, int _characters = 0, int _separator1 = 0, int _separator2 = 0)
{
switch (_cv.variantType())
{
case (COMVariantType::VT_BSTR):
return _cv.bStr();
case (COMVariantType::VT_R4):
return num2str(_cv.float(),_characters,_decimals,_separator1,_separator2);
case (COMVariantType::VT_R8):
return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);
case (COMVariantType::VT_DECIMAL):
return num2str(_cv.decimal(),_characters,_decimals,_separator1,_separator2);
case (COMVariantType::VT_DATE):
return date2str(_cv.date(),123,2,1,2,1,4);
case (COMVariantType::VT_EMPTY):
return "";
default:
throw error(strfmt("@SYS26908", _cv.variantType()));
}
return "";
}
boolean assignRole( str 100 _paramUser,
str 100 _paramRole,
str 100 _paramComp)
{
UserInfo _userInfo;
SecurityRole _role;
SecurityUserRole _userRole;
OMUserRoleOrganization _userRoleOrg;
CompanyView _company;
boolean _result = true;
select _userInfo where _userInfo.networkAlias == _paramUser;
if(!_userInfo)
{
_resultMsg = 'User Not found.';
return false;
}
select _role where _role.Name == _paramRole;
if(!_role)
{
_resultMsg = 'Role Not found.';
return false;
}
select _company where _company.id == _paramComp;
if(!_company)
{
_resultMsg = 'Company Not found.';
return false;
}
// Check has Role
select *
from _userRole
where _userRole.SecurityRole == _role.RecId &&
_userRole.User == _userInfo.id;
if (!_userRole || (_userRole.AssignmentStatus != RoleAssignmentStatus::Enabled))
{
// Assign Role
try
{
_userRole.User = _userInfo.id;
_userRole.SecurityRole = _role.RecId;
_userRole.AssignmentMode = RoleAssignmentMode::Manual;
_userRole.AssignmentStatus = RoleAssignmentStatus::Enabled;
SecuritySegregationOfDuties::assignUserToRole(_userRole, null);
//info(strFmt('Role "%1" added to the user "%2" successfully.', _role.Name, _userInfo.networkAlias));
_resultMsg = '';
}
catch (Exception::CLRError)
{
ex = ClrInterop::getLastException();
if (ex != null)
{
ex = ex.get_InnerException();
if (ex != null)
{
_resultMsg = ex.ToString();
}
}
}
}
else
{
// Already assigned Role. Skip
//warning(strFmt('User "%2" already assigned role "%1". Skipping assign role.', _role.Name, _userInfo.networkAlias));
_resultMsg = "Role Exist.";
}
// Check has Company
select firstonly RecId
from _userRoleOrg
where _userRoleOrg.User == _userInfo.id &&
_userRoleOrg.SecurityRole == _role.RecId &&
_userRoleOrg.OMInternalOrganization == _company.recId;
if (!_userRoleOrg.RecId)
{
// Assign Company
try
{
_userRoleOrg.OMHierarchyType = 0;
_userRoleOrg.User = _userInfo.Id;
_userRoleOrg.SecurityRole = _role.RecId;
_userRoleOrg.OMInternalOrganization = _company.recId;
_userRoleOrg.SecurityRoleAssignmentRule = 0;
_userRoleOrg.insert();
//info(strFmt(' Company "%1" added to the role "%2" successfully.', _company.id, _role.Name));
_resultMsg = '';
}
catch (Exception::CLRError)
{
ex = ClrInterop::getLastException();
if (ex != null)
{
ex = ex.get_InnerException();
if (ex != null)
{
_resultMsg = ex.ToString();
}
}
}
}
else
{
// Already assigned Company. Skip
//warning(strFmt(' User "%3" already assigned company "%1" on role "%2".', _company.id, _role.Name, _userInfo.networkAlias));
_resultMsg = _resultMsg + " Company Exists.";
}
return _result;
}
;
application = SysExcelApplication::construct();
workbooks = application.workbooks();
//====???? select====//
dialog = new Dialog("ExcelUpload");
dialogFilename = dialog.addField(ExtendedTypeStr("FilenameOpen"));
dialog.filenameLookupFilter(["@SYS28576",'*.xlsx', "@SYS28576",'*.xls']);
dialog.filenameLookupTitle("Upload from Excel");
dialog.caption("Select excel file");
dialogFilename.value(filename);
if(!dialog.run())
info ('Select error');
filename = dialogFilename.value();
try
{
workbooks.open(filename);
}
catch (Exception::Error)
{
throw error("File not found");
}
workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();
//Iterate through cells and get the values
do
{
//Incrementing the row line to next Row
row++;
_userAlias = COMVariant2Str(cells.item(row,1).value());
_roleName = COMVariant2Str(cells.item(row,2).value());
_compId = COMVariant2Str(cells.item(row,3).value());
_resultMsg = '';
if (assignRole(_userAlias, _roleName, _compId)) {
if (assignRole(_userAlias, "System user", _compId)) {
//info(strFmt('Role "%1", company "%3" added to the user "%2" successfully. (%4)', _roleName, _userAlias, _compId, _resultMsg));
cells.item(row,4).value('Success');
cells.item(row,5).value('Normal');
cells.item(row,6).value(today());
} else {
//warning(strFmt('Failed assign "System user" role to user "%1".', _userAlias));
cells.item(row,4).value('Failed');
cells.item(row,5).value('"System user" role: ' + _resultMsg);
cells.item(row,6).value(today());
}
} else {
//warning(strFmt('Failed assign "%1" role to user "%2".', _roleName, _userAlias));
cells.item(row,4).value('Failed');
cells.item(row,5).value(_resultMsg);
cells.item(row,6).value(today());
}
// Loads the next row into the variant type and validating that its is empty or not
type = cells.item(row + 1, 1).value().variantType();
}
while (type != COMVariantType::VT_EMPTY);
application.visible(true);
// quits the application
//workbooks.save();
//workbooks.close();
//application.quit();
}
'Microsoft > Dynamics' 카테고리의 다른 글
[AX 2012] File Attachment X++ Code (0) | 2016.11.03 |
---|---|
[AX 2012] Create Privilege X++ Code (0) | 2016.11.03 |
[AX 2012] User-Role Assign X++ Code (0) | 2016.10.26 |
[AX 2012] Using Dynamics AX 2012 Trace parser (0) | 2016.09.09 |
[AX 2012] Configure and use Microsoft Dynamics AX document management with Microsoft SharePoint document libraries (0) | 2016.08.22 |