高章伟
2022-02-18 650847118307a1c9ae0ada15b7c69bbf5792c54c
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
// 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 {
        domainInstanceMap = new Map<String, String>();
        domainInstanceMap.put('1', 'ap0');
        domainInstanceMap.put('2', 'ap2');
        domainInstanceMap.put('9', 'ap1');
        domainInstanceMap.put('O', 'cs5');
        domainInstanceMap.put('N', 'cs6');
        domainInstanceMap.put('0', 'cs112');
        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();
        //system.debug('=========='+soap.SessionHeader.sessionId.subString(3, 4)); 
        soap.endpoint_x = 'https://' + domainInstanceMap.get(soap.SessionHeader.sessionId.subString(3, 4)) + '-api.salesforce.com/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 {
            return soap.describeLayout(sObjectType, layoutName, recordTypeIds);
        }
    }
 
    // 編集用レイズとの項目の r,w,wm の情報を 返す
    // Mapには、RecordTypeId(18桁) => {api名 => r,w,wm}
    public static Map<String, Map<String, String>> getEditLayoutItemRW(String sObjectType, String[] recordTypeIds) {
        PartnerSoapSforceCom.DescribeLayoutResult dlr = describeLayout(sObjectType, null, recordTypeIds);
        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;
    }
    
    // SObjectのRecordTypeに応じて、PicklistのValueを抽出
    public static List<String> getPicklistValueByRecordType(String sObjectType, String[] recordTypeIds, String picklistApi) {
        PartnerSoapSforceCom.DescribeLayoutResult dlr = describeLayout(sObjectType, null, recordTypeIds);
        List<String> rtn = new List<String>();
        for (Integer i = 0; i < dlr.layouts.size(); i++) {
            String recordTypeId = dlr.recordTypeMappings[i].recordTypeId;
            PartnerSoapSforceCom.PicklistForRecordType[] ptrList =  dlr.recordTypeMappings[i].picklistsForRecordType;
            for (Integer j = 0; j < ptrList.size(); j++) {
                if (ptrList[j].picklistName == picklistApi) {
                    PartnerSoapSforceCom.PicklistEntry[] pe = ptrList[j].picklistValues;
                    for (Integer k = 0; k < pe.size(); k++) {
                        rtn.add(pe[k].label);
                    }
                    break;
                }
            }
        }
        return rtn;
    }
}