Showing posts with label dynamics365. Show all posts
Showing posts with label dynamics365. Show all posts

Tuesday, 8 August 2017

Create Custom Number sequence in Dynamics 365 for operations

In AX2012 or AX 2009 we directly write code on existing number sequence class​​ but in AX (7) new version need to create new class and required extend to NumberSeqApplicationModule and required delegate method for mapping.

1. Create EDT ChangeId.
2. Add EDT to Table ChangeLog.
3. Create new Table method numRefChangeId().
public class ChangeLog extends common
{
   static NumberSequenceReference numRefChangeId()
    {
        return NumberSeqReference::findReference(extendedTypeNum(ChangeId));
    }
}
​4. Create New Class NumberSeqModuleRisk and extend NumberSeqApplicationModule​ and a delegate method.
Picture
lass NumberSeqModuleRisk extends NumberSeqApplicationModule
{
    public void initializeReference(NumberSequenceReference _reference,
         NumberSeqDatatype _datatype, NumberSeqScope _scope)
    {
        #ISOCountryRegionCodes
        super(_reference, _datatype, _scope);

        switch (_datatype.parmDatatypeId())
        {
            case extendedTypeNum(ChangeId):
         
                if("USA")
                {
                    _reference.AllowSameAs = true;
                }
               /*if (SysCountryRegionCode::isLegalEntityInCountryRegion([#isoIT]))
               {
                   _reference.AllowSameAs = true;
               }*/
        }
    }

    protected void loadModule()
    {
        NumberSeqDatatype datatype = NumberSeqDatatype::construct();

        datatype.parmDatatypeId(extendedTypeNum(ChangeId));
        datatype.parmReferenceHelp(literalStr("Creating new ChangeId"));
        datatype.parmWizardIsManual(NoYes::No);
        datatype.parmWizardIsChangeDownAllowed(NoYes::No);
        datatype.parmWizardIsChangeUpAllowed(NoYes::No);
        datatype.parmWizardHighest(999999);
        datatype.parmSortField(1);

        datatype.addParameterType(NumberSeqParameterType::DataArea, true, false);
        this.create(datatype);   

    }

    public NumberSeqModule numberSeqModule()
    {
        return NumberSeqModule::Proj;
    }

    [SubscribesTo(classstr(NumberSeqGlobal),delegatestr(NumberSeqGlobal,buildModulesMapDelegate))]
    static void buildModulesMapSubsciber(Map numberSeqModuleNamesMap)
    {
        NumberSeqGlobal::addModuleToMap(classnum(NumberSeqModuleRisk), numberSeqModuleNamesMap);
    }
}

5. Write a job and run that
static void ChangeId(Args _args)
{
    NumberSeqModuleRisk numberSeqModuleRisk = new NumberSeqModuleRisk ();
    numberSeqModuleRisk .load();
}
6. Then run the wizard   Organization Administration -> CommonForms -> Numbersequences -> Numbersequences -> Generate -> run the wizard.

GO to Project management module -> setup -> Project management parameters form -> Num Seq
Picture
​7. Now create methods as shown on Form below.
Picture
[Form]
public class ChangeLog extends FormRun
{
    NumberSeqFormHandler numberSeqFormHandler;
    /// <summary>
    ///
    /// </summary>
    NumberSeqFormHandler numberSeqFormHandler()
    {
        if (!numberSeqFormHandler)
        {
numberSeqFormHandler=NumberSeqFormHandler::newForm(ChangeLog::numRefChangeId().NumberSequenceId,  element, ChangeLog_DS,   fieldNum(ChangeLog, ChangeId));
        }
        return numberSeqFormHandler;
    }
    public void close()
    {
        if (numberSeqFormHandler)
        {
            numberSeqFormHandler.formMethodClose();
        }
        super();
    }

    [DataSource]
    class ChangeLog
    {
        public void create(boolean _append = false,
                           boolean _extern = false)
        {
            element.numberSeqFormHandler().formMethodDataSourceCreatePre();

            super(_append);
            if (!_extern)
            {
                element.numberSeqFormHandler().formMethodDataSourceCreate(true);
            }

        public void write()
        {
            super();
            element.numberSeqFormHandler().formMethodDataSourceWrite();
        }
        public boolean validateWrite()
        {
            boolean         ret;
            ret = super();
            ret = element.numberSeqFormHandler().formMethodDataSourceValidateWrite(ret) && ret;
            if (ret)
            {
                ChangeLog.validateWrite();
            }
            return ret;
        }
        public void linkActive()
        {
            element.numberSeqFormHandler().formMethodDataSourceLinkActive();
            super();
        }

        public void delete()
        {
            element.numberSeqFormHandler().formMethodDataSourceDelete();
            super();
        }
    }
}

Now our custom number sequence is generated. Open your Form and check Num Seq by creating new record on your filed.

​ Reference: https://community.dynamics.com/ax/b/daxseed/archive/2016/11/17/ax-7-custom-number-sequence

Debugging code in Dynamics 365 for Operations

This topic reviews how you can debug X++ code by using the debugging feature in Microsoft Visual Studio.
To debug X++ code, you use the debugger in Microsoft Visual Studio. The process is similar to the process that is used for any other application that is created in Visual Studio. For example, the standard tools for examining the application are available when your code is stopped at a breakpoint.

Debug your code

To debug X++ code, follow these steps.
  1. In Visual Studio, open the X++ code to debug.
  2. Find the line or lines where you want execution to stop, and set breakpoints in those lines. To set a breakpoint in a line, click in the left column of the code editor or press F9 while the cursor is on that line. A red dot indicates that a breakpoint has been set.
    Red dot
  3. Set a startup project and a startup object. Startup objects can be any form, any class that has the main method, or any menu item. You can set the startup object in the Properties pane for the project. Alternatively, right-click the element in Solution Explorer, and then click Set as Startup Object.
    Set as startup object
  4. On the Debug menu, click Start Debugging.
  5. In the application, perform the action that causes the code that you're interested in to run. Typical actions include opening a form. Processing stops at the breakpoints that you set.
    Run
  6. Use the tools in Visual Studio to examine the application. For example, you can hover over variables in the X++ code to see their values. You can also use commands on the Debug menu to step through the code. Additionally, tools such as the Autos pane in Visual Studio will show important information about the state of the application.
    Hover
    Another tool that is specific to Finance and Operations is the Infolog. Often, info()statements are added to code to log status messages while the application is running. You can view these Infolog messages directly in Visual Studio. On the View menu, click Infolog.
    Infolog
  7. After you've finished debugging the application, exit Finance and Operations . Visual Studio will exit debugging mode.

Display method in Dynamics 365 for operations

Hi All,
This posts helps you to understand and create a "display"  method for the table extension.
Lets say, the requirement is to add a method in the standard table, it can be achieved either by creating a table extension.
So in this scenario we had a requirement to add a display method in the standard table "CustTrans".
In Dynamics 365 we wont be able to add the new method or modify the existing method to the standard table or to an extension table.
It can be achieved by using the extension class.
Step 1: Create a new class and name it as <Classname>_<Extension>.
<Class-name> - can be any name, but it is preferred to give the table name for which the extension is being created. 
postfix <_Extension> is must.
public static class CustTrans_Extension
{
}
Step 2 : Now add the display methods in the class which is required to be shown.
public static class CustTrans_Extension
{
[SysClientCacheDataMethodAttribute(true)]
public static display AgreementId agreementId(CustTrans _this)
{
LedgerJournalTrans ledgerJournalTrans;
select ledgerJournalTrans
where ledgerJournalTrans.TransactionType == LedgerTransType::Payment &&
LedgerJournalTrans.CustTransId == _this.RecId;

return ledgerJournalTrans.AgreementId;
}
}
Step 3: To use this display method in the form.
Create a string control in the form design and set the following properties
Data source: CustTrans
DataMethod: CustTrans_Extension::agreementId
CacheDataMethod: Yes
Below is the screen shot for reference.
Step 4: Build/Rebuild the project/solution and check the output in the URL.

Thursday, 3 August 2017

Number sequence in Dynamics 365









                                                1.Create a new  EDT string: StudentID






2.  Create a new Table : MK_Seq

3.  Add EDT and fields into the  Table: MK_Seq





4.Create Form MK_Seq








5.Create a new method (numberSeq) for Table MK_Seq


Write the below code in that method
                                                            public class MK_NumSeq extends common
{
                                        /// <summary>
                 ///
    /// </summary>
    static NumberSequenceReference numberSeq()
    {
        return NumberSeqReference::findReference(extendedTypeNum(StudentID));
    }

}


6.Create a new class (numseqStudent) and make extends with NumberSeqApplicationModule


class numseqStudent extends NumberSeqApplicationModule
{
    public void initializeReference(NumberSequenceReference _reference,
         NumberSeqDatatype _datatype, NumberSeqScope _scope)
    {
        #ISOCountryRegionCodes
        super(_reference, _datatype, _scope);
        switch (_datatype.parmDatatypeId())
        {
            case extendedTypeNum(StudentID):
           if("USA")
                {
                    _reference.AllowSameAs = true;
                }
        }
    }
    protected void loadModule()
    {
        NumberSeqDatatype datatype = NumberSeqDatatype::construct();
        datatype.parmDatatypeId(extendedTypeNum(StudentID));
        datatype.parmReferenceHelp(literalStr("@studentlable:StudentID"));
        datatype.parmWizardIsManual(NoYes::No);
        datatype.parmWizardIsChangeDownAllowed(NoYes::No);
        datatype.parmWizardIsChangeUpAllowed(NoYes::No);
        datatype.parmWizardHighest(999999);
        datatype.parmSortField(1);
        datatype.addParameterType(NumberSeqParameterType::DataArea, true, false);
        this.create(datatype);
    }
    public NumberSeqModule numberSeqModule()
    {
        return NumberSeqModule::Vend;
    }
    [SubscribesTo(classstr(NumberSeqGlobal),delegatestr(NumberSeqGlobal,buildModulesMapDelegate))]
    static void buildModulesMapSubsciber(Map numberSeqModuleNamesMap)
    {
        NumberSeqGlobal::addModuleToMap(classnum(numseqStudent), numberSeqModuleNamesMap);
    }

}


7.Write a job and run that

class Studentjob
{       
   
    public static void main(Args _args)
    {  
        numseqStudent numberSeqModuleRisk = new numseqStudent ();
            numberSeqModuleRisk .load();
    }
}



8.Then run the wizard   Organization Administration -> CommonForms -> Numbersequences -> Numbersequences -> Generate -> run the wizard.





9.Go to Account Payable-> setup -> Account Payable parameters form -> Num Seq.




10. Now create methods as shown on Form below



11.Write below code in  form method code
    

[Form]
public class MK_Seq extends FormRun
{
    NumberSeqFormHandler numberSeqFormHandler;
    NumberSeqFormHandler numberSeqFormHandler()
    {
        if (!numberSeqFormHandler)
        {
numberSeqFormHandler=NumberSeqFormHandler::newForm(MK_NumSeq::numberSeq().NumberSequenceId,  element,MK_NumSeq_ds , fieldNum(MK_NumSeq, StudentID));
        }
        return numberSeqFormHandler;
    }

    public void close()
    {
        if (numberSeqFormHandler)
        {
            numberSeqFormHandler.formMethodClose();
        }
        super();
 }




12.Write below code in form datasource method code.

class MK_NumSeq
    {
      
        public void create(boolean _append = falseboolean _extern = false)
        {
            element.numberSeqFormHandler().formMethodDataSourceCreatePre();
            super(_append);
            if (!_extern)
            {
                element.numberSeqFormHandler().formMethodDataSourceCreate(true);
            }
        }

        public void write()
        {
            super();
            element.numberSeqFormHandler().formMethodDataSourceWrite();
        }

        public boolean validateWrite()
        {
            boolean         ret;
            ret = super();
            ret = element.numberSeqFormHandler().formMethodDataSourceValidateWrite(ret) && ret;
            if (ret)
            {
                MK_NumSeq.validateWrite();
            }
            return ret;
        }

        public void linkActive()
        {
            element.numberSeqFormHandler().formMethodDataSourceLinkActive();
            super();
        }

        public void delete()
        {
            element.numberSeqFormHandler().formMethodDataSourceDelete();
            super();
        }

    }


13.Now our custom number sequence is generated. Open your Form and check Num Seq by creating new record on your filed.