I've been looking in to the ui-api
but it doesn't cut it for me, what I need It's to return exclusively the fields in the Page Layout for an object and some additional records.
Something like Getting the Opportunity
and getting its related records like the Contact
records related to it as well as the OpportunityLineItem
records related to it.
The ui-api
does the job, but still, it's a little of a pain to extract the data from it, I'm trying to feed an external service with this, but there are just so few fields and I want to do it for many objects, that is why querying the Page Layout could be useful, may be not the data of the record but at least the involved fields, so I can do the SOQL query and get them.
My first attempt to solve this issue, was querying the full object, with 2 levels of depth, but It was too much, for the request.
Best Answer
The other way besides the UI API to approach this in pure Apex (that is, without calling out to an API explicitly) is to use the Apex Metadata API, a thin wrapper over some of the basic functionality of the Metadata API. Apex Metadata supports pulling layout details. Cf. also Trailhead, which has an example of using this functionality for a different purpose.
An example:
This would output the API names of all of the fields on the layout "Account-Account Layout". But it doesn't do all the other things the UI API does for you, like managing record types, visibility/FLS, picklist entries, etc. You'd need to handle all of that yourself, including determining the correct layout to display for any given combination of profile and record type so that you can query it.
I don't want to opine too much about your system architecture because there's obviously complexity there that I wouldn't assume you can cover in a comment. I'd just urge considering a few points before you reach for a completely different solution: