binxie
2024-01-22 4688e276eee13cd2e521aa55afb5725cb0fda270
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
<apex:page standardController="Rental_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 = \'{!Rental_Apply__c.Id}\' and name = \'QRCode-{!Rental_Apply__c.Name}\'");
            // //20231027 ymh添加注释  lighting修改附件上传 start
            var record = sforce.connection.query("select ContentDocumentId from ContentDocumentLink where LinkedEntityId = \'{!Rental_Apply__c.Id}\'");
            console.log("record.size--------999------------",record.size);
            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-{!Rental_Apply__c.Name}\'");
            }           
             // 既にあれば無視
             console.log("record.size--------888------------",record.size+'~'+record.getArray("records"));
            if (record.size == 0) {
                console.log("record.size--------777------------",record.size);
                var version = new sforce.SObject("ContentVersion");
                version.VersionData = src;
                version.Title = "QRCode-{!Rental_Apply__c.Name}";
                version.ContentLocation = 's';
                version.PathOnClient= "QRCode-{!Rental_Apply__c.Name}.jpg";
                var versionResult = sforce.connection.create([version]);
                record = sforce.connection.query("SELECT ContentDocumentId FROM ContentVersion WHERE Id =\'" + versionResult[0].id + "\'");
                var records = record.getArray("records");
                var link = new sforce.SObject("ContentDocumentLink");
                link.ContentDocumentId = records[0].ContentDocumentId;
                link.LinkedEntityId = "{!Rental_Apply__c.Id}";
                link.ShareType = 'I';
                link.Visibility = 'AllUsers';
                var result = sforce.connection.create([link]);
 
                // var atta = new sforce.SObject("Attachment");
                // atta.Name = "QRCode-{!Rental_Apply__c.Name}";
                // atta.ParentId = "{!Rental_Apply__c.Id}";
                // atta.Body = src;
                // var result = sforce.connection.create([atta]);
 
                var es = new sforce.SObject("Rental_Apply__c");
                es.Id = "{!Rental_Apply__c.Id}";
                es.QRId__c = versionResult[0].id;
                result = sforce.connection.update([es]);
                // window.location = '/' + '{!Rental_Apply__c.Id}';
            //申请书更新失败的话再次更新
            } else {
                // 20240113 ljh update start 
                var records = record.getArray("records");
                console.log('zheliupdate:'+records[0].Id);
                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 != "{!Rental_Apply__c.QRId__c}"){
                    var es = new sforce.SObject("Rental_Apply__c");
                    es.Id =  "{!Rental_Apply__c.Id}";
                    es.QRId__c = version[0].Id;
                    result = sforce.connection.update([es]);
                }
                // 20240113 ljh update end
            }
            // //20231027 ymh添加注释  lighting修改附件上传 end
        }
 
        var qrcode = new QRCode(document.getElementById("qrcode"), {
            text: "{!Rental_Apply__c.Name}",
            width: 128,
            height: 128,
            colorDark : "#000000",
            colorLight : "#ffffff",
            correctLevel : QRCode.CorrectLevel.H
        });
 
        // qrcode生成後、Attachmentを作成
        setTimeout("createAttachment()", 1000);
    </script>
</apex:page>