web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

How to Retrieve Data from table to another

(1) ShareShare
ReportReport
Posted on by 24
internal final class TestClass1
{
    // Pre-insert event handler for VendInvoiceInfoLine
    [PreHandlerFor(tableStr(VendInvoiceInfoLine), tableMethodStr(VendInvoiceInfoLine, insert))]
    public static void VendInvoiceInfoLine_PreInsert(XppPrePostArgs args)
    {
        VendInvoiceInfoLine vendInvoiceInfoLine = args.getThis() as VendInvoiceInfoLine;
        PurchLine purchLine;
        Real totalConfirmedTaxAmount = 0; // Variable to hold the sum if needed
        // Check if vendInvoiceInfoLine is not null
        if (vendInvoiceInfoLine)
        {
            // Log OrigPurchId and InventTransId for debugging
            info(strFmt("OrigPurchId: %1, InventTransId: %2", vendInvoiceInfoLine.OrigPurchId, vendInvoiceInfoLine.InventTransId));
            // Use 'while select' to loop through multiple matching PurchLine records and select only ConfirmedTaxAmount
            while select ConfirmedTaxAmount from purchLine
                where purchLine.PurchId == vendInvoiceInfoLine.OrigPurchId
                && purchLine.InventTransId == vendInvoiceInfoLine.InventTransId
            {
                // Here we are summing the ConfirmedTaxAmount if multiple records exist.
                totalConfirmedTaxAmount += purchLine.ConfirmedTaxAmount;
                // Log each ConfirmedTaxAmount for debugging
                info(strFmt("ConfirmedTaxAmount found: %1", purchLine.ConfirmedTaxAmount));
            }
            // Set RealAmount to the total ConfirmedTaxAmount if records were found
            if (totalConfirmedTaxAmount > 0)
            {
                vendInvoiceInfoLine.RealAmount = totalConfirmedTaxAmount;
                vendInvoiceInfoLine.write(); // Save the RealAmount update
                info(strFmt("Total RealAmount set to: %1", vendInvoiceInfoLine.RealAmount));
            }
            else
            {
                error("No matching PurchLine records found for given criteria.");
            }
        }
        else
        {
            error("vendInvoiceInfoLine is null.");
        }
    }
}
I wanna here get data from PurchLine to put it in custom field i created it to store data from PurchLine to Vendor Table such as SalesTax before Post specific invoice how can i do this ?
 
I have the same question (0)
  • Jonas "Jones" Melgaard Profile Picture
    5,016 Most Valuable Professional on at
    This looks like X++ code, so I'm assuming you are in D365 F&O.
    I have formatted your attached code, so it's easier to read.
     
    internal final class TestClass1
    {
        // Pre-insert event handler for VendInvoiceInfoLine
        [PreHandlerFor(tableStr(VendInvoiceInfoLine), tableMethodStr(VendInvoiceInfoLine, insert))]
        public static void VendInvoiceInfoLine_PreInsert(XppPrePostArgs args)
        {
            VendInvoiceInfoLine vendInvoiceInfoLine = args.getThis() as VendInvoiceInfoLine;
            PurchLine purchLine;
            Real totalConfirmedTaxAmount = 0; // Variable to hold the sum if needed
    
            // Check if vendInvoiceInfoLine is not null
            if (vendInvoiceInfoLine)
            {
                // Log OrigPurchId and InventTransId for debugging
                info(strFmt("OrigPurchId: %1, InventTransId: %2", vendInvoiceInfoLine.OrigPurchId, vendInvoiceInfoLine.InventTransId));
    
                // Use 'while select' to loop through multiple matching PurchLine records and select only ConfirmedTaxAmount
                while select ConfirmedTaxAmount from purchLine
                    where purchLine.PurchId == vendInvoiceInfoLine.OrigPurchId
                       && purchLine.InventTransId == vendInvoiceInfoLine.InventTransId
                {
                    // Summing the ConfirmedTaxAmount if multiple records exist.
                    totalConfirmedTaxAmount += purchLine.ConfirmedTaxAmount;
                    
                    // Log each ConfirmedTaxAmount for debugging
                    info(strFmt("ConfirmedTaxAmount found: %1", purchLine.ConfirmedTaxAmount));
                }
    
                // Set RealAmount to the total ConfirmedTaxAmount if records were found
                if (totalConfirmedTaxAmount > 0)
                {
                    vendInvoiceInfoLine.RealAmount = totalConfirmedTaxAmount;
                    vendInvoiceInfoLine.write(); // Save the RealAmount update
                    info(strFmt("Total RealAmount set to: %1", vendInvoiceInfoLine.RealAmount));
                }
                else
                {
                    error("No matching PurchLine records found for given criteria.");
                }
            }
            else
            {
                error("vendInvoiceInfoLine is null.");
            }
        }
    }
    
    I'm not sure what you are asking, do you receive an error? If so what error do you receive?
    You don't need to call vendInvoiceInfoLine.write(), as you are modifying the record before it's saved to the database. Skip that line.
     
    Afterwards we can talk about what could be improved.
  • Jonas "Jones" Melgaard Profile Picture
    5,016 Most Valuable Professional on at
    Another point after reviewing the code again:

    The following if statement will always return false.
    The type conversion between common and boolean relies on .RecId. If .RecId has a value, the type conversion will evaluate to true; otherwise, it will evaluate to false.
    Since it's inside an insert method, .RecId will presumable always be 0.
     
            // Check if vendInvoiceInfoLine is not null
            if (vendInvoiceInfoLine)
            {
                *business logic*
            }
            else
            {
                error("vendInvoiceInfoLine is null.");
            }
  • AbdelrahmanAshraf Profile Picture
    24 on at
    i've created EDT Real data type and add it to vendEditInfo form and trying with this code to retrieve data in this created field, i don't have any error displayed just have 0.0 value in realamount field 
  • Jonas "Jones" Melgaard Profile Picture
    5,016 Most Valuable Professional on at
    You should get an infolog message, unless if what is creating these lines is not calling data methods.
    What happens if you add a breakpoint to VendInvoiceInfoLine.insert(), and then create a new invoice?
     
  • AbdelrahmanAshraf Profile Picture
    24 on at
    [ExtensionOf(tableStr(VendInvoiceInfoLine))]
    final class VendInvoiceInfoLine_Extension
    {
        // Static display method to fetch ConfirmedTaxAmount from PurchLine
        display public static real displayConfirmedTaxAmount(VendInvoiceInfoLine _vendInvoiceInfoLine)
        {
            PurchLine purchLine;
            // Retrieve the related PurchLine record based on OrigPurchId and InventTransId
            select firstonly purchLine
                where purchLine.PurchId == _vendInvoiceInfoLine.OrigPurchId
                   && purchLine.InventTransId == _vendInvoiceInfoLine.InventTransId;
            // Return ConfirmedTaxAmount if PurchLine is found, otherwise return 0
            return purchLine ? purchLine.ConfirmedTaxAmount : 0;
        }
        // Static method to test updating RealAmount for VendInvoiceInfoLine
        static real TestVendInvoiceInfoLine(Args _args)
        {
            VendInvoiceInfoLine vendInvoiceInfoLine;
            if (vendInvoiceInfoLine)
            {
                PurchLine purchLine;
                // Retrieve the related PurchLine record
                select firstonly purchLine
                    where purchLine.PurchId == vendInvoiceInfoLine.OrigPurchId
                       && purchLine.InventTransId == vendInvoiceInfoLine.InventTransId;
                if (purchLine)
                {
                    vendInvoiceInfoLine.RealAmount = purchLine.ConfirmedTaxAmount;
                    vendInvoiceInfoLine.update(); // Save the updated RealAmount to the database
                    info(strFmt("RealAmount updated successfully to: %1", vendInvoiceInfoLine.RealAmount));
                    return vendInvoiceInfoLine.RealAmount; // Return the updated RealAmount
                }
                else
                {
                    info("No matching PurchLine found.");
                    return 0; // Default return if no matching PurchLine is found
                }
            }
            else
            {
                info("No matching VendInvoiceInfoLine found.");
                return 0; // Default return if no VendInvoiceInfoLine is found
            }
        }
    }
    I've updated my code first function display only without saving in Database 2nd function trying to save data in database while creating EDT Real and add it in VendInvoiceInfoLine table then drag this and drop it in vendEditInfo form .. what i can do to let it to be retrieved in EDT Real 

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 628 Super User 2026 Season 1

#2
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 612

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 589 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans