buli
2022-04-08 b6f2c55d21463def425048aba48bed273156e9a9
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
public class DeveloperUtility {
    
    public static List<HTTPResponse> CreateFields(string sobject_name,string [] fields, boolean create_field,boolean create_config){
        
        PI_Policy_Configuration__c ppc = null;
        if(create_config){
            List<PI_Policy_Configuration__c> ppcs = [select id from PI_Policy_Configuration__c where Sobject_Type__c = :sobject_name];
            if(ppcs.size()>0){
                ppc = ppcs[0];
            }
        }
        
        List<PI_Field_Policy_Detail__c> ds = new List<PI_Field_Policy_Detail__c>();
        List<HTTPResponse> results = new List<HTTPResponse>();
        for(string f : fields){
            string old_label = f.removeEnd('__c').replace('_',' ');
            string label = old_label + ' Encrypted';
            string name =  label.replace(' ','_')+'__c';
            string description = '';
            system.debug('old_label='+old_label);
            system.debug('label='+label);
            system.debug('name='+name);
            if(create_field){
                system.debug(CreateField(sobject_name,label,name,description,'Text'));
            }
            
            
            if(create_config){
                PI_Field_Policy_Detail__c d = new PI_Field_Policy_Detail__c();
                d.SF_Field_API_Name__c = f;
                d.SF_Field_Encrypted_API__c = name;
                d.AWS_Field_API__c = f;
                d.AWS_Encrypted_Field_API__c = name;
                d.Field_Type__c = 'String';
                d.Enable_Encrypt__c = true;
                if(ppc!=null){
                    d.PI_Policy_Configuration__c = ppc.Id;
                }
                
                ds.add(d);
            }
            
        }
        
        if(ppc==null){
            ppc = new PI_Policy_Configuration__c();
            ppc.Sobject_Type__c = sobject_name;
            insert ppc;
            for(PI_Field_Policy_Detail__c d : ds){
                d.PI_Policy_Configuration__c = ppc.Id;
            }
        }
        
        insert ds;
        
        return results;
    }
    
    
    public static HTTPResponse CreateField(string sobject_name, string label,string name,string description,string type){
        HTTP h = new HTTP();
        HTTPRequest req = new HTTPRequest();
        req.setMethod('POST');
        req.setHeader('Content-Type', 'text/xml');
        req.setHeader('SOAPAction', 'create');
        
        
        //string sobject_name = 'Contact';
        //string label = 'Title';
        //string name =  'Title'+'_Encrypted__c';
        //string description = '';
        
        //string type = 'Text';
        boolean typeSpecified = true;
        
        String b = '<?xml version="1.0" encoding="UTF-8"?>';
        b += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';
        b += '<soapenv:Header>';
        b += '<ns1:SessionHeader soapenv:mustUnderstand="0" xmlns:ns1="http://soap.sforce.com/2006/04/metadata">';
        b += '<ns1:sessionId>' + UserInfo.getSessionId() + '</ns1:sessionId>';
        b += '</ns1:SessionHeader>';
        b += '</soapenv:Header>';
        b += '<soapenv:Body>';
        b += '<create xmlns="http://soap.sforce.com/2006/04/metadata">';
        b += '<metadata xsi:type="ns2:CustomField" xmlns:ns2="http://soap.sforce.com/2006/04/metadata">';
        b += '<type>'+type+'</type>';
        b += '<fullName>'+sobject_name+'.'+name+'</fullName>';
        b += '<label>'+label+'</label>';
        b += '<length>255</length>';
        b += '<description>'+type+'</description>';
        b += '</metadata>';
        b += '</create>';
        b += '</soapenv:Body>';
        b += '</soapenv:Envelope>';
        
        req.setBody(b);
        req.setCompressed(false);
        req.setEndpoint('https://'+System.URL.getOrgDomainUrl().getHost()+'/services/Soap/m/25.0');
        HTTPResponse resp = h.send(req);
        System.debug(resp.getStatus());
        System.debug(resp.getBody());
        return resp;
    }
}