DAXSPOT

Saturday, November 12, 2022

Confirmation dialog box/pop up box, X++

Hello,


Below code will help you to show a pop up confirmation dialog.  


boolean    confirmButton;

confirmButton = Box::confirm("Are you sure want to confirm?", "Confirmation", "Once Confirmed, record can not be edited!");

if(confirmButton == true)

{

     info("Confirmed");

}

else

{

    info("Not Confirmed");

}


Thank you,

Tuesday, November 8, 2022

Calculate no of months between two dates, X++ Updated

Hello,

Recently I got a small problem to calculate no of months between two dates.

Dynamics Ax 2012 provides a default function which calculates no of intervals between two dates based on enumeration.


Function looks like:

intvNo(date input_date, date ref_date, int func)


You can use it as;

noOfIntervals = intvNo(refDate, inputDate, intvScale::Month);

 

intvScale enumeration have two values for months

 1. Month

 2. YearMonth


If we provide the intvScale::Month then X++ ignores the year and assumes that month is calculated within one year.

If we provide the intvScale::YearMonth then X++ calculate the number of months between different years. Consider following example:

static void NumberofMonthsbetweenTwodates(Args _args)

{

date inputDate = str2Date("1/1/2007", 213);

date refDate = str2Date("3/1/2009", 213);

int noOfMonths, noOfMonthsBetweenYears;

;

noOfMonths = intvNo(refDate, inputDate, intvScale::Month);

noOfMonthsBetweenYears = intvNo(refDate, inputDate, intvScale::YearMonth);

//noOfMonths= intvNo(firstdate,seconddate,IntvScale::Month);

print("noOfMonths  :"+int2str(noOfMonths) + "  ,noOfMonthsBetweenYears :"+int2str(noOfMonthsBetweenYears));

}

Info box will be return like:

noOfMonths  :2  ,noOfMonthsBetweenYears  :26

-----------------------------------------------------------------------------------------------

Some cases above code did not works then used following:

 InfAdjValidation_MX::monthDifference(FromDate _fromDate, ToDate _toDate)


Example is as follow.

FromDate _fromDate = mkDate(1,1,2018);

ToDate _toDate =mkDate(31,1,2018);

info(int2str( InfAdjValidation_MX::monthDifference( fromDate, toDate)));


Thank you,

Saturday, November 5, 2022

Clear value of all the controls in form, D365FO - X++

Hello,


Below code sample will help to clear value of all the controls in form. Code tested in D365FO X++.


   void clearFormControls(Object o)

    {

        FormBuildControl    formBuildControl;

        FormStringControl   formStringControl;

        FormInt64Control    formInt64Control ;

        FormIntControl      formIntControl ;

        FormRealControl     formRealControl;

        FormTimeControl     formTimeControl;

        FormCheckBoxControl formCheckBoxControl;

        FormDateControl     formDateControl;

        Date                emptyDate;

        int                 i;

        ;

        for (i=1;i<=o.controlCount(); i++)

        {

            formBuildControl = o.controlNum(i);                                       

            switch (ClassId2Name(classIdGet( element.control(formBuildControl.id()))))

            {

                case 'FormStringControl':

                    formStringControl = element.control(formBuildControl.id());

                    formStringControl.text('');

                    break;

                case 'FormIntControl':

                    formIntControl = element.control(formBuildControl.id());

                    formIntControl.value(0);

                    break;

                case 'FormInt64Control':

                    formInt64Control = element.control(formBuildControl.id());

                    formInt64Control.value(0);

                    break;

                case 'FormRealControl':

                    formRealControl = element.control(formBuildControl.id());

                    formRealControl.realValue(0);

                    break;

                case 'FormTimeControl':

                    formTimeControl = element.control(formBuildControl.id());

                    formTimeControl.value(0);

                    break;

                case 'FormCheckBoxControl':

                    formCheckBoxControl = element.control(formBuildControl.id());

                    formCheckBoxControl.value(0);

                    break;

                case 'FormDateControl':

                    formDateControl = element.control(formBuildControl.id());

                    formDateControl.dateValue(emptyDate);

                    break;

            }

            if (formBuildControl.isContainer())

                clearFormControls(formBuildControl);

        }

    }

Wednesday, October 19, 2022

How to drop SQL Server database currently in use and in Single user mode, MS SQL.

Hi All,


Working in MS SQL, you must have faced below error at least once. 


Issue: How to drop SQL Server database currently in use and in Single user mode.


Error:

"changes to the state or options of database 'AxDB' cannot be made at this time. The database is in single-user mode, and a user is currently connected to it."


Solution:

This built-in stored procedure shows all connections to a database

exec sp_who2


And this query kills a connection to the database, for example connection SPID #53

kill 53


Thank you!

Tuesday, October 18, 2022

Avoiding "Divided by zero" error, X++

Hi,


Two ways to avoid the error "Divided by Zero".


int    a,b,c;

a = 2;

b = 0;

if(b)

    c = a / b;

else

    print "Cannot divide by zero"

//Output is "Cannot divide by zero" because "b" is zero so else statement will run.


(OR)


c  = a / minOne(b); 

//Output is C = 2, reason given below.


Function minOne() returns non zero values. If "b" is zero then it returns one.


Thank you,


Tuesday, October 11, 2022

Add or Minus Days in DateTime or Date Datatype, X++

Hi,

below you will find ways to add/minus days in "DateTime" or "Date" Datatype.


Example 1: Days add/minus in "DateTime" Datatype.

{

 utcDateTime         todayLessOneDay;

// Get the actual UTCDateTime based on the current system

todaysDateTime = DateTimeUtil::utcNow();

// Convert it to a string, just to show in on the info log

info(DateTimeUtil::toStr(todaysDateTime));

// Now less a day

todayLessOneDay = DateTimeUtil::addDays(todaysDateTime, -1);

// And Info it out again

info(DateTimeUtil::toStr(todayLessOneDay));

}


Example 2: Days add/minus in "Date" Datatype.

{

  TransDate transDate = today();

  ;

  transDate++;

  print transDate - 2;

  pause;

}

Sunday, October 9, 2022

Add multiple Query Value to one Query Range - X++

 Hello,

In this post you will find a code sample to run multiple Query Value to one Query Range.


You have two options:


Option 1: Add multiple ranges.

QueryBuildDataSource qbds = q.dataSourceTable(BOMTable);

QueryBuildRange qbr;

while (...)

{

    qbr = qbds.addRange(fieldNum(BOMTable, BOMId));

    qbr.value(queryValue(BOMVersion.BOMId));

}


Option 2: Add multiple values to one range separated by comma.

QueryBuildRange qbr = q.dataSourceTable(BOMTable).addRange(fieldNum(BOMTable, BOMId));

container c;

while (...)

{

    c+= queryValue(BOMVersion.BOMId);

}

qbr.value(con2str(c));

How to execute SQL directly form Dynamics AX X++

How to execute Sql directly form Dynamics AX X++ Reference by : alirazazaidi Dynamics Ax provide many other ways to communicate with databas...