Wednesday, 21 September 2016

Restrict mutiple logins of same user in ax 2012

Note:Compied from external source

Copy Paste the Following Code in startupPost method of info class in AOT

void startupPost()
{
// To restrict user login form second login
xSession session;
SysClientSessions SysClientSessions;
UserId currentUserId;
int counter;
;
currentUserId = curUserId();
if(currentUserId!="Admin")// Allow Admin User to login multiple time
{
while select SysClientSessions
where SysClientSessions.userId == currentUserId &&
SysClientSessions.Status == 1 // 1 : Login 0 : Logout
{
session = new xSession(SysClientSessions.SessionId, true);
if (session && session.userId())
{
counter++;
}
}
if(counter>=2)
{
Box::stop("Already Logged-in : The same user id can't log in twice.");
infolog.shutDown(true);
}
}
}

Wednesday, 14 September 2016

Creation of simple workflow in AX 2012


Creating Workflow for courses in HumanResource Module.
  • Create WFstatus enum

















  • Create query for that table
















  • Add workflowstate enum as a field in HRMCourseTable
  • Override CanSubmittoworkflow() of table with following code
        public boolean canSubmitToWorkflow(str _workflowType = '')
{
  boolean ret;
     ret = super(_workflowType);
     ret = this.RecId != 0 && this.WorkflowState == WorkflowState::NotSubmitted;
     return ret;
}

  • Also write Updateworkflowstate method(Not override)
public static void UpdateWorkflowState(RefRecId _recId, WorkflowState _state)
{
    HRMCourseTable hRMCourseTable = hRMCourseTable::findByRecId(_recId, true);

    ttsBegin;
    hRMCourseTable.WorkflowState = _state;
    if(hRMCourseTable.RecId)
        hRMCourseTable.update();
    ttsCommit;
}
  • Create/Identify WF category(module)
  • Create WF type from wizard


  • Create WF approval from wizard and give appropriate label names to all created MIs

  • Private projects will be created.
  • In WF Type main() of submitMngr class write as below and make necesary changes 
public static void main(Args args)
{
    HRMCourseTable                          hRMCourseTable;
    FormRun                                 formRun = args.caller();
    recId                                   recId = args.record().RecId;
    WorkflowCorrelationId                   workflowCorrelationId;

    WorkflowTypeName                        workflowTypeName =workflowtypestr(CoursesWFType);

    WorkflowComment                         note ="";
    WorkflowSubmitDialog                    workflowSubmitDialog;

    workflowSubmitDialog = WorkflowSubmitDialog::construct(args.caller().getActiveWorkflowConfiguration());
    workflowSubmitDialog.run();

    if (workflowSubmitDialog.parmIsClosedOK())
    {
        recId = args.record().RecId;
        hRMCourseTable = HRMCourseTable::findByRecId(recId);
       //We can validate record before submite and will through error
        note = workflowSubmitDialog.parmWorkflowComment();
        try
        {
            ttsbegin;
            workflowCorrelationId = Workflow::activateFromWorkflowType(workflowTypeName,recId,note,NoYes::No);
            hRMCourseTable::UpdateWorkflowState(recId,WorkflowState::PendingApproval);

            info("Submitted to workflow.");
            ttscommit;
        }
        catch(exception::Error)
        {
            info("Error on workflow activation.");
        }
    }
    formRun.dataSource().research(1);
    formRun.reload();
}
  • In the WFType Eventhandler write appropriate code in existed methods
public void completed(WorkflowEventArgs _workflowEventArgs)
{
    if (_workflowEventArgs.parmWorkflowContext().parmTableId() == tableNum(HRMCourseTable))
        HRMCourseTable::UpdateWorkflowState(_workflowEventArgs.parmWorkflowContext().parmRecId(),WorkflowState::Approved);
}

  • In WFApproval ResubmitActionManager class, write following
public class CoursesWFApprovalResubmitActionMgr
{
    RecId           recId;
    Args            args;
}

protected boolean actionDialog(WorkflowWorkItemActionDialog _workflowWorkItemActionDialog)
{
    _workflowWorkItemActionDialog.run();
    return _workflowWorkItemActionDialog.parmIsClosedOK();
}


protected WebActionItemName getEPReSubmitAction()
{
    WebActionItemName WebActionItemName;
    return WebActionItemName;
}


public Args parmArgs(Args _args = args)
{
    args = _args;

    return args;
}




public boolean reSubmit()
{
    WorkflowWorkItemActionDialog    workflowWorkItemActionDialog;
    WorkflowWorkItemTable           workItem = args.caller().getActiveWorkflowWorkItem();
    boolean                         ret;
    boolean                         dispatchWorkItem = false;
    WorkflowComment                 comment;
    UserId                          targetUser;

    if (workItem.RecId)
    {
        workflowWorkItemActionDialog = WorkflowWorkItemActionDialog::construct( workItem,
                                                                                    WorkflowWorkItemActionType::Resubmit,
                                                                                    
new MenuFunction(args.menuItemName(), args.menuItemType()));
            if (this.actionDialog(workflowWorkItemActionDialog))
            {
                comment = workflowWorkItemActionDialog.parmWorkflowComment();
                targetUser = workflowWorkItemActionDialog.parmTargetUser();

                dispatchWorkItem = true;
            }

    }

    if (dispatchWorkItem)
    {
        WorkflowWorkItemActionManager::dispatchWorkItemAction(workItem,
                                                                comment,
                                                                targetUser,
                                                                WorkflowWorkItemActionType::Resubmit,
                                                                args.menuItemName(),
                                                                args.menuItemName() == this.getEPReSubmitAction());
        ret = true;
    }

    return ret;
}



public static void main(Args args)
{
    CoursesWFApprovalResubmitActionMgr submitManager = new CoursesWFApprovalResubmitActionMgr();
    submitManager.parmArgs(args);
    submitManager.reSubmit();
}

  • Drag WFapproval into WFtype-->supported elements
  • Give Form design properties(WorkflowEnabledyes,WorkflowDataSource,WorkflowType)
  • Generate Incremental CIL.
  • Configure WF












  • Double click on approval element