Tuesday, March 27, 2018

Fetch number of children associated with a worker

Little code snippet as provided below can help you get the number of children associated with a particular worker.


    public static void getEmployeeTotalChildren(Args _args)
    {       
        DirPartyRelationship            partyRelationship;
        DirRelationshipTypeTable        relationshipTypeTable;
        DirPerson                                                                               dirPerson;
        HcmWorker                                                                                          worker;
        DirPartyTable                                                                        partyTable;
      
        ///get the worker by personnel number
       worker         = HcmWorker::findByPersonnelNumber('NNNNN');

        select           firstonly dirPerson
        where          dirPerson.RecId == worker.Person
            join           partyTable
                where partyTable.PartyNumber == dirPerson.PartyNumber;

        select count(RecId) from partyRelationship
            join  RelationshipTypeId, SystemType from relationshipTypeTable
            where partyRelationship.ParentParty == partyTable.RecId
            &&    relationshipTypeTable.RelationshipTypeId == partyRelationship.RelationshipTypeId
            &&    relationshipTypeTable.SystemType ==  DirSystemRelationshipType::Child;

        info(strFmt("Total Children of Worker %1, are %2", worker.name(), partyRelationship.RecId));
    }

Friday, March 23, 2018

Event handlers and post handlers in D365



Let’s discuss today, how to get the table buffers, form control values, class parameters and method arguments etc., while writing our own event-handlers in D365. I’ll elaborate this using example code snippets in this post. There are already very useful posts on this topic you can look into. Here I required these event handlers while doing customization in Item master.

Form data source event handler


 [FormDataSourceEventHandler(formDataSourceStr(EcoResProductDetailsExtended, InventTable), FormDataSourceEventType::Written)]
public static void InventTable_OnWritten(FormDataSource sender, FormDataSourceEventArgs e){
    FormRun                 form           = sender.formRun();
    FormDataSource          InventTable_ds =       form.dataSource(formDataSourceStr(EcoResProductDetailsExtended,InventTable)) as FormDataSource;
   InventTable             inventTable    = InventTable_ds.cursor();
}

Form event handler

Table Buffer on form closing event


[FormEventHandler(formStr(EcoResAttributeValue), FormEventType::Closing)]
public static void EcoResAttributeValue_OnClosing(xFormRun sender, FormEventArgs e)
{
     FormDataSource ecoResProduct_ds   =          sender.dataSource(formDataSourceStr(EcoResAttributeValue, EcoResProductAttributeValue));
      EcoResProductAttributeValue      ecoResAttributeValue = ecoResProduct_ds.cursor();
}   


Control value and form event level for which auto declaration must be set true


[FormControlEventHandler(formControlStr(EcoResProductCreate, OKButton), FormControlEventType::Clicked)]
public static void OKButton_OnClicked(FormControl sender, FormControlEventArgs e)
{
       FormRun             element       = sender.formRun();
       //form control
       FormControl         modelGroupRef = element.design(0).controlName("ModelGroupId");
        Info(strfmt(“Model Group %1”, modelGroupRef.valueStr()));
       //form parameter
       ItemId              itemId        = element.parmItemId();
}

Post handler for class method


[PostHandlerFor(classStr(EcoResProductReleaseManager), methodStr(EcoResProductReleaseManager, release))]
public static void EcoResProductReleaseManager_Post_release(XppPrePostArgs args){
     EcoResProductReleaseManager releaseMgr;
    //Getting the class object
    releaseMgr     = args.getThis();
   //Getting the class parameter
   ItemId itemId  = releaseMgr.parmItemId();
   //Getting the method argument
    boolean itemCreation = args.getArg("_isCreation");
}

Post handler for overriding table methods modified field and validate Write


[PostHandlerFor(tableStr(InventTable), tableMethodStr(InventTable, validateWrite))]
public static void InventTable_Post_validateWrite(XppPrePostArgs args)
{
      InventTable inventTable = args.getThis() as InventTable
      boolean ret = true;
      // Override the validations here and set the return value accordingly.
       Args.setReturnValue(ret);
}

[PostHandlerFor(tableStr(InventTable), tableMethodStr(InventTable, modifiedField))]
public static void InventTable_Post_modifiedField(XppPrePostArgs args)
{
        //Getting the table buffer
        InventTable inventTable = args.getThis() as InventTable
       //Getting the field id method argument.
        FieldId fieldModified = args.getArg("_fieldId");
        switch (fieldModified)
        {
            //Here you can write your logic on modified field method
                break;
        }
}

Hope this post will help to give you quick start in customization and writing your code in event handlers and post handlers. More information can be found over here