Alfasith AX

Alfasith AX
اللَّهُمَّ انْفَعْنِي بِمَا عَلَّمْتَنِي، وَعَلِّمْنِي مَا يَنْفَعُنِي، وَزِدْنِي عِلْمًا

Friday, November 24, 2023

Auto submit of any process to Workflow in D365 / X++

 Hi,


        try
        {
            ttsbegin;
                        
            Workflow::activateFromWorkflowType( workFlowTypeStr( "workFlowTypeName"),  TableName.RecId, "Auto submitted by action Alfa", NoYes::No);
            
            ttscommit;
        }
        catch(exception::Error)
        {
            throw Error("Not submitted");
        }

///             _workflowCorrelationId = Workflow::activateFromWorkflowType( _workflowTypeName, _recId, "Comments", NoYes::No);
             

             IssueStateChangeManager::submit(TableBuffer);


Regards,

Auto Submit the pending vendor Invoice to workflow in D365

 /// <summary>
    /// Post open inventory journals
    /// </summary>
    public void run()
    {
        TradeLineRefId       headerReference;
        VendInvoiceInfoTable vendInvoiceInfoTable;
     
        //vendInvoiceInfoTable = VendInvoiceInfoTable::findTableRefId(headerReference);
 
        while (gQueryRun.next())
        {
            vendInvoiceInfoTable    = gQueryRun.get(tableNum(VendInvoiceInfoTable));
            try
            {
                if(this.validationWorkflowSubmit(vendInvoiceInfoTable))
                {
                    this.SubmitVendorInvoice(vendInvoiceInfoTable);
                }
            }
            
            catch (Exception::CLRError)
            {
                System.Exception interopException = CLRInterop::getLastException();
                error(strFmt("%1", interopException));
            }
            catch (Exception::Error)
            {
                error(strFmt("An error occured during the update."));
            }
        }
    }

    /// <summary>
    /// Validates if the workflow can be started.
    /// </summary>
    /// <returns>true if workflow can start</returns>
    private boolean validationWorkflowSubmit(vendInvoiceInfoTable   _vendInvoiceInfoTable)
    {
        // Don't submit if a posted invoice with the same invoice number already exists.
        if (!this.validateDuplicateInvoice(_vendInvoiceInfoTable))
        {
            throw Exception::error;
        }
                
        if (PublicSectorUtils::isBudgetReservationEnabled())
        {
            VendInvoiceInfoTable::checkBudgetReservationBalance_PSN(_vendInvoiceInfoTable.TableRefId, _vendInvoiceInfoTable.SourceDocumentHeader);
        }
        if (isConfigurationkeyEnabled(configurationKeyNum(Project)) && ProjFundingLimitTrackingManager::hasLimitErrorSourceDocument(_vendInvoiceInfoTable.SourceDocumentHeader))
        {
            throw error("@SYS4110061");
        }
        boolean areAccountingDistributionsValid;
        VendParameters vendParameters = VendParameters::find();
        if((Dynamics.AX.Application.FeatureStateProvider::isFeatureEnabled( AccountingDistributionWorkflowSubmissionFeature::instance()) && vendParameters.BypassValidationOfAccountingDistributions == NoYes::Yes)
            || SourceDocumentProvider::areSourceDocAccDistAccountValid( SourceDocumentHeader::find( _vendInvoiceInfoTable.SourceDocumentHeader), true, true))
        {
            areAccountingDistributionsValid = true;
        }
        if (!areAccountingDistributionsValid)
        {
            return false;
        }
        return true;
    }

    /// <summary>
    /// Determines if a posted invoice with the same invoice number already exists.
    /// </summary>
    /// <returns>
    /// True if a posted invoice with the same invoice number does not exist. False if already exists.
    /// </returns>
    private boolean validateDuplicateInvoice(VendInvoiceInfoTable  _vendInvoiceInfoTable)
    {
        boolean isInvoiceNumberFreeForUse = true;
        if (Dynamics.AX.Application.FeatureStateProvider::isFeatureEnabled( VendInvoiceRejectDuplicateNumOnWorkflowSubmitFeature::instance()))
        {
            VendTable localVendTable = VendTable::find(_vendInvoiceInfoTable.InvoiceAccount);
            if (!localVendTable.checkInvoice(_vendInvoiceInfoTable.Num, _vendInvoiceInfoTable.TransDate))
            {
                isInvoiceNumberFreeForUse = false;
            }
        }
        return isInvoiceNumberFreeForUse;
    }
    /// <summary>
    /// Creates the vendor invoice.
    /// </summary>
    public void SubmitVendorInvoice(VendInvoiceInfoTable  _vendInvoiceInfoTable)
    {
        workflowComment        workflowComment = "Auto submission to workflow.";
        //changecompany (custInvoice.ProjIntercompany)
        //{
        if (_vendInvoiceInfoTable.RecId
                && _vendInvoiceInfoTable.RequestStatus == VendInvoiceRequestStatus::Draft)
               // && custTable.interCompanyTradingPartner().interCompanyTradingRelation().Active
                //&& this.validationWorkflowSubmit())
            {
                VendInvoiceHeaderWorkflow::submitToWorkflow( _vendInvoiceInfoTable, workflowComment);
            }
        //}
    }


Thursday, November 9, 2023

CancelSuperCall in D365

 Hi,


Usually, based on the FormControlEventArgs e, the cancellation can be implemented using the following code snippets:


FormControlCancelableSuperEventArgs     eventArgs = e as FormControlCancelableSuperEventArgs;

eventArgs.CancelSuperCall();


In cases where there are no FormControlEventArgs, you are required to throw an error to halt the super process.

Thanks,

Wednesday, August 16, 2023

Code to call the caller form refresh in D365

 Hi,

can use the below code snippets in Controller or any other class called from

        #Task

        FormRun formRun;


        formRun = element.args().caller(); //  args.caller();

        if(formRun)

               formRun.task(#taskF5);

   

Regards,

Tuesday, June 20, 2023

X++ code to get ExchRate() in D365

 hi,


  public ExchRate exchRate(

        CurrencyCode _fromCurrency,

        CurrencyCode _toCurrency = Ledger::find(Ledger::current()).AccountingCurrency,

        TransDate    _transDate = today(),

)

    {

        ExchangeRate     exchangeRate;

        ExchangeRateType ExchangeRateType;

        ExchangeRateCurrencyPair exchangeRateCurrencyPair;

        real             exchRate;


        CurrencyCode fromCurrency  = this.CurrencyCode;

        TransDate    transDate     = this.TransDate;


        select firstonly exchangeRateCurrencyPair   where

        exchangeRateCurrencyPair.ExchangeRateType == Ledger::find(Ledger::current()).DefaultExchangeRateType

        &&  exchangeRateCurrencyPair.FromCurrencyCode == _fromCurrency

        &&  exchangeRateCurrencyPair.ToCurrencyCode   == _toCurrency;

        return exchangeRate::findByDate(exchangeRateCurrencyPair.RecId,_transDate).ExchangeRate;

    }

Wednesday, May 10, 2023

Project resource with Role ID lookup in D365

 Hi,


               SysReferenceTableLookup     sysRefTableLookup;

        Query                       lookupQuery = new Query();

        QueryBuildDataSource        lookupQueryDataSource;

        QueryBuildDataSource        PSASchedRoleQueryDataSource;


        sysRefTableLookup = SysReferenceTableLookup::newParameters(tableNum(ResourceCategoryView), _formReferenceGroupControl);


        sysRefTableLookup.addLookupfield(fieldNum(ResourceCategoryView, CategoryId));

        sysRefTableLookup.addLookupfield(fieldNum(ResourceCategoryView, Name));


        lookupQueryDataSource = lookupQuery.addDataSource(tableNum(ResourceCategoryView));


        

        PSASchedRoleQueryDataSource = lookupQueryDataSource.addDataSource(tableNum(PSASchedRole));

        PSASchedRoleQueryDataSource.relations(false);

        PSASchedRoleQueryDataSource.joinMode(JoinMode::ExistsJoin);

        PSASchedRoleQueryDataSource.addLink(fieldNum(ResourceCategoryView, RecId), fieldNum(PSASchedRole, RecId));


Regards,

Monday, April 3, 2023

Iterate form Datasource in AX2012 /D365 FO

 Hi,

I used the event hander onClicked button to select all the records of lines in datasources 

   public static void Demo_DeleteButton_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        FormButtonControl  callerButton = sender as FormButtonControl;
        FormRun form = callerButton.formRun();
        FormDataSource forecastSales_ds =  form.dataSource(formdatasourcestr(Demo, ForecastSales))  as FormDataSource;
        ForecastSales forecastSalesExpand;
        ttsBegin;
        for(forecastSalesExpand = forecastSales_ds.getFirst(true) ? forecastSales_ds.getFirst(true) : forecastSales_ds.cursor();
forecastSalesExpand; 
forecastSalesExpand = forecastSales_ds.getNext())
        {
            //Perform your operation in forecastSalesExpand;
        }
        forecastSales_ds.executeQuery();
        ttsCommit;
    }

Regards,

How find size of recordsortedlist in D365/AX 2012

Hi, This is the continuity of the previous article where we are now getting the size of recordsortedlist . if(recordsortedlist.len() >1) ...