liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
<apex:page standardController="Consum_Apply__c">
    <apex:includeScript value="{!URLFOR($Resource.qrcode)}"/>
    <apex:includeScript value="{!URLFOR($Resource.connection20)}"/>
    <apex:includeScript value="{!URLFOR($Resource.apex20)}"/>
 
    <div id="qrcode"></div>
    <script type="text/javascript">
        function createAttachment() {
            var div = document.getElementById("qrcode");
            var img = div.getElementsByTagName("img");
            var src = img[0].src;
            src = src.substring(22, src.length);
 
            sforce.connection.sessionId = '{!GETSESSIONID()}';
 
            // var record = sforce.connection.query("select id from Attachment where ParentId = \'{!Consum_Apply__c.Id}\' and name = \'QRCode-{!Consum_Apply__c.Name}\'");
            var record = sforce.connection.query("select ContentDocumentId from ContentDocumentLink where LinkedEntityId = \'{!Consum_Apply__c.Id}\'");
            // 不存在qr文件时,新生成一个,并连到申请单上
            if(record.size > 0){
                var records = record.getArray("records");
                var idList = records.map(obj => "'" + obj.ContentDocumentId + "'").join(",");
                record = sforce.connection.query("select Id from ContentDocument where Id in (" + idList + ") and Title = \'QRCode-{!Consum_Apply__c.Name}\'");
                console.log("select Id from ContentDocument where Id in (" + idList + ") and Title = \'QRCode-{!Consum_Apply__c.Name}\'");
            }
            if (record.size == 0) {
                // var atta = new sforce.SObject("Attachment");
                // atta.Name = "QRCode-{!Consum_Apply__c.Name}";
                // atta.ParentId = "{!Consum_Apply__c.Id}";
                // atta.Body = src;
                // var result = sforce.connection.create([atta]);
 
                var version = new sforce.SObject("ContentVersion");
                version.Title = "QRCode-{!Consum_Apply__c.Name}";
                version.VersionData = src;
                version.ContentLocation = "s";
                version.PathOnClient = "QRCode-{!Consum_Apply__c.Name}.jpg";
                var result = sforce.connection.create([version]);
                version = sforce.connection.query("select Id,ContentDocumentId from ContentVersion where Id = \'" + result[0].id + "\'");
                version = version.getArray("records"); 
                var link = new sforce.SObject("ContentDocumentLink");
                link.LinkedEntityId = "{!Consum_Apply__c.Id}";
                link.ContentDocumentId = version[0].ContentDocumentId;
                link.ShareType = 'I';
                link.Visibility = 'AllUsers';
                result = sforce.connection.create([link]);
 
                var es = new sforce.SObject("Consum_Apply__c");
                es.Id = "{!Consum_Apply__c.Id}";
                es.QRId__c = version[0].Id;
                result = sforce.connection.update([es]);
            }
            else {
                var records = record.getArray("records");
                var version = new sforce.SObject("ContentVersion");
                version = sforce.connection.query("select Id,ContentDocumentId from ContentVersion where ContentDocumentId = \'" + records[0].Id + "\'");
                version = version.getArray("records"); 
                // 存在qr时,绑到申请单上
                if(version[0].Id != "{!Consum_Apply__c.QRId__c}"){
                    var es = new sforce.SObject("Consum_Apply__c");
                    es.Id = "{!Consum_Apply__c.Id}";
                    es.QRId__c = version[0].Id;
                    result = sforce.connection.update([es]);
                }
            }
        }
 
        var qrcode = new QRCode(document.getElementById("qrcode"), {
            text: "{!Consum_Apply__c.Name}",
            width: 128,
            height: 128,
            colorDark : "#000000",
            colorLight : "#ffffff",
            correctLevel : QRCode.CorrectLevel.H
        });
 
        // qrcode生成後、Attachmentを作成
        setTimeout("createAttachment()", 1000);
    </script>
</apex:page>