{"id":487,"date":"2018-10-24T16:39:00","date_gmt":"2018-10-24T14:39:00","guid":{"rendered":"http:\/\/daxvisionerp.com\/?p=487"},"modified":"2025-10-27T08:24:00","modified_gmt":"2025-10-27T08:24:00","slug":"chain-of-command-coc-example","status":"publish","type":"post","link":"https:\/\/daxvisionerp.com\/home\/chain-of-command-coc-example\/","title":{"rendered":"Chain of command (CoC) example"},"content":{"rendered":"\n<p>There is a quite good documentation about Chain of command (CoC) here: <a href=\"https:\/\/docs.microsoft.com\/en-us\/dynamics365\/unified-operations\/dev-itpro\/extensibility\/method-wrapping-coc\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.microsoft.com\/en-us\/dynamics365\/unified-operations\/dev-itpro\/extensibility\/method-wrapping-coc<\/a><\/p>\n\n\n\n<p>But this is almost all that I could find about it now, so I have played a bit with it to see clearer and I am sharing here with you what I have experienced.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Chain of command (CoC) example<\/h2>\n\n\n\n<p>CoC is a way of augmentation (new way of class extension) by which you can wrap logic around a method of a base class that you are augmenting. There can be more CoC extensions of the same method in different packages that all reference the package of the base class. The order of execution is random.<br>\u201cThe system <strong>randomly<\/strong> runs one of these methods\u2026 When the call to the next \u2026 method occurs, the system randomly picks another method in the CoC. If no more wrapped methods exist, the system calls the original implementation\u201d<\/p>\n\n\n\n<p>Ok, but I had to try it out on an example. My test was around Global::int642int(..) method.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>I created a new Package that reference Application Platform where Global class sits.<\/li>\n\n\n\n<li>Created a new extension class with CoC on global::int642str where I multiply the value by 5.<br><pre class=\"lang:default decode:true\">[extensionof(classStr(Global))]<br>final static class Global_JADTest_Extension<br>{<br>    static int int642int(int64 _value, boolean _throwIfError)<br>    {<br>        int i = next int642int(_value*5, _throwIfError);<br>        return i;<br>    }<br><br>}<\/pre><br><\/li>\n\n\n\n<li>Executed it with a runnable class and passed value 100<br><pre class=\"lang:default decode:true\">class job1<br>{        <br>    public static void main(Args _args)<br>    {        <br>        info(strFmt('%1',int642int(100)));<br>    }<br><br>}<\/pre><br><\/li>\n\n\n\n<li>Result was 500<br><figure><a href=\"http:\/\/daxvisionerp.com\/wp-content\/uploads\/2018\/10\/1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"694\" height=\"314\" class=\"alignnone size-full wp-image-488\" src=\"http:\/\/daxvisionerp.com\/wp-content\/uploads\/2018\/10\/1.png\" alt=\"\" srcset=\"https:\/\/daxvisionerp.com\/home\/wp-content\/uploads\/2018\/10\/1.png 694w, https:\/\/daxvisionerp.com\/home\/wp-content\/uploads\/2018\/10\/1-300x136.png 300w, https:\/\/daxvisionerp.com\/home\/wp-content\/uploads\/2018\/10\/1-480x217.png 480w\" sizes=\"(max-width: 694px) 100vw, 694px\" \/><\/a><\/figure><\/li>\n\n\n\n<li>Created another CoC in the same new extension package but different class. Here I add 10 to the value. The order matters as we know from primary school mathematics.<br><pre class=\"lang:default decode:true\">[extensionof(classStr(Global))]<br>final static class Global_JADTest2_Extension<br>{<br>    static int int642int(int64 _value, boolean _throwIfError)<br>    {<br>        int i = next int642int(_value+10, _throwIfError);<br>        return i;<br>    }<br><br>}<\/pre><br><\/li>\n\n\n\n<li>Called it 10 times with the job<br><pre class=\"lang:default decode:true\">class job1<br>{        <br>    public static void main(Args _args)<br>    {        <br>        for (int i = 0; i &lt; 10; i++)<br>        {<br>            info(strFmt('%1',int642int(100)));<br>        }<br>    }<br><br>}<\/pre><br><\/li>\n\n\n\n<li>The result was always 550, so first, the add than the multiplier was executed.<br><figure><a href=\"http:\/\/daxvisionerp.com\/wp-content\/uploads\/2018\/10\/2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"487\" height=\"374\" class=\"alignnone size-full wp-image-489\" src=\"http:\/\/daxvisionerp.com\/wp-content\/uploads\/2018\/10\/2.png\" alt=\"\" srcset=\"https:\/\/daxvisionerp.com\/home\/wp-content\/uploads\/2018\/10\/2.png 487w, https:\/\/daxvisionerp.com\/home\/wp-content\/uploads\/2018\/10\/2-300x230.png 300w, https:\/\/daxvisionerp.com\/home\/wp-content\/uploads\/2018\/10\/2-480x369.png 480w\" sizes=\"(max-width: 487px) 100vw, 487px\" \/><\/a><\/figure><\/li>\n\n\n\n<li>I opened another Visual Studio and created another extension model referencing application platform<\/li>\n\n\n\n<li>Added another CoC to the same method. This divides the value by 5.<br><pre class=\"lang:default decode:true\">[extensionof(classStr(Global))]<br>final static class Global_PEPTest_Extension<br>{<br>    static int int642int(int64 _value, boolean _throwIfError)<br>    {<br>        int i = next int642int(_value\/5, _throwIfError);<br>        return i;<br>    }<br><br>}<\/pre><br><\/li>\n\n\n\n<li>The result was always 150. So execution order was ((100\/5)+10)*5.<\/li>\n\n\n\n<li>I added info(&#8216;1&#8217;) as the first line\u00a0of the multiplier extension. info(&#8216;2&#8217;) to the add and info(&#8216;3&#8217;) to the division method. So, the order that I experienced in the last step was 3->2->1.<\/li>\n\n\n\n<li>Went for lunch, executed again and the result was still 150 (3->2->1).<\/li>\n\n\n\n<li>Removed the \u2018for\u2019 loop and added an extra \u201cstart\u201d message to the Infolog<\/li>\n\n\n\n<li>The result changed. New result was 110, the order was Start->2->1->3.<\/li>\n<\/ol>\n\n\n\n<p>So I could see the order of execution changing like they already wrote in the documentation, but it was good to try and now I can see more clearly on how CoC work.<\/p>\n\n\n\n<p>Noticed 2 things:<\/p>\n\n\n\n<p>1.<\/p>\n\n\n\n<p>It is interesting that I had an error when creating the extension class saying the class must be \u201cfinal\u201d, but in the end, I removed the \u2018final\u2019 keyword and it works without that.<\/p>\n\n\n\n<p><a href=\"http:\/\/daxvisionerp.com\/wp-content\/uploads\/2018\/10\/final.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-491\" src=\"http:\/\/daxvisionerp.com\/wp-content\/uploads\/2018\/10\/final.png\" alt=\"\" width=\"875\" height=\"211\" srcset=\"https:\/\/daxvisionerp.com\/home\/wp-content\/uploads\/2018\/10\/final.png 875w, https:\/\/daxvisionerp.com\/home\/wp-content\/uploads\/2018\/10\/final-300x72.png 300w, https:\/\/daxvisionerp.com\/home\/wp-content\/uploads\/2018\/10\/final-768x185.png 768w, https:\/\/daxvisionerp.com\/home\/wp-content\/uploads\/2018\/10\/final-480x116.png 480w\" sizes=\"(max-width: 875px) 100vw, 875px\" \/><\/a><br>2.<\/p>\n\n\n\n<p>Ok, I know they told it is not allowed, but I tried to solve the order of execution issue with extending the extension, but it was not allowed even if I was allowed to remove the \u2018Final\u2019 keyword<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/daxvisionerp.com\/wp-content\/uploads\/2018\/10\/extending-extension.png\"><img loading=\"lazy\" decoding=\"async\" width=\"672\" height=\"228\" src=\"http:\/\/daxvisionerp.com\/wp-content\/uploads\/2018\/10\/extending-extension.png\" alt=\"\" class=\"wp-image-490\" srcset=\"https:\/\/daxvisionerp.com\/home\/wp-content\/uploads\/2018\/10\/extending-extension.png 672w, https:\/\/daxvisionerp.com\/home\/wp-content\/uploads\/2018\/10\/extending-extension-300x102.png 300w, https:\/\/daxvisionerp.com\/home\/wp-content\/uploads\/2018\/10\/extending-extension-480x163.png 480w\" sizes=\"(max-width: 672px) 100vw, 672px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n<div class=\"taxonomy-post_tag wp-block-post-terms\"><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/coc\/\" rel=\"tag\">#CoC<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/msdyn365fo\/\" rel=\"tag\">#MSDyn365FO<\/a><span class=\"wp-block-post-terms__separator\">, <\/span><a href=\"https:\/\/daxvisionerp.com\/home\/tag\/chain-of-command\/\" rel=\"tag\">Chain of command<\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>There is a quite good documentation about Chain of command (CoC) here: https:\/\/docs.microsoft.com\/en-us\/dynamics365\/unified-operations\/dev-itpro\/extensibility\/method-wrapping-coc But this is almost all that I could find about it now, so I have played a bit with it to see clearer and I am sharing here with you what I have experienced. Chain of command (CoC) example CoC is a [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"1080","footnotes":""},"categories":[19],"tags":[27,25,29],"class_list":["post-487","post","type-post","status-publish","format-standard","hentry","category-dynamics-365fo","tag-coc","tag-msdyn365fo","tag-chain-of-command"],"_links":{"self":[{"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/posts\/487","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/comments?post=487"}],"version-history":[{"count":1,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/posts\/487\/revisions"}],"predecessor-version":[{"id":1401,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/posts\/487\/revisions\/1401"}],"wp:attachment":[{"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/media?parent=487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/categories?post=487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daxvisionerp.com\/home\/wp-json\/wp\/v2\/tags?post=487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}