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

How do I call a method that is inside another button.

(0) ShareShare
ReportReport
Posted on by 148

Hi, 

Inside a form I have a button called Multivencimientos which when clicked it does the following:

void clicked()
{
    CustVendPaymSched  custVendPaymSched;

    if (! CustPaymSched::exist(salesParmTable.TableId,salesParmTable.RecId))
    {
        custVendPaymSched = CustVendPaymSched::construct(SysModule::Cust,salesParmTable);

        if (specQty.selection() == SalesUpdate::All && CustPaymSched::exist(salesParmTable.salesTable().TableId, salesParmTable.salesTable().RecId))
        {
            custVendPaymSched.copyCustVendPaymSched(CustPaymSched::find(salesParmTable.salesTable().TableId, salesParmTable.salesTable().RecId));
        }
        else
        {
            custVendPaymSched.copyPaymSched(PaymSched::find(salesParmTable.salesTable().PaymentSched));
            salesParmTable.createPaymentSched();
        }
    }
    super();
}


Now, I have another button called Ok inside the same form which has the following when clicked:

void clicked()
{
    boolean procced = true;
    LecVoucherType doc; //LECMMV03.ln
    custinvoicejour _custinvoicejour;  //LEC081.ln
    custinvoicejour _custinvoicejour1;  //LEC081.ln

    //LEC085.sn
    LecVoucherType                      docType;
    OpeningId                           ID_Establecimiento;

    LecInvSecForOpening                 _LecInvSecForOpening;
    LecGuiaRemSecForOpening             _LecGuiaRemSecForOpening;
    LecRetSecForOpening                 _LecRetSecForOpening;
    LecDebNoteSecForOpening             _LecDebNoteSecForOpening;
    LecCredNoteSecForOpening            _LecCredNoteSecForOpening;

    LecInvConfTable                     _LecInvConfTable;
    LecGuiaRemConfTable                 _LecGuiaRemConfTable;
    LecCredDocConfTable                 _LecCredDocConfTable;
    LecDebDocConfTable                  _LecDebDocConfTable;
    SalesParmTable                      _salesParmTable1; //LEC0041.nl
    FormDataObject                      myField;//PROICE_002.nl
    int                                 MaximoDocumento = 0;
    int                                 MaximoTalonario = 0;
    //LEC085.en
    ;

  
    if(PrintFormLetter.visible() == true && DocumentStatus==DocumentStatus::Invoice)
    {
        if (printFormLetter.checked() == false)
        {
            throw error ("NOV: El check 'Imprimir factura' debe estar marcado para poder continuar");
        }
    }
    
    if( documentStatus   != DocumentStatus::Confirmation &&
        documentStatus   != DocumentStatus::PackingSlip)
    {
    
    _salesParmTable1 = salesParmTable_ds.getFirst();
    while (_salesParmTable1)
    {
       if (!element.validateSalesItemTaxFields(_salesParmTable1.SalesId))
        {
            procced = false;
        }
        _salesParmTable1 = salesParmTable_ds.getNext();
    }
    
    }       

    
    if(DocumentStatus==DocumentStatus::PackingSlip)
    {
        _salesParmTable1 = salesParmTable_ds.getFirst();
        while (_salesParmTable1)
        {
            if (!element.valida_OV_GuiaRem(_salesParmTable1.SalesId))
            {
                procced = false;
            }
            _salesParmTable1 = SalesParmTable_ds.getNext();
        }
    }

    if(DocumentStatus==DocumentStatus::Invoice)
    {
        _salesParmTable1 = salesParmTable_ds.getFirst();
        while (_salesParmTable1)
        {
            if (!element.valida_OV_Factura(_salesParmTable1.SalesId))
            {
                procced = false;
            }
            _salesParmTable1 = SalesParmTable_ds.getNext();
        }

        element.compensation(LecRegisterCompensation::Register);//LEC0087.nl
    }
    
    if(documentStatus == documentStatus::PackingSlip)
    {
        if(cbGenerateGuia.checked() && updateNow.checked())
        {
            procced = LecSaleGuiaRemClass::validateAccountOrder(SalesparmUpdate.SumBy,SalesparmUpdate.SumSalesId);
            if(procced)
            {
                procced = LecSaleGuiaRemClass::validateExistGuiaRemTmp(SalesparmUpdate.SumBy,SalesParmTable_ds,SalesParmUpdate.SumSalesId);
                //todo probar validación de que se hayan ingresados datos de guias tmp
            }
            if(procced)
            {
                procced = LecSaleGuiaRemTmpTable::isMandatoryDataInSaleGuiaRemTmp(SalesParmTable.ParmId);
            }
            if(procced)
            {
                procced = LecSaleGuiaRemClass::valMaxNumOflinesPerSalesGuia(SalesParmTable);
            }
        }
    }

    if(procced)
    {
        if(SalesParmTable.salesTable().LecIsDebitNote == NoYesCombo::Yes && SalesParmupdate.SpecQty != SalesUpdate::All)
        {
            procced = false;
            info("@LEC1420");
        }
    }

    if(procced)
    {
        
        doc = LecVoucherTypeClass::getDocType(SalesParmTable.DocType);
        if(doc.SystemDocType == LecDocTypeEnum::DocInternoReverso)
        {
            if(SalesParmTable.LecModifiedDocNum_Reverso == '')
            throw error('LEC: Usted seleccionó el tipo de documento '    SalesParmTable.DocType   ', es necesario seleccionar una '   enum2str(LecDocTypeEnum::CreditDoc)   ' o una '   enum2str(LecDocTypeEnum::DebNote));
        }
       
        myField = SalesParmUpdate_ds.object(fieldnum(SalesParmUpdate,SpecQty));
        myField.modified();
       

        if(DocumentStatus==DocumentStatus::PackingSlip || DocumentStatus==DocumentStatus::Invoice)
        {
            if(LecEstabPredUserClass::ValidatePostSales(DocumentStatus,SalesParmTable,cbGenerateGuia.checked(),updateNow.checked())==true)
                super();
        }
        else
        {
            super();
        }
    }
}


The thing is that I need to pass through the code inside the Multivencimientos button even if it wasn't clicked every time. I was thinking of just copying the code inside the button Ok but I think that might cause a problem of duplicates or something like that if a user first clicks the Multivencimientos button and later the Ok button. It also must have a validation to check if the button was already clicked and in that case it wont need to pass through it again.

  • Daniel Mora Profile Picture
    148 on at
    RE: How do I call a method that is inside another button.

    Yes, thanks Martin. The issue with that is that the "clicked( )" method from the button Ok was made and touched by different programmers and by company policy, I can't change that unless I have a direct order to change it. I know I can suggest changing it, but that already happens in several code sections all around this specific environment AX2012 and it's already chaos. At the moment, I just want to resolve the above, with the help you already offered. Thank you.

  • Martin Dráb Profile Picture
    231,723 Most Valuable Professional on at
    RE: How do I call a method that is inside another button.

    In general, you should get used to splitting code to methods, because what you're currently writing is code that is difficult to understand, change and reuse. What you have in the other clicked() should also be moved to a separate method, and it should even be split to smaller methods. For example, this is how you can avoid duplication of your code iterating records:

    void aMethod()
    {
        ...
        
        salesParmTable1 = salesParmTable_ds.getFirst();
        while (salesParmTable1)
        {
        	if (!this.validateSalesParmTable(salesParmTable1))
        	{
        		procced = false;
        	}
        	
        	salesParmTable1 = SalesParmTable_ds.getNext();
        }
        ...
    }
    
    boolean validateSalesParmTable(SalesParmTable _salesParmTable)
    {
    	boolean ok;
    	
    	switch (documentStatus)
    	{
    		case Confirmation:
    			ok = true;
    			break;
    		case DocumentStatus::Invoice:
    			ok = element.valida_OV_Factura(_salesParmTable1.SalesId);
    			break;
    		case DocumentStatus::PackingSlip:
    			ok = element.valida_OV_GuiaRem(_salesParmTable1.SalesId);
    			// no break
    		default:
    			ok = element.validateSalesItemTaxFields(_salesParmTable1.SalesId);
    			
    	}
    	
    	return ok;
    }

    Also note that your code contains unused variables, method names should be in English, local variables shouldn't have names starting with underscore (that should be used only for method parameters) and variables should have names starting with lowercase. Always use parentheses around code blocks (e.g. if or while) and use line indentation. E.g. do this:

    if (SalesParmTable.LecModifiedDocNum_Reverso == '')
    {
        throw error("...");
    }

    instead of this:

    if(SalesParmTable.LecModifiedDocNum_Reverso == '')
    throw error('...');

    Prefer strFmt() when constructing strings for error messages.

    You'll make your code easier to understand if you follow usual conventions like these. Code that is easier to understand is less likely to contain bugs and it's much easier to fix if it does.

  • Verified answer
    Martin Dráb Profile Picture
    231,723 Most Valuable Professional on at
    RE: How do I call a method that is inside another button.

    clicked() isn't the right name for your logic and putting the business logic to a form controls causes problems with reusability, as you've just found. It may be in the form, or maybe you want to create a class or something. Duplicating the code is also bad idea, as you correctly recognized.

    The correct solution is simple: create a new method for your logic, give it a descriptive name and call this method where needed. You can also easily create an instance variable saying whether the logic was already called, and you'll check (and set) this variable in your method.

  • Suggested answer
    Mohit Rampal Profile Picture
    12,554 Moderator on at
    RE: How do I call a method that is inside another button.

    Hi Daniel, How about adding a boolean parmMethod in form methods and setting it to True on clicked method of Multivencimientos button. Check the value of this parmMethod in clicked of Ok button and write code accordingly.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,865 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,723 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans