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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Condition put in CoC in D365FO

(3) ShareShare
ReportReport
Posted on by 198
Dear team,
 
Can we put condition in CoC? I mean if condition is true then only CoC will execute else not 
 
If not possible then if any workaround is there?
 
Please give me the thoughts. thanks!
Categories:
I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    301,877 Super User 2025 Season 2 on at
    Hi,
     
    A small clarification. Your CoC coding will be always executed. The next() call is mandatory. You can add coding before and after the next() command.
     
    You now have conditions in your X++ coding. The condition will be evaluated and will be true or false. So, in your example, if the customer account number is CUST001, then you will get your info message, whereas for any other customer, the message is skipped.
     
    If this is what you meant by a condition, then this is not useless. In many cases, I used such condition in X++ coding on CoC methods. I think that is also what Anton wanted to say in the message just after your code example.
  • Anton Venter Profile Picture
    20,410 Super User 2025 Season 2 on at
    Not sure if I follow you and I might be on a wrong track here. That depends on the code your CoC code. If you are worried about performance, the fact the CoC code is called by the system does not really add much performance overhead on it's own provided there is no database query / update etc. or other time consuming code like calling a web service etc. So, if you have a CoC with a condition that only executes your logic when necessary, it does not really add noticeable overhead.
     
    What adds performance overhead is querying or updating the database, or calling other things like web services etc..
  • CU10121822-0 Profile Picture
    198 on at
    Thanks Anton,
     
    Then put the condition in CoC, will be use less.
    Am I correct ?
  • Anton Venter Profile Picture
    20,410 Super User 2025 Season 2 on at
    Yes, once created / defined, the CoC will always be called by the system. Adding conditions in the your extension code is probably the solution you are after.
  • CU10121822-0 Profile Picture
    198 on at
    Hi, I tried in below way:
     
    [ExtensionOf(classStr(SalesTable))]
    final class SalesTable_Extension
    {
        public void update()
        {
            try
            {
                // Pre-logic with condition
                if (this.CustAccount == 'CUST001')
                {
                    info("Before update: Special customer logic");
                }
                
                next update();
    
                // Post-logic with condition
                if (this.SalesStatus == SalesStatus::Invoiced)
                {
                    info("After update: Sales order invoiced");
                }
            }
            catch (Exception::Error)
            {
                error("Error occurred while updating SalesTable");
                throw; // always rethrow in CoC
            }
        }
    }
     
    So, here what I understood, the condition is returned TRUE or False, it's doesn't matter, CoC will execute always.
     
    am I correct?
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    26,687 Super User 2025 Season 2 on at
     
    It is not recommended to ignore the call to next, but you can do so with an exception !
     
    Best regards,
  • Suggested answer
    Martin Dráb Profile Picture
    238,316 Most Valuable Professional on at
    No, it's not possible. CoC means that you create an extension class and extend a method; there is no way how to make the existence of a class or a method conditional. It's either there or not.
     
    But you can put conditions to the method code.
     
    Maybe you mean whether you can conditionally call the base method (with the next keyword). It's not normally possible, unless the base method is explicitly marked as replaceable.

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

News and Announcements

Season of Giving Solutions is Here!

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
Abhilash Warrier Profile Picture

Abhilash Warrier 843 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

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

#3
Martin Dráb Profile Picture

Martin Dráb 338 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans