Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

Next call on Chain of Command

Posted on by 71
Hi everyone,
I'll try to explain briefly my goal. My code should display a NoYes pop up when I click on a butto and a certain condition happens, If I press Yes the code keep going, if I press No it stops. I used a Chain of Command class, as an axtension of the  button on my form, This is my code:
[ExtensionOf(formControlStr(VendEditInvoice, OK))]final class VendEditInvoice_OK_AzimutBenetti_Extension{    public void clicked()    {        IntentLetter_IT intentLetter;        VendInvoiceInfoLine vendInvoiceInfoLine;                if(vendInvoiceInfoLine.LineAmount > intentLetter.remainAmountMST())        {            DialogButton diagBut;            str strMessage = /Importo da decurtare al saldo della lettera di intento maggiore dell’importo residuo. Proseguire?/;            str strTitle = /Splafonamento/;            diagBut = Box::yesNo(                    strMessage,                    DialogButton::No,                    strTitle                    );            if(diagBut == DialogButton::No)            {                return;            }        }        next clicked();    }}

But I get this error once I build the project: call to 'next' should be done only once and unconditionally.
I assume that's because if I press No, the code returns and the method next is never called, because if I delete Return and put for example an info, everything works fine. How Can i do it in a different way that works? 
Thank you very much.
  • Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    Next call on Chain of Command
    Please use the debugger to find out what's going on in your code. This forum is not a replacement of the debugger.
     
    I think you'll find that your code gets called. The problem is that you didn't test your code and therefore you don't know that it doesn't do anything. You check whether purchTable.IntentLetterId_IT has a value, but it can't have any, because you forgot populate purchTable variable. And if this worked, if would fail a bit later because totalAmount is always empty too.
  • FabFab97 Profile Picture
    FabFab97 71 on at
    Next call on Chain of Command
     
    i don't know but when I clikc on the PostButton, nothing happens. It only happens the logic of the button, but nothing about my code. When I was using the extensions of the button at least the popup used to appear
  • Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    Next call on Chain of Command
    You said that "it should start when I Click on the button PostJournal", but that's happening, isn't it? You click the Post button (the label of the OK button you used in your code is Post, not PostJournal), which calls canProceedWithInvoicePosting() before posting.
     
    If you look into code, you'll see that OK.clicked() calls postInvoice(), which calls canProceedWithInvoicePosting(). Therefore canProceedWithInvoicePosting() is indeed called from OK.clicked().
     
    So... what is the actual problem?
  • FabFab97 Profile Picture
    FabFab97 71 on at
    Next call on Chain of Command
    Thank you very much, I really appreciate your help and I'm sorry that my low knowledge can be annoying.
    I wrote this code now 
    [ExtensionOf(formStr(VendEditInvoice))]
    final class VendEditInvoice_AzimutBenetti_Extension
    {
    
        protected boolean canProceedWithInvoicePosting()
        {
            boolean ret = next canProceedWithInvoicePosting();
            
            if (!this.validateIntentLetter())
            {
                ret = false;
            }
           
            return ret;
        }
    
        //public void clicked()
        //{
        //    boolean proceed = this.validateIntentLetter();
    
    
        //    if (proceed)
        //    {
    
        //       super();
        //    }
    
    
        //}
    
        // A new method for your validation logic
        private boolean validateIntentLetter()
        {
            VendInvoiceInfoLine vendInvoiceInfoLine; // Ottieni la fattura corrente
            PurchTable purchTable;
            
            Amount totalAmount = vendInvoiceInfoLine.LineAmount;
    
            // Ottenere l'ID della lettera di intento dalla tabella di lookup
            if (purchTable.IntentLetterId_IT)
            {
                IntentLetter_IT intentLetter;
    
                // Recuperare la lettera di intento associata all'ID
                select firstOnly intentLetter
                    join purchTable
                    where intentLetter.IntentLetterId == purchTable.IntentLetterId_IT;
    
                // Ottenere l'ID della lettera di intento
                Amount remainingAmount = intentLetter.remainAmountMST();
    
                Info(strFmt('%1, %2', totalAmount, remainingAmount));
    
                if (totalAmount >= remainingAmount)
                {
                    DialogButton diagBut;
                    str strMessage = "Importo da decurtare al saldo della lettera di intento maggiore dell’importo residuo. Proseguire?";
                    str strTitle = "Splafonamento";
                    diagBut = Box::yesNo(
                        strMessage,
                        DialogButton::No,
                        strTitle
                    );
                    if (diagBut == DialogButton::No)
                    {
                        info('Operazione annullata');
                        return false;
                    }
                   
                }
            }
    
            return true;
        }
    }
    
    The problem I have now is that this logic (the pop up with the buttons) should start when I Click on the button PostJournal, that was define in the previuos extensione I made. How can I implement that?
    This is the code of the button class I found in the code of the form VendEditInvoice: 
     [Control("Button")]
        class OK
        {
            public void clicked()
            {
                // <GEERU>
                if (SysCountryRegionCode::isLegalEntityInCountryRegion([ #isoRU ]) && !purchFormLetter.checkBeforePost())
                {
                    return;
                }
                // </GEERU>
    
                element.postInvoice();
            }
    
        }
     
  • Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    Next call on Chain of Command
    Well, that's not the code I meant. You're not extending canProceedWithInvoicePosting() method of the form at all; you kept all the code in clicked() and added two more methods to the buttton (!), which has absolutely no impact on the behaviors of canProceedWithInvoicePosting() on the form.
     
    Delete your form control extension and create an extension class for the form. There create a CoC extension of canProceedWithInvoicePosting() and implement your logic below next canProceedWithInvoicePosting().
     
    For example:
    [ExtensionOf(formStr(VendEditInvoice))]
    final class VendEditInvoice_AzimutBenetti_Extension
    {
        // CoC extension
        protected boolean canProceedWithInvoicePosting()
        {
            boolean ret = next canProceedWithInvoicePosting();
            
            if (!this.validateIntentLetter())
            {
                ret = false;
            }
            
            return ret;
        }
    
        // A new method for your validation logic
        private boolean validateIntentLetter()
        {
            ...
        }
    }
    By the way, are you sure that you want to iterate all records in VendInvoiceInfoLine? It sounds like a bug to me; I'm assuming you want just lines of the invoice currently being processed. If you really want load all records from database, putting such logic to a form sounds like a bad idea. I would more to the actual posting, so it can run in batch.
     
    Another bug is that you're looking for a record in IntentLetter_IT that has the same RecId as a record of VendInvoiceInfoLine, which makes no sense. It seems that you never debugged your code at all, otherwise you'd notice that this select statement never returns expected data. You'll make your work much easier if you get to used to testing your code in small pieces before adding them together.
     
    By the way, I would never consider putting such business logic to clicked(), neither should you. It's not GUI logic, clicked() says nothing about the purpose of the code (logic should be in methods with descriptive names) and it can't be reused.
  • FabFab97 Profile Picture
    FabFab97 71 on at
    Next call on Chain of Command
    Sorry, here is my code

    [ExtensionOf(formControlStr(VendEditInvoice, OK))]
    final class VendEditInvoice_OK_AzimutBenetti_Extension
    {
        private boolean canProceed = true;
     
        public void clicked()
        {
            IntentLetter_IT intentLetter;
            VendInvoiceInfoLine vendInvoiceInfoLine;
     
            Amount totalAmount;
            Amount remainingAmount;
            RecId intentLetterRecId;
     
            // Ciclo attraverso tutte le fatture
            while select * from vendInvoiceInfoLine
            {
                totalAmount = vendInvoiceInfoLine.LineAmount;
                intentLetterRecId = vendInvoiceInfoLine.RecId;
     
                select firstOnly intentLetter
                    where intentLetter.RecId == intentLetterRecId;
     
                remainingAmount = intentLetter.remainAmountMST();
     
                Info(strFmt('%1, %2', totalAmount, remainingAmount));
     
                if (totalAmount >= remainingAmount)
                {
                    DialogButton diagBut;
                    str strMessage = "Importo da decurtare al saldo della lettera di intento maggiore dell’importo residuo. Proseguire?";
                    str strTitle = "Splafonamento";
                    diagBut = Box::yesNo(
                        strMessage,
                        DialogButton::No,
                        strTitle
                    );
                    if (diagBut == DialogButton::No)
                    {
                        canProceed = false;
                        info('Operazione annullata');
                    }
                }
            }
            next clicked();
        }
     
        public boolean canProceedWithInvoicePosting()
        {
            return canProceed;
        }
     
        public void setCanProceed(boolean _canProceed)
        {
            canProceed = _canProceed;
        }
    }
     
    I can't access to the Vm right now so I can't debug, if you still need it I'll do it tomorrow Asap.
    Thank you!
  • Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    Next call on Chain of Command
    Unfortunately I see no code in your reply; please add it.
     
    Also, please tell where the thrown from. For example, look at the exception in debugger and check its StackTrace property.
  • FabFab97 Profile Picture
    FabFab97 71 on at
    Next call on Chain of Command
    Hi everyone, it's me again. 
    This is my code now, I'm using the canProceedWithInvoicePOsting() method as suggested by @Martin Dràb, but I still get an error once i click Yes on my pop-up. This is my code now:

    [ExtensionOf(formControlStr(VendEditInvoice, OK))]

    final class VendEditInvoice_OK_AzimutBenetti_Extension

    {

        private boolean canProceed = true;

     

        public void clicked()

        {

            IntentLetter_IT intentLetter;

            VendInvoiceInfoLine vendInvoiceInfoLine;

     

            Amount totalAmount;

            Amount remainingAmount;

            RecId intentLetterRecId;

     

            // Ciclo attraverso tutte le fatture

            while select * from vendInvoiceInfoLine

            {

                totalAmount = vendInvoiceInfoLine.LineAmount;

                intentLetterRecId = vendInvoiceInfoLine.RecId;

     

                select firstOnly intentLetter

                    where intentLetter.RecId == intentLetterRecId;

     

                remainingAmount = intentLetter.remainAmountMST();

     

                Info(strFmt('%1, %2', totalAmount, remainingAmount));

     

                if (totalAmount >= remainingAmount)

                {

                    DialogButton diagBut;

                    str strMessage = "Importo da decurtare al saldo della lettera di intento maggiore dell’importo residuo. Proseguire?";

                    str strTitle = "Splafonamento";

                    diagBut = Box::yesNo(

                        strMessage,

                        DialogButton::No,

                        strTitle

                    );

                    if (diagBut == DialogButton::No)

                    {

                        canProceed = false;

                        info('Operazione annullata');


                    }

                }

            }

    next clicked();

        }

     

        public boolean canProceedWithInvoicePosting()

        {

            return canProceed;

        }

     

        public void setCanProceed(boolean _canProceed)

        {

            canProceed = _canProceed;

        }

    }

    but i get this error: Code execution error: Form Button Control (object), clicked method called with invalid parameters

    anyone knows why? 
    Can it be because of the while select loop? 
    Thank You.
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,198 Most Valuable Professional on at
    Next call on Chain of Command
    A good place for your logic seems to be canProceedWithInvoicePosting() method of VendEditInvoice form. You can create a CoC extension, add the dialog below next and return false if you want to stop the process.
  • Bharani Preetham Peraka Profile Picture
    Bharani Preetham Pe... 3,605 Super User 2024 Season 1 on at
    Next call on Chain of Command
    If there is an error on clicking a button, then the process will stop. If it is not stopping have you debugged it? Can you send your code here with throwing exception?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans