Sunday, April 30, 2023

Play around with D365FO workflow .. Some code samples to

 1. Current workflow User a  by code 

public WorkflowUser currentWFUser(Common _common)

    {

        WorkflowTrackingStatusTable workflowTrackingStatusTable;

        WorkflowTrackingTable       workflowTrackingTable; 

        

        select firstonly RecId from workflowTrackingStatusTable

            join User from workflowTrackingTable

            order by WorkflowTrackingTable.CreatedDateTime desc

            where workflowTrackingTable.WorkflowTrackingStatusTable == workflowTrackingStatusTable.RecId

            && workflowTrackingStatusTable.ContextRecId             == _common.RecId

            && workflowTrackingStatusTable.ContextTableId           == _common.TableId

            && workflowTrackingTable.TrackingContext                == WorkflowTrackingContext::WorkItem;


        return workflowTrackingTable.User;

    }

2. Approve a workflow by code

public void autoApproveWF(Common _common, str _user)

    {

        WorkflowWorkItemTable WorkflowWorkItemTable;


        select WorkflowWorkItemTable

            where  workflowWorkItemTable.Type   == WorkflowWorkItemType::WorkItem

            &&  workflowWorkItemTable.Status    == WorkflowWorkItemStatus::Pending

            &&  WorkflowWorkItemTable.RefTableId== _common.TableId

            &&  WorkflowWorkItemTable.RefRecId  ==  _common.RecId;


        WorkflowWorkItemActionManager::dispatchWorkItemAction(

                                WorkflowWorkItemTable,

                                "Auto Approve by system",

                                _user,

                                WorkflowWorkItemActionType::Complete,

                                menuitemDisplayStr(PurchReqTable)); 

    }


3, Display approver name

 display WorkflowApprovalName Displayapprovername()

    {

        WorkflowTrackingStatusTable workflowTrackingStatusTable;

        WorkflowTrackingTable WorkflowTrackingTable;

        WorkflowApprovalName approvername;


        select firstonly workflowtrackingstatustable

            join workflowtrackingtable

           where workflowtrackingstatustable.ContextRecId == this.recid

            && workflowtrackingtable.TrackingContext == workflowtrackingcontext::WorkItem

            && workflowtrackingtable.TrackingType == workflowtrackingtype::Approval

            && workflowtrackingtable.WorkflowTrackingStatusTable == workflowtrackingstatustable .recid;

        {

            approvername = workflowtrackingtable.User;

        }

        return approvername;

    }

    4. Get last approver name for PO by PurchID


     select firstFast _PURCHTABLE where _PURCHTABLE.PurchId == purchTable.PurchId ;

            select firstFast _WorkflowTrackingStatusTable

             order by _WorkflowTrackingStatusTable.RecId desc

             where _WorkflowTrackingStatusTable.ContextRecId == _PURCHTABLE.RecId  && _WorkflowTrackingStatusTable.ContextCompanyId == curext()

                          && _WorkflowTrackingStatusTable.TrackingStatus == WorkflowTrackingStatus::Completed ;

            select _WorkflowTrackingTable

                    order by _WorkflowTrackingTable.createdDateTime desc

                   where _WorkflowTrackingTable.WorkflowTrackingStatusTable == _WorkflowTrackingStatusTable.RecId

                && _WorkflowTrackingTable.TrackingType == 4 ;


Saturday, April 29, 2023

AX / D365FO – GROUPBY ON LOOKUP

 I have a custom lookup with a GroupBy inside Query statement

First time, when the is nothing choosen the group by works fine. If I choose something from list and again want to change it, now the group by is lost.

To solve the issue simply use sysTableLookup.parmUseLookupValue(false) like show in below code

        public void lookup()
        {
            Query query = new Query();
            QueryBuildDataSource queryBuildDataSource;
            QueryBuildRange queryBuildRange;
    
            SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(ots_GuidedSalesOrderEntryRules), this);
    
            sysTableLookup.addLookupField(fieldNum(ots_GuidedSalesOrderEntryRules, ToleranceType));
    
            queryBuildDataSource = query.addDataSource(tableNum(ots_GuidedSalesOrderEntryRules));

            QueryGroupByField queryGroupByField = queryBuildDataSource.addGroupByField(fieldNum(ots_GuidedSalesOrderEntryRules, ToleranceType));
            sysTableLookup.parmUseLookupValue(false);
            sysTableLookup.parmQuery(query);
    
            sysTableLookup.performFormLookup();
        }

Tuesday, April 18, 2023

D365 X++ Passing parameters in Microsoft Dynamics

 Args is your companion in the world of X++ especially when passing parameters in Microsoft Dynamics AX. Args is an abbreviation for arguments. It allows you to pass information from one object to another newly created object. I utilize it frequently to pass the objects, records, strings, etc that I need to have in scope while accessing the object from another object. It’s very easy to utilize the Args class to get the desired result you’re looking for.

The following example demonstrates how to create an instance of the Args class:

Declaration of Args

Args  args = new Args();
CustTable  custTable;

Setting record is Args

select custTable where custTable.AccountNum == ‘XXXX’

if(custTable)
{
args.record(custTable);
}

 

Some important methods of Args class

Caller

Get or sets the instance of the object that created this instance of the Args class.

Name

Gets and sets the name of the application object to call.

Parm

Gets or sets a string that specifies miscellaneous information for the called object.

parmEnum

Gets or sets the enumeration value of the enumeration type that is specified in the parmEnumType method.

parmEnmType

Gets or sets the ID value of any enumeration type.

ParmObject

Gets or sets an instance of any object to pass, to the called object.

Record

Gets and sets the record from the table on which the caller object is working.

 

Sample Code  For Setting Value in Args

Create an instance of the Args and custom class.

 Args  args;
CustTable  custTable;
//custom class object to pass
Student student= new Student();
args = new args();

Set the parameter which you want to pass. If you just want to pass a simple string you can do it like this

 args.parm("Hello World");

If you want to pass an enum value you can do so via following code.

args.parmEnum( NoYesEnumValue.selection() );
args.parmEnumType( EnumNum( NoYes ) );

If you want to pass table buffer  in parameter

args.record( EmplTable );

If we want pass a more complex set of parameters, you can develop your own class just for passing those parameters.

student.parmName(‘William’ );
student.parmRollNum(‘st-123‘);
args.parmObject(student);

If you to pass records from a table, use following code.

select custTable where custTable.AccountNum == ‘XXXX’

if(custTable)
{
args.record(custTable);
}

 

Sample Code  For Getting  Value From Args

Create an instance of the Args class and your custom class.

Args   args;
CustTable  custTable;
//custom class object to pass
Student student= new Student();
args = new args();

Check that the passed arguments are not null

if( element.args() )
{
.....
}

Get string value

strValue.text( element.args().parm() );

Check parmEnum is not null and then get enum parameter

if( element.args().parmEnumType() == EnumNum( NoYes ) )
{
NoYesEnumValue.selection( element.args().parmEnum() );
}

Check parmEnum is not null and then get object

if( element.args().parmObject() )
{
student = element.args().parmObject();
}

Check that the table buffer is not null and then get record.

if( element.args().record() && element.args().record().TableId == TableNum( CustTable ) )
{
custTable=  element.args().record();
}

D365 Calling - Opening AX form through X++

static voidOpenForm_ThroughCode(Args _args)
{
    Args                            args;
    Object                          formRun;

    // open form
    args = new Args();
    args.name(formstr(FormName));
    formRun = classfactory.formRunClass(args);
    formRun.init();
    formRun.run();
    formRun.wait();
}

If you want to pass a record to open a form

args = newArgs();
args.record(ProjTable::find('PR00001'));
args.name(formstr(FormName));
formRun = classfactory.formRunClass(args);
formRun.init();
formRun.run();

formRun.wait();

How to retrieve these args on caller form's init()

public voidinit()
{
    ProjTable   projTableLocal;   
    super();   
    projTableLocal = element.args().record();   

}


Call A Form Using A Menu Item

[Form]
public class rsmModel extends FormRun
{
    [Control("Button")]
    class FormButtonControlMake
    {
        /// <summary>
        /// Call the rsmMake form
        /// </summary>
        public void clicked()
        {
            Args    args;
            FormRun formRun;

            super();

            //Call the form using a Menu Item
            args = new Args();
            formRun = new menufunction(menuItemDisplayStr(rsmModel), MenuItemType::Display).create(args);
            
            formRun.init();
            formRun.run();
            formRun.wait();
        }
    }
}

 

Thursday, April 13, 2023

Form Control Event Handler Methods in Dynamics 365

 For example, In Dynamics 365 for Operations you can react to the OnClicked event by copying the event handler method for the event and pasting the method into a class. 


Below is an example of an event handler method that reacts to the OnClicked event of a button on a form.



Create new Class and paste below code in it for SalesEditLines EventHandlers

 /// <summary>
    ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormControlEventHandler(formControlStr(SalesEditLines, OK), FormControlEventType::Clicked)]
    public static void OK_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        Args args = new Args();
        FormCommandButtonControl  callerButton = sender as FormCommandButtonControl;  //Retrieves the button that we're reacting to
        FormRun form = callerButton.formRun(); //Gets the running SalesEditLines form

        //Get the salesId that was selected in the SalesEditLines form
        FormDataSource salesParmTable_ds = form.dataSource(formDataSourceStr(SalesEditLines, SalesParmTable)) as FormDataSource;
        SalesParmTable salesParmTable = salesParmTable_ds.cursor();


        SalesTable salesTable=salesParmTable.salesTable();

        if(salesTable.SalesStatus==SalesStatus::Invoiced)
        {
     
//Any Action

        }
    }

Reference

Reference



Another Sample for OnModified of check box control - AllowEdit  text box control based on checkbox selection




 /// <summary>
    ///
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    [FormControlEventHandler(formControlStr(EcoResProductDetailsExtended, Other_CatchWeight), FormControlEventType::Modified)]
    public static void Other_CatchWeight_OnModified(FormControl sender, FormControlEventArgs e)
    {
        FormCheckBoxControl  callerButton = sender as FormCheckBoxControl ;  //Retrieves the button that we're reacting to
        FormRun element = callerButton.formRun();
        FormControl catchweightMultipleToSubProject = element.design(0).controlName("Other_CatchweightMultiple");
        if(callerButton.checked())
        {
            catchweightMultipleToSubProject.allowEdit(true);
        }
        else
        {
            catchweightMultipleToSubProject.allowEdit(false);
        }
    }


On Look Up Event for String Control in the form

 [FormControlEventHandler(formControlStr(VendBankAccounts, PartyPaymLocationName), FormControlEventType::Lookup)]
    public static void PartyPaymLocationName_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        FormControl   callerStr = sender as FormControl;  //Retrieves the string that we're reacting to
        FormRun form = callerStr.formRun(); //Gets the running VendBankAccounts form

        FormDataSource vendBankAccount_ds = form.dataSource(formDataSourceStr(VendBankAccounts, VendBankAccount)) as FormDataSource;
        VendBankAccount vendBankAccount = vendBankAccount_ds.cursor();

        vendBankAccount.vendPartyPaymLookup(callerStr );
 

    }

Creating License Plate at Item Arrival line creation while Enter non-existing Liecense Plate



 public static void InventoryDimensionsGrid_LicensePlateId_OnEnter(FormControl sender, FormControlEventArgs e)
    {
            FormControl   callerStr = sender as FormControl;  //Retrieves the string that we're reacting to
            FormRun form = callerStr.formRun(); //Gets the running  form

            FormStringControl InventoryDimensionsGrid_LicensePlateId = form.design(0).controlName("InventoryDimensionsGrid_LicensePlateId");

            if(InventoryDimensionsGrid_LicensePlateId.text())
            {
                WHSLicensePlate::createLicensePlate(InventoryDimensionsGrid_LicensePlateId.text());
            }
    }

Assign or SET value to Form string control 

 [FormControlEventHandler(formControlStr(InventJournalTransferReceive, InventDimIssue_InventBatchId), FormControlEventType::Modified)]
    public static void InventDimIssue_InventBatchId_OnModified(FormControl sender, FormControlEventArgs e)
    {

        FormControl   callerStr = sender as FormControl;  //Retrieves the string that we're reacting to
        FormRun form = callerStr.formRun(); 
     
//   FormDataSource dsInventJournalTrans = sender.formRun().dataSource("InventJournalTrans");

     
        FormStringControl InventJournalTrans_PurchId = form.design(0).controlName("InventJournalTrans_PurchId");
     
        
     
               InventJournalTrans_PurchId.enabled(true);

                //True parameter append the text but false removes existing text and paste new text

                InventJournalTrans_PurchId.pasteText("SET TEXT HERE",FALSE);
                InventJournalTrans_PurchId.enabled(false);

              
            }
        }
    }

Event handlers usage through code in D365

 Here are some of basic use of EVENTS handlers of the Form with respective syntax for logic.

Form datasource from xFormRun
[FormEventHandler(formStr(SomeForm), FormEventType::Initialized)]
public static void SomeForm_OnInitialized(xFormRun sender, FormEventArgs e)
{
FormDataSource MyRandomTable_ds = sender.dataSource(formDataSourceStr(SomeForm, MyRandomTableDS));
...
}

Get FormRun from form datasource
[FormDataSourceEventHandler(formDataSourceStr(MyForm, MyRandomTableDS), FormDataSourceEventType::Written)]
public static void MyRandomTableDS_OnWritten(FormDataSource sender, FormDataSourceEventArgs e)
{
FormRun formRun = sender.formRun() as FormRun;
formRun.myCustomMethod();
}

Get FormRun from form control

[FormControlEventHandler(formControlStr(MyForm, MyButton), FormControlEventType::Clicked)]
public static void MyButton_OnClicked(FormControl sender, FormControlEventArgs e)
{
FormRun formRun = sender.formRun() as FormRun;
formRun.myCustomMethod();
}

Access form control from xFormRun
[FormEventHandler(formStr(SomeForm), FormEventType::Initialized)]
public static void SomeForm_OnInitialized(xFormRun sender, FormEventArgs e)
{
sender.design().controlName(formControlStr(SomeForm, MyControl)).visible(false);
}

Get current record in form control event
[FormControlEventHandler(formControlStr(SomeForm, SomeButton), FormControlEventType::Clicked)]
public static void SomeButton_OnClicked(FormControl sender, FormControlEventArgs e)
{
SomeTable callerRec = sender.formRun().dataSource(1).cursor();
}