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

Ways to stop code from run by parameter

(0) ShareShare
ReportReport
Posted on by 172

Hi,

could someone share some ways to stop code from running with a parameter?

Actually I always find my parameter and add it at begin to into a condition but when a method need to return something its a bit tricky because return needs to be outside of the condition with code I created.

THere is also a way to throw error but I would like to give a info msg or nothing when the code didnt run.

I have the same question (0)
  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi RadekM,

    Not sure what do you mean 'tricky' with conditions and return statement. If you deactivate some code by parameter, you can always return default value in else statement and show info log. Maybe you can share your code with more descriptions about the issue?

  • RadekM Profile Picture
    172 on at

    Well for example I created an extension and declared the same table so if my code don't run it will give no output data.

    For this actually I need only to insert 4 more fields but without the whole standard code It didn't returned all the data it was at beginning.

    If I run it like this the new i empty ntrastat table will run.

    [ExtensionOf(classStr(IntrastatTransfer))]
    final class IntrastatTransferSZM_Extension
    {
    
        protected Intrastat initIntrastatFromProjInvoiceItem(
            ProjInvoiceJour         _projInvoiceJour,
            ProjInvoiceItem         _projInvoiceItem)
        {
            next initIntrastatFromProjInvoiceItem(_projInvoiceJour, _projInvoiceItem);
            Intrastat                   intrastat;
            LogisticsPostalAddress      sendRecvAddr;
            ServiceParametersSz         serviceparm = ServiceParametersSz::find();
       
            if(curExt() == 'SZM' && serviceparm.AutoFillServiceData == true)
            {
                intrastat.initValue();
    
                intrastat.TransDate         = _projInvoiceJour.InvoiceDate;
                intrastat.AccountNum        = this.calcProjInvoiceJourAccountNum(_projInvoiceJour);
                intrastat.DlvTerm           = _projInvoiceJour.DlvTerm;
                intrastat.DebCredType       = ModuleInventCustVend::Cust;
    
                intrastat.CountryRegionId   = deliveryPostalAddress.CountryRegionId;
                intrastat.State             = deliveryPostalAddress.State;
                intrastat.County            = deliveryPostalAddress.County;
    
                /* Unique reference */
                intrastat.TransTableId      = _projInvoiceItem.TableId;
                intrastat.TransRecId        = _projInvoiceItem.RecId;
    
                intrastat.UpdateNum         = _projInvoiceItem.ProjInvoiceId;
    
                intrastat.Qty               = _projInvoiceItem.IntrastatTransferMap::qty(abs(_projInvoiceItem.Qty), _projInvoiceItem.Salesunit);
    
                this.calcAmountsAndMarkups(_projInvoiceJour, _projInvoiceItem, intrastat);
                intrastat.VatNum            = CustTable::find(intrastat.AccountNum).getPrimaryRegistrationNumber(TaxRegistrationTypesList::TAXID, _projInvoiceJour.InvoiceDate);
    
                this.initDirectionAndOrderType(ModuleCustVend::Cust, intrastat.Qty, intrastat, _projInvoiceItem.TransactionCode, _projInvoiceItem.salesLine().SalesType == SalesType::ReturnItem);
    
                intrastat.Category          = SalesLine::findInventTransId(_projInvoiceItem.InventTransId).SalesCategory;
    
                this.initIntrastatFromTransferMap(intrastat,  _projInvoiceItem);
    
                sendRecvAddr = this.getSendRecvAddress(ModuleCustVend::Cust,
                                                intrastat.OrderType,
                                                _projInvoiceJour.InvoiceAccount,
                                                _projInvoiceJour.DeliveryPostalAddress,
                                                TransportationDocument::find(_projInvoiceJour.TransportationDocument).LoadedPostalAddress);
    
                intrastat.SendReceiveCountryRegionId    = sendRecvAddr.CountryRegionId;
                intrastat.SendReceiveState              = sendRecvAddr.State;
    
                intrastat.TransactionCode         = _projInvoiceitem.TransactionCode;
                intrastat.IntrastatCommodity      = InventTable::find('REKLAMACJA_FM').intrastatCommodity;
                intrastat.TransportMode           = '3';
                intrastat.dlvTerm                 = _projInvoiceJour.DlvTerm;
    
                this.setDirectionCode(intrastat);
            
            }
                return intrastat;
            
        }
    
    }

  • Verified answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi RadekM,

    Have you tried to use the value returned from next statement and fill only fields you need?

    Intrastat intrastat = next initIntrastatFromProjInvoiceItem(_projInvoiceJour, _projInvoiceItem);
    
    if (myCondition)
    {
        //Fill my fields
    }
    
    return intrastat;
            

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

    Indeed you should utilize the return value of the next call, otherwise you will skip all results from the standard method.

  • RadekM Profile Picture
    172 on at

    Wow thanks, I didn't know I can declare it this way.

    But it didn't answer my question 100%.

    Still I would like to know if there is a more 'elegant' way to break code to run.

    Inserting more and more conditions on another isn't looking good.

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

    First of all, you should never use hard coded company names in your code. Instead you should create a parameter which can be set in a company, and then make your condition check that parameter. Otherwise you need a developer if this code should ever be enabled in another company.

    So far I see two conditions in your code: the company check, and check for AutoFillServiceData. I don't see anything unelegant to check these both conditions, if that's your requirement.

  • nmaenpaa Profile Picture
    101,162 Moderator on at

    Moved to D365 Finance forum since the question is not about AX.

  • Sergei Minozhenko Profile Picture
    23,093 on at

    Hi RadekM,

    I would say in CoC, conditions by parameters are probably the only way to bring flexible solutions.

    In other cases, class inheritance could be used (if it's possible)

  • RadekM Profile Picture
    172 on at

    As for now the company check is just to make sure it won't run in other company where are gobal test running.

    We got like 10 companies but only one started D365 for now all others will run 2021.

    This is just a simple example, in bigger modifications can be more conditions. Even now I have a small code that calculate prices based on multiple conditions using 4 or 5 ifs in row isn't really nice.

    Actually I use mostly CoC extensions but if it's like Sergei said there is no better way for it.

    It would be nice if even if I could just do it like this.

    if(mycondition = false)
    {
     break;
    }
    
    //rest of my code that run if it dont hit the break

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

    Instead of break, you can "return" if your condition is false. But I think it's cleaner if you only check "if (myCondition == true)" and run your code if it's true. Otherwise don't run anything.

    You can also move your code completely out from the CoC class and just call it from the CoC class. This way the code is more reusable if you need to call it from other processes now or in the future.

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 451 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans