web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

AX 2012 Change code of Method in X++

(1) ShareShare
ReportReport
Posted on by 550

Hello,

I try to Change a Class Method in X++ code. I write the following code:

 

    #AOT
    ClassName       className = classStr(Class1); 
    TreeNode        classLayerVersionTreeNode, classLayerVersion;
    Source          layerVersion;
    date            now;
    ;

    classLayerVersion = TreeNode::findNode(strFmt(@"%1\%2",#ClassesPath, className));
    classLayerVersion.AOTrestore(true);

    classLayerVersionTreeNode = TreeNode::findNode(strFmt(@"%1\%2\%3",#ClassesPath, className, #MethodName));
    classLayerVersionTreeNode.AOTrestore(true);

    layerVersion = strFmt("static str layerVersion() \r\n { \r\n return '%1-%2'; \r\n }",strUpr(strFmt("%1",Global::currentAOLayer())), date2str(today(),321,DateDay::Digits2,DateSeparator::None,DateMonth::Digits2,DateSeparator::None,DateYear::Digits4));


    classLayerVersionTreeNode.AOTsetSource(layerVersion, false);    
    classLayerVersionTreeNode.AOTsave();
    classLayerVersionTreeNode.AOTcompile();


    classLayerVersion = TreeNode::findNode(strFmt(@"%1\%2",#ClassesPath, className));    
    classLayerVersion.AOTsave();    
    classLayerVersion.AOTcompile();


This code works fine while i call it from a Job in AX Client and Change the Method in VAR Layer as expected.

 

Now I want to call this method to Change the Code within a ConsoleApplication (in Future a Windows Forms application) with this code:

 

        static void Main(string[] args)
        {
            proxy.Axapta DynAx = new proxy.Axapta();
            string info = "N/A";
            
            try
            {
                DynAx.Logon(null, null, null, @"D:\Config2012\DAX2012R3EntwVAR.axc");
                
                Console.WriteLine("Verbunden");

                info = (string)DynAx.CallStaticClassMethod("Class", "changeCode");

                Console.WriteLine(info);

                DynAx.Logoff();
                Console.WriteLine("beendet");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
            
        }


The Result of this is, the code will Change, but it was changed in USR - Layer, not in VAR layer. The Config File for Logon ist specifed for VAR-Layer and within the AX-Code at Position ... strFmt("%1",Global::currentAOLayer()... knows the correct - VAR - Layer. So why are the Changes for the Code at USR - Layer after I run the ConsoleApplication?

*This post is locked for comments

I have the same question (0)
  • Douglas Noel Profile Picture
    3,905 on at

    Hi Stefan,

    sounds really funny and interesting. What version does your ax bconn have exactly?

    I'm going to try this in the newest version the next time and keep you informed if I can reproduce this.

    sorry - no idea at the moment.

    maybe it's a bug/feature or a 'by design'

    regards

    Douglas

  • Stefan Wojtas Profile Picture
    550 on at

    Hi Douglas,

    I use AX 2012 R3, Kernel 6.3.164.0

  • Anton Venter Profile Picture
    20,345 Super User 2025 Season 2 on at

    Hi Stefan, have you configured the business connector configuration to the VAR layer?

  • Stefan Wojtas Profile Picture
    550 on at

    Hi,

    yes, the Config is correct and while runnig the Console Application I get the VAR-Layer Information by returning Global::currentAOLayer(). So the BC running at the correct Layer. I hope th BC knows that :D

  • Verified answer
    Carsten Cors Profile Picture
    120 on at

    I've got a different approach, you could delete the class to be changed and then re-create with the changes by code. See the following listing.

    static void MakeClass(str _versionString)
    {
        ClassBuild  classBuild;
        DictClass   dictClass;
        str sourceStr  = strfmt(@"static str layerVersion()
                               {
                                    return '%1';
                                }", _versionString);
        ;
         
        classBuild = new ClassBuild("Class", true);
    
        classBuild.addMethod("layerVersion", sourceStr);
        classBuild.addMethod("getLayerVersion", 'static str getLayerVersion(UtilEntryLevel _layer)\n{\n}');
        classBuild.addSourceToMethod("getLayerVersion", @"
             str version;
             UtilElements utilElements;
             str sourceFromLayer;
             ;
             SELECT FIRSTONLY utilElements
                WHERE utilElements.RecordType == UtilElementType::ClassStaticMethod &&
                utilElements.ParentId == classnum(Class) &&
                utilElements.Name == staticmethodstr(Class, layerVersion) &&
                utilElements.UtilLevel == _layer;
    
            if (utilElements)
            {
                 try
                {
                    sourceFromLayer = conpeek(utilElements.Source, 1);
                    version = runbuf(sourceFromLayer);
                }
                catch
                {
                    exceptionTextFallThrough();
                }
            }
    
            return version;");
    
        classBuild.classNode().AOTcompile();
    }
    


    I hope I could help a little

  • Douglas Noel Profile Picture
    3,905 on at

    Hi Stefan,

    I will give you a short update.

    I can confirm your decribed behavoior even with R3 binaries 6.3.1000.390.

    It's always written into the USR Layer even if you try to change every configuration in the system (the condig from the aos to) with an aol=USP.

    Even more strange is the follwing (maybe this has to do something with it)

    I'm only able to reproduce this if calling the static method run on  server.

    The call of the same method (defined running on client) led to an System.Runtime.InteropServices.SEHException   External componet has thrown an exception

    I've checked this with an R2 >= CU8 6.2.2000.131

    Here the method can be called running on both tiers and does execatly what you expected.

    So something must have been changed 'deep dark' in the R3 release. I've meanwhile detected more of such "you shouldn't do such things" hidden features'.

    regards

    Douglas

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Liquid error: parsing "/globalsearch/?q=%2F.++6" - Nested quantifier +.