How to get the child table records?

Hi All,

I can get the parent table record from child table record like

purchline.purchtable();

But, how to get the child record, in our case how to get the purchline for the selected purchid.

I am using below method

purchline localpurchline;

select * from localpurchline where localpurchline.purchid == purchtable.purchid;

Is there any other idea to get the child records for the selected parent record.

Thanks,

Hari

In principle, you always have to do the same thing: to find records in database filtered by an identifier of the parent table.

Using the select statement in X++ makes sense, although you can use the Query framework instead. The question is what problem you’re trying to solve.

Hi Martin,

Thanks for your reply.

We can get the parent table record by simply using this method like purchline.purchtable().

So, I just tried to find any easy method available to get the child records instead of using select query like LINQ EF.

Thanks,

Hari

It seems to me that you expect some magic to happen in PurchLine.purchTable(), but it’s just a select statement wrapped in a method.

This is PurchLine.purchTable():

PurchTable purchTable(boolean update = false)
{
    return PurchTable::find(this.PurchId, update);
}

And this is PurchTable.find():

static PurchTable find(
    PurchId          purchId,
    boolean          _forUpdate = false,
    ConcurrencyModel _concurrencyModel = ConcurrencyModel::Auto)
{
    PurchTable purchTable;

    if (purchId)
    {
        ...

        <b>select firstonly purchTable
            index hint PurchIdx
            where purchTable.PurchId == purchId;</b>
    }

    return purchTable;
}

Of course, it always returns a single header, while your method would have to return 0 - n lines.

Maybe you wanted to ask how to return multiple records from a single method call, but that has many possible solutions and your choice depends on circumstances. You may return a collection (e.g. List or RecordSortedList). You may return an instance of Query or QueryRun and let the caller to fetch records. You could even return a buffer and let the caller to iterate through records by the next statement (but it’s counter-intuitive and I wouldn’t recommend it). You may pack one of the previous solutions to a specialized class representing a collection of purchase lines.

It has its place in some complex scenarios, but I don’t see the point with purchase lines. Will you get any simpler and more readable code than this?

while select purchLine
    where purchLine.PurchId == purchTable.PurchId
{
}