Monday, 23 April 2012

Creating a Case with association using microsoft Dynamics ax

 In this post I will tell you how to create a simple case along with one association using x++.
In Microsoft Dynamics AX 2012, a “case” can be defined as an issue that needs a resolution. For eg, we have an education solution that provides students the facility to find a suitable university they wish to apply to. After a student gets registered in a certain university, a “Case” will be created. Same will be the scenario for a hospital management application when a patient gets admitted to the hospital and is treated by a doctor.
 You can use case management in Microsoft Dynamics AX and in Enterprise Portal for Microsoft Dynamics AX to record, update, track, follow up on, and close issues that are raised by your customers, vendors, or employees, or that are created through your audit processes. By planning, tracking, and analyzing cases, you can develop efficient resolutions that can be used for similar issues.
Tables involved:
The following tables are involved in case management:
CaseDetailBase (the base table through which other tables are derived)
CaseDetail  (the main table)
CaseLog (the log table. This is the table in which additional information is stored)
CaseAssociation ( any association of a case is stored in this table. For education management solution, a “Student” will be associated with the case. Likewise, for a hospital management solution, a “Patient” legal entity will be associated with the case and so on.)

To create a case with association using dynamics ax, create a class using AOT by right clicking on "Classes"-> "New Class" and add a suitable name to the class. I have named my class to EdmPostRegistrationEventsManager. Now add the two static methods to the class. One is for "Case" creation and another is for "Case Association" creation.

Here is the x++ code to create a case:

private static void createCase(RecId _studentId,
                              RecId _party,
                              NoYesId _isAssociationPrimary,
                              Notes _notes,
                              Description _description,
                              CaseCategoryRecId _caseCategoryRecId,
                              HierarchyName _processName,
                              CaseStatus _caseStatus,
                              RecId _prospectId=0
                              ){

    CaseDetailBase caseDetail;
    CaseId caseId;

    ttsBegin;


    if(CaseDetailBase::findByParty(_party).RecId==0){
    //generate the case id first.
    caseId = NumberSeq::newGetNum(smmParameters::numRefCaseId()).num();

    caseDetail.CaseId = caseId;

    caseDetail.Memo=_notes;

    CaseDetail.Description=_description;

    caseDetail.Party=_party;

    caseDetail.Process= _processName;

    caseDetail.CategoryRecId=_caseCategoryRecId;

    caseDetail.Status= _caseStatus;

    //insert the case.
    caseDetail.insert();

 
    //insert the case association(s).
    EdmPostRegistrationEventsManager::createCaseAssociation(CaseDetailBase::find(caseId).RecId,CaseEntityType::EdmStudent,_studentId,NoYes::No); 

    }

    ttsCommit;
}


And following is the method for creating a case association:

private static void createCaseAssociation(RecId _caseRecId,CaseEntityType _caseEntityType,RecId _refRecId,NoYesId _isAssociationPrimary){

    CaseAssociation caseAssociation;
    //insert the student association first.
    caseAssociation.EntityType = _caseEntityType;
    caseAssociation.RefRecId=_refRecId;
    caseAssociation.IsPrimary=_isAssociationPrimary;
    caseAssociation.CaseRecId=_caseRecId;
    caseAssociation.insert();
}

2 comments:

  1. Hi,
    i tried to use your job but i don't have a static methode called numRefCaseId() in a smmParameters calss. I work with AX2012 R2 with CU7

    Thank you

    ReplyDelete