I'm working on a project building an APEX REST API that would benefit by understanding the page layout for the user/record to decide on the response. It will feed a mobile app, and so I'm trying to manage/minimize API calls from the mobile device.

In short, I'm trying to callout to the REST api for /services/data/v36.0/sobjects/Account/describe/layouts/ (as an example) from within my apex class, using the response from that callout with some other data, to respond to the GET request for this apex class.

I've looked into JSON2Apex but I wasn't able to get it working correctly for the results from describe/layouts/.

Any help or pointers are greatly appreciated!

  • It returns a JSON response when you call this API? Is there any specific details you want to fetch out of the results? – Salesforcesmarty Jan 11 '16 at 23:39
  • Yes, the standard describe layout rest API returns JSON. I would like to pull the order of the fields from the layout, whether they are editable, etc. to render appropriately in a React Native app. I could accomplish this with several standard API calls, but that would be wasteful of API calls (and limits) and mobile bandwidth. – Rob Goldiez Jan 11 '16 at 23:42

This is how I will solve this issue.

I used "http://jsoneditoronline.org/" to display the JSON in organised way and then created a class structure to get the required details.

  1. LayoutRows - Each row on my layout
  2. Layoutitems - Fields on each row

    List < Layouts> layouts = (List< Layouts> )JSON.deserialize(String jsonString, Layouts.Class);

//jsonString is your response from the API call

Public class Layouts{

    Public ButtonLayoutSection buttonLayoutSection;
    Public DetailLayoutSections detailLayoutSections;
    Public EditLayoutSections editLayoutSections;
    Public RelatedLists relatedLists

    Public Class ButtonLayoutSection{


    Public Class DetailLayoutSections{
        Public Integer columns;
        Public String heading;
        Public LayoutRows layoutsRows;

    Public Class EditLayoutSections{


    Public Class RelatedLists{


    Public Class LayoutRows{
        Public LayoutItems layoutItems;
        Public Integer numItems;

    Public Class LayoutItems{
        Public boolean editableForNew;
        Public boolean editableForUpdate
        Public string label;
        Public string required;

I have not personally verified this code, but I have listed a structure to follow.

  • I'm getting an 'invalid type' error at compile when trying to invoke the class with the code you suggested here: List < Layouts> layouts = (List< Layouts> )deserialize(String jsonString, Layouts.Class); – Rob Goldiez Jan 12 '16 at 2:14
  • Update this to List < Layouts> layouts = (List< Layouts> )JSON.deserialize(String jsonString, Layouts.Class); – Salesforcesmarty Jan 12 '16 at 2:29
  • Thanks. I'm past the compile error so thank you. However, something is still not right. My REST API call returns an apex Object. So, I tweaked your string to JSON.serialize the Object from the API call. Everything compiles and saves, but a call to the new REST API (the one I'm trying to write, which in itself calls out to another API) returns a 500 server error and the stack trace points to the line of code we're discussing in these comments: System.JSONException: Apex Type unsupported in JSON: System.HttpResponse – Rob Goldiez Jan 12 '16 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.