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 :
Finance | Project Operations, Human Resources, ...
Answered

Correct COC sequence

(0) ShareShare
ReportReport
Posted on by 313

Hi everyone,

I want to add a few lines to a method of a class and normally, this is simple enough and I know how to do it.  But this one example I'm unsure how to correctly achieve what I want.

Let's say my class is named ClassA

The method in it I want to add to is

protected void updateStatus()
    {
        MapEnumerator       me;
        ReqTrans            reqTransMark;
        ReqPO               reqPO;

        this.mapReqTransMark();

        if (   mapReqTransMark
            && mapReqTransMark.elements())
        {
            me = mapReqTransMark.getEnumerator();

            while (me.moveNext())
            {
                reqTransMark = me.currentValue();

                reqPO = reqTransMark.reqPo(true);

                setPrefix(#prefixField(reqPo,refId));

                if (!reqPO.RecId)
                {
                    throw error(strFmt("@SYS24774",reqTransMark.RefId));
                }

                if (!ReqTrans::refTypeIsPlannedOrder(reqPO.RefType))
                {
                    throw error("@SYS69936");
                }

                reqPO.ReqPOStatus = ReqPOStatus::Approved;
                reqPO.update();

                mapReqPoUpdated.insert(reqPO.RecId,reqPO);
            }
        }
    }

I want to add 2 lines right after reqPO.ReqPOStatus = ReqPOStatus::Approved;

But if I create an extension class of this class, then call this method, whether I put code before or after the "next", I am not inside that while loop so it doesn't function correctly.

What is the best practice of achieving something like this?

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    You can't add code in the middle of a standard method via Chain of Command. You can only run code before/after standard methods.

    So you can't achieve this requirement.

    However I'm sure that your actual business requirement is not adding two lines of code in this place. And most likely there's a way to solve your business requirement with Chain of Command. If you'd like help with your business requirement, could you share it with us? Thanks!

  • Andrew Huisman Profile Picture
    313 on at

    I am trying to record who approved our Planned Order and at what time.  I've created two fields for this and put them in the ReqPO table.  Now I just need to fill it when that Planned Order gets approved.  I was picking this spot because this is where the code was put in AX2012 for this same requirement.  I'm open to other ideas of course :)

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Thanks for the information. Where would you fetch the information to these two fields? Is it just the current user of the D365 session?

    If yes, maybe you can hook in ReqPo.update(), and populate your fields if the status changed to Approved in this update.

    I can also see that each updated ReqPO is added to mapReqPoUpdated, which you should be able to access in your CoC logic after the updateStatus was run.

  • Andrew Huisman Profile Picture
    313 on at

    Hello Nikolaos,

    I really liked your suggestion of expanding on the update of the table.  I made an extension and I can't see to get it to work.

    Here is my code

    public void update(

           ReqPlanData         _reqPlanData,

           ReqTrans            _reqTrans,

           InventLocationId    _fromInventLocationId,

           boolean             _noExplosion,

           ReqTraceLogger      _reqTraceLogger,

           boolean             _scheduleOrder)

       {

           ReqPO                   reqPo_orig;

           reqPo_orig      = this.orig();

           next update(_reqPlanData, _reqTrans, _fromInventLocationId, _noExplosion, _reqTraceLogger, _scheduleOrder);

           ttsbegin;

           if (reqPo_orig.ReqPOStatus == ReqPOStatus::Approved)

           {

               reqPo_orig.TCI_ReqApproveDateTime = DateTimeUtil::getSystemDateTime();

               reqPo_orig.TCI_ReqApproveUserId = curUserId();

               reqPo_orig.update();

           }

           ttscommit;

       }

    But when it comes to the if statement, the status is not approved yet.  If I try to approve a record that is already approved, then it does go in this If statement but then gives me an error.  I think I can't update a record that has been approved.

    Anyway, I feel like I'm not doing something correctly.  What am I missing?

  • Verified answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    First of all, you must compare reqPo_orig.ReqPOStatus (original value) and this.ReqPOStatus (the new status that is going to be updated).

    Second, you must run your logic before calling next, and set the values to your fields. You don't need to call update() since update is already happening. Just set your fields, call next and you're good to go.

    Something like this:

    public void update(
           ReqPlanData         _reqPlanData,
           ReqTrans            _reqTrans,
           InventLocationId    _fromInventLocationId,
           boolean             _noExplosion,
           ReqTraceLogger      _reqTraceLogger,
           boolean             _scheduleOrder)
    
       {
           ReqPO reqPo_orig = this.orig();
           
           if (this.ReqPOStatus == ReqPOStatus::Approved
                && reqPO_orig.ReqPOStatus != ReqPOStatus::Approved)
            {
                this.TCI_ReqApproveDateTime = DateTimeUtil::getSystemDateTime();
                this.TCI_ReqApproveUserId = curUserId();
            }
    
           next update(_reqPlanData, _reqTrans, _fromInventLocationId, _noExplosion, _reqTraceLogger, _scheduleOrder);
       }

  • Andrew Huisman Profile Picture
    313 on at

    You rock Nikolaos!  Thank you so much!  Can you just let me know how you put in a code snippet in a reply?  I had the option on the OP but not in a reply box.

  • Andrew Huisman Profile Picture
    313 on at

    Nikolaos,

    Can you also please look at my new post

    community.dynamics.com/.../update-table-on-insert-when-no-insert-method

    It's a continuation of this project and I assume it's very similiar

  • nmaenpaa Profile Picture
    101,160 Moderator on at

    You can post a code snippet by clicking "Use rich text formatting" - "Insert - > Code".

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans