function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DrceteDrcete 

How to get complex type values in SOQL query?

Hello,
I want to get the value of the fields in a complex type through a SOQL query.
for(SearchLayout l : [
    SELECT FieldsDisplayed from SearchLayout 
    WHERE EntityDefinition.QualifiedApiName='Contact' AND LayoutType='Lookup']) {
  System.debug(l.FieldsDisplayed);
}
FieldsDisplay is a complex type containing the fields string property.
How can I read this value?
Thanks for your help!
Best Answer chosen by Drcete
GauravendraGauravendra
Hi Dcrete,

To read the FieldsDisplay complex type, you need to serialize the SearchLayout object.
Inside the loop you can use JSON.parse(searchLayout), that will serialize the entire record. After that you can use the inner class to read the recrod.
for(SearchLayout l : [
        SELECT FieldsDisplayed from SearchLayout 
        WHERE EntityDefinition.QualifiedApiName='Contact' AND LayoutType='Lookup']) {
            System.debug(JSON.serialize(l));
            System.debug(l.FieldsDisplayed);

        }
That serialization will return JSON something like that.. which will contains all the fields in the seach layout:
{
    "attributes":{
        "type":"SearchLayout","url":"/services/data/v50.0/sobjects/SearchLayout/Contact.Lookup"
    },
    "FieldsDisplayed":{
        "applicable":true,
        "fields":[
            {"apiName":"Name","label":"Name","sortable":true},
            {"apiName":"Account.Name","label":"Account Name","sortable":true}
            {"apiName":"MailingStreet","label":"Mailing Street","sortable":true}
                ]
        },
    "Label":"Lookup Dialogs",
    "EntityDefinitionId":"Contact",
    "Id":"000000000000000AAA"
}

Hope this helps!
 

All Answers

GauravendraGauravendra
Hi Dcrete,

To read the FieldsDisplay complex type, you need to serialize the SearchLayout object.
Inside the loop you can use JSON.parse(searchLayout), that will serialize the entire record. After that you can use the inner class to read the recrod.
for(SearchLayout l : [
        SELECT FieldsDisplayed from SearchLayout 
        WHERE EntityDefinition.QualifiedApiName='Contact' AND LayoutType='Lookup']) {
            System.debug(JSON.serialize(l));
            System.debug(l.FieldsDisplayed);

        }
That serialization will return JSON something like that.. which will contains all the fields in the seach layout:
{
    "attributes":{
        "type":"SearchLayout","url":"/services/data/v50.0/sobjects/SearchLayout/Contact.Lookup"
    },
    "FieldsDisplayed":{
        "applicable":true,
        "fields":[
            {"apiName":"Name","label":"Name","sortable":true},
            {"apiName":"Account.Name","label":"Account Name","sortable":true}
            {"apiName":"MailingStreet","label":"Mailing Street","sortable":true}
                ]
        },
    "Label":"Lookup Dialogs",
    "EntityDefinitionId":"Contact",
    "Id":"000000000000000AAA"
}

Hope this helps!
 
This was selected as the best answer
DrceteDrcete

Thanks Gauravendra,
To complement your answer:
JSON2Apex (https://json2apex.herokuapp.com/) generates custom Apex classes for the specific JSON structure, I found it very useful.
Regards