buli
2022-03-10 a90c9ecfc5118547d0a92b2fee2779eca95e09a5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public without sharing class TestClass 
{
    public static List<Metadata.LayoutSection> GetLayoutSections(string object_name, string layout_name){
        List<String> componentNameList = new List<String>{object_name+'-'+layout_name};
        //通过Metadata.Operations.retrieve获取metadata
        //Metadata.Layout -> Metadata.LayoutSection -> Metadata.LayoutColumn objects -> Metadata.LayoutItem objects
        List<Metadata.Metadata> componentList = Metadata.Operations.retrieve(Metadata.MetadataType.Layout, componentNameList);
        if(componentList?.size() > 0){
            Metadata.Layout layout = (Metadata.Layout) componentList.get(0);
            List<Metadata.LayoutSection> layoutSectionList = layout.layoutSections;
            return layoutSectionList;
        }
        else{
            return null;
        }
        
    }
 
    public static void Foo(){
        string query = 'SELECT Layout.Name, Layout.TableEnumOrId, ProfileId, Profile.Name, RecordTypeId FROM ProfileLayout where recordtypeid=\'01210000000QfWdAAK\'';
 
        String baseURL = URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v41.0/tooling/query?q='+ query.replace(' ', '+');
        HttpResponse resp = null;
        HttpRequest req = new HttpRequest();         
        req.setMethod('GET');        
        req.setHeader('Authorization', 'Bearer ' + UserInfo.getsessionid()); 
        req.setEndpoint(baseURL); 
        Http client = new Http();    
        resp = client.send(req);
        Map<string,object> mso = (Map<string,object>)JSON.deserializeUntyped(resp.getBody());
        for(string key : mso.keySet()){
            system.debug('key='+key);
            system.debug(mso.get(key));
        }
        mso = (Map<string,object>)(((List<object>)mso.get('records'))[0]);
        for(string key : mso.keySet()){
            system.debug('key='+key);
            system.debug(mso.get(key));
        }
    }
}