From 0a3f9325599467015aecc4af7543dd10fe70ec56 Mon Sep 17 00:00:00 2001
From: yumenghui <953181891@qq.com>
Date: 星期五, 04 八月 2023 14:14:28 +0800
Subject: [PATCH] 附件修改

---
 force-app/main/default/pages/RentalApplyBR.page |   58 +++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 45 insertions(+), 13 deletions(-)

diff --git a/force-app/main/default/pages/RentalApplyBR.page b/force-app/main/default/pages/RentalApplyBR.page
index e13a18f..a428d4e 100644
--- a/force-app/main/default/pages/RentalApplyBR.page
+++ b/force-app/main/default/pages/RentalApplyBR.page
@@ -52,29 +52,61 @@
             src = srcNew.substring(22, srcNew.length);
             sforce.connection.sessionId = '{!GETSESSIONID()}';
 
-            var record = sforce.connection.query("select id from Attachment where ParentId = \'{!Rental_Apply__c.Id}\' and name = \'BRCode-{!Rental_Apply__c.Name_No__c}\'");
+            // var record = sforce.connection.query("select id from Attachment where ParentId = \'{!Rental_Apply__c.Id}\' and name = \'BRCode-{!Rental_Apply__c.Name_No__c}\'");
+            var record = sforce.connection.query("select ContentDocumentId from ContentDocumentLink where LinkedEntityId = \'{!Rental_Apply__c.Id}\'");
+            console.log("333333333333",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 = \'BRCode-{!Rental_Apply__c.Name_No__c}\'");
+            }
             // 鏃€伀銇傘倢銇扮劇瑕�
             if (record.size == 0) {
-                var atta = new sforce.SObject("Attachment");
-                atta.Name = "BRCode-{!Rental_Apply__c.Name_No__c}";
-                atta.ParentId = "{!Rental_Apply__c.Id}";
-                atta.Body = src;
-                var result = sforce.connection.create([atta]);
+                var version = new sforce.SObject("ContentVersion");
+                version.VersionData = src;
+                version.Title = "BRCode-{!Rental_Apply__c.Name_No__c}";
+                version.ContentLocation = 's';
+                version.PathOnClient= "BRCode-{!Rental_Apply__c.Name_No__c}.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 = "BRCode-{!Rental_Apply__c.Name_No__c}";
+                // 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.BRId__c = result[0].id;
+                es.BRId__c = versionResult[0].id;
                 result = sforce.connection.update([es]);
             //鐢宠涔︽洿鏂板け璐ョ殑璇濆啀娆℃洿鏂�
             } else if ("{!Rental_Apply__c.BRId__c}" == '' || "{!Rental_Apply__c.BRId__c}" == null) {
-                var records = record.getArray("records");
-                var es = new sforce.SObject("Rental_Apply__c");
-                es.Id = "{!Rental_Apply__c.Id}";
-                es.BRId__c = records[0].Id;
-                result = sforce.connection.update([es]);
+                // var records = record.getArray("records");
+                // var es = new sforce.SObject("Rental_Apply__c");
+                // es.Id = "{!Rental_Apply__c.Id}";
+                // es.BRId__c = records[0].Id;
+                // result = sforce.connection.update([es]);
                 // TODO Name澶夈倧銈嬪彲鑳芥�с亗銈娿�乽psert銇仐銇俱仚
+                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"); 
+                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]);
+                }
             } else {
-                alert(record[0].id);
+                // alert(record[0].id);
             }
         }
         // BRcode鐢熸垚寰屻�丄ttachment銈掍綔鎴�

--
Gitblit v1.9.1