高章伟
2022-02-24 81b0892b8c6aa064a9e8dbca09a8f7f1eb6de40e
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// UnitTestも考慮してPartnerSoapSforceCom.cls をwrapperする
public with sharing class SoapApi {
    private static final PartnerSoapSforceCom.Soap soap;
 
    // https://developer.salesforce.com/forums/ForumsMain?id=906F000000092a6IAA
    private static Map<String, String> domainInstanceMap { get; private set; }
    static {
        soap = new PartnerSoapSforceCom.Soap();
        soap.SessionHeader = new PartnerSoapSforceCom.SessionHeader_element();
        // This is important, give session id, to let the call work.
        soap.SessionHeader.sessionId = UserInfo.getSessionId();
        soap.endpoint_x = URL.getSalesforceBaseUrl().toExternalForm() + '/services/Soap/u/33.0';
    }
 
    /*
    PartnerSoapSforceCom.DescribeLayoutResult dlr = SoapApi.describeLayout('Repair__c', null, new String[]{'01210000000QmS9'});
    System.debug('layoutId=' + dlr.layouts[0].id);
    System.debug('recordTypeId=' + dlr.recordTypeMappings[0].recordTypeId);
    */
    public static PartnerSoapSforceCom.DescribeLayoutResult describeLayout(String sObjectType, String layoutName, String[] recordTypeIds) {
        if (System.Test.isRunningTest()) {
            // UnitTest 用
            PartnerSoapSforceCom.DescribeLayoutResult dlr = new PartnerSoapSforceCom.DescribeLayoutResult();
            PartnerSoapSforceCom.RecordTypeMapping rtMap = new PartnerSoapSforceCom.RecordTypeMapping();
            PartnerSoapSforceCom.DescribeLayout editLayout = new PartnerSoapSforceCom.DescribeLayout();
            PartnerSoapSforceCom.DescribeLayoutSection editLayoutSection = new PartnerSoapSforceCom.DescribeLayoutSection();
            PartnerSoapSforceCom.DescribeLayoutRow layoutRow = new PartnerSoapSforceCom.DescribeLayoutRow();
            PartnerSoapSforceCom.DescribeLayoutItem layoutItem = new PartnerSoapSforceCom.DescribeLayoutItem();
            PartnerSoapSforceCom.DescribeLayoutComponent layoutComponent = new PartnerSoapSforceCom.DescribeLayoutComponent();
            dlr.recordTypeMappings = new List<PartnerSoapSforceCom.RecordTypeMapping>();
            dlr.recordTypeMappings.add(rtMap);
            rtMap.recordTypeId = 'recordTypeId';
            dlr.layouts = new List<PartnerSoapSforceCom.DescribeLayout>();
            dlr.layouts.add(editLayout);
            editLayout.editLayoutSections = new List<PartnerSoapSforceCom.DescribeLayoutSection>();
            editLayout.editLayoutSections.add(editLayoutSection);
            editLayoutSection.layoutRows = new List<PartnerSoapSforceCom.DescribeLayoutRow>();
            editLayoutSection.layoutRows.add(layoutRow);
            layoutRow.layoutItems = new List<PartnerSoapSforceCom.DescribeLayoutItem>();
            layoutRow.layoutItems.add(layoutItem);
            layoutItem.layoutComponents = new List<PartnerSoapSforceCom.DescribeLayoutComponent>();
            layoutItem.layoutComponents.add(layoutComponent);
            return dlr;
        } else {
            System.debug('=====1====='+sObjectType);
        System.debug('=====2====='+recordTypeIds);
        System.debug('=====3====='+layoutName);
            return soap.describeLayout(sObjectType, layoutName, recordTypeIds);
        }
    }
 
    // 編集用レイズとの項目の r,w,wm の情報を 返す
    // Mapには、RecordTypeId(18桁) => {api名 => r,w,wm}
    // 如果Object中设定记录类型,该方法不能取出对应记录类型的项目,修改后方法名:getEditRWByRecordType --下一个方法
    public static Map<String, Map<String, String>> getEditLayoutItemRW(String sObjectType, String[] recordTypeIds) {
        System.debug('=====1====='+sObjectType);
        System.debug('=====2====='+recordTypeIds);
        PartnerSoapSforceCom.DescribeLayoutResult dlr = describeLayout(sObjectType, null, recordTypeIds);
         System.debug('=====1====='+sObjectType);
        System.debug('=====2=====--------');
        Map<String, Map<String, String>> rtn = new Map<String, Map<String, String>>();
        for (Integer lidx = 0; lidx < dlr.layouts.size(); lidx++) {
            String recordTypeId = dlr.recordTypeMappings[lidx].recordTypeId;
            Map<String, String> rtnInner = new Map<String, String>();
            rtn.put(recordTypeId, rtnInner);
            System.debug('recordTypeId=' + recordTypeId);
            for (PartnerSoapSforceCom.DescribeLayoutSection section : dlr.layouts[lidx].editLayoutSections) {
                for (PartnerSoapSforceCom.DescribeLayoutRow row : section.layoutRows) {
                    for (PartnerSoapSforceCom.DescribeLayoutItem item : row.layoutItems) {
                        if (item.layoutComponents != null && item.layoutComponents.size() > 0
                                && String.isBlank(item.layoutComponents[0].value) == false) {
                            rtnInner.put(item.layoutComponents[0].value, 'r');
                            if (item.editableForUpdate) {
                                rtnInner.put(item.layoutComponents[0].value, 'w');
                            }
                            if (item.required) {
                                rtnInner.put(item.layoutComponents[0].value, 'wm');
                            }
                        }
                    }
                }
            }
        }
        return rtn;
    }
 
    // 編集用レイズとの項目の r,w,wm の情報を 返す
    // Mapには、RecordTypeId(18桁) => {api名 => r,w,wm}
    public static Map<String, Map<String, String>> getEditRWByRecordType(String sObjectType, String[] recordTypeIds) {
        PartnerSoapSforceCom.DescribeLayoutResult dlr = describeLayout(sObjectType, null, recordTypeIds);
        Map<String, Map<String, String>> rtn = new Map<String, Map<String, String>>();
        //
        Map<String, PartnerSoapSforceCom.DescribeLayout> layputMap = new Map<String, PartnerSoapSforceCom.DescribeLayout>();
        for (PartnerSoapSforceCom.DescribeLayout layout : dlr.layouts) {
            layputMap.put(layout.id, layout);
        }
        //for (Integer lidx = 0; lidx < dlr.layouts.size(); lidx++) {
        for (Integer lidx = 0; lidx < dlr.recordTypeMappings.size(); lidx++) {
            String recordTypeId = dlr.recordTypeMappings[lidx].recordTypeId;
            Map<String, String> rtnInner = new Map<String, String>();
            rtn.put(recordTypeId, rtnInner);
            System.debug('recordTypeId=' + recordTypeId);
            PartnerSoapSforceCom.DescribeLayout layout = layputMap.get(dlr.recordTypeMappings[lidx].layoutId);
            //for (PartnerSoapSforceCom.DescribeLayoutSection section : dlr.layouts[lidx].editLayoutSections) {
            for (PartnerSoapSforceCom.DescribeLayoutSection section : layout.editLayoutSections) {
                for (PartnerSoapSforceCom.DescribeLayoutRow row : section.layoutRows) {
                    for (PartnerSoapSforceCom.DescribeLayoutItem item : row.layoutItems) {
                        if (item.layoutComponents != null && item.layoutComponents.size() > 0
                                && String.isBlank(item.layoutComponents[0].value) == false) {
                            rtnInner.put(item.layoutComponents[0].value, 'r');
                            if (item.editableForUpdate) {
                                rtnInner.put(item.layoutComponents[0].value, 'w');
                            }
                            if (item.required) {
                                rtnInner.put(item.layoutComponents[0].value, 'wm');
                            }
                        }
                    }
                }
            }
        }
        return rtn;
    }
}