buli
2023-07-14 36d15f189de2e83ce2576715dac30c3c260388dd
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<<<<<<< HEAD
global without sharing class Batch_FixAttachmentToFiles implements Database.Batchable<sObject> {
    // Id batchJobId = Database.executeBatch(new Batch_FixAttachmentToFiles('Consumable_order__c',Datetime.newInstance(2023, 1, 1, 8, 0, 0),Datetime.newInstance(2024, 1, 1, 8, 0, 0)),2000);
    private Datetime creStartDate = null;
    private Datetime creEndDate = null;
    private String objectApiName = null;
    private Set<String> parentIds = new Set<String>(); //Add by Li Jun 20230703
=======
global without sharing class Batch_FixAttachmentToFiles implements Database.Batchable<sObject>{
    // Id batchJobId = Database.executeBatch(new Batch_FixAttachmentToFiles('Consumable_order__c',Datetime.newInstance(2023, 1, 1, 8, 0, 0),Datetime.newInstance(2024, 1, 1, 8, 0, 0)),2000);
    private Datetime creStartDate = null;
    private Datetime creEndDate = null;
    private String objectApiName  = null;
    private Set<String> parentIds = new Set<String>();//Add by Li Jun 20230703 
>>>>>>> LEXCommunityLiJun
    public Batch_FixAttachmentToFiles(String objectType, Datetime startTime, Datetime endTime) {
        creStartDate = startTime;
        creEndDate = endTime;
        objectApiName = objectType;
    }
    //Add by Li Jun 20230703 Start
    public Batch_FixAttachmentToFiles(Set<String> parentIds) {
        this.parentIds = parentIds;
    }
<<<<<<< HEAD
    //Add by Li Jun 20230703 End
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String queryObject = 'SELECT Id, Name, OwnerId, ParentId, Parent.Name, Parent.Type, Body, CreatedDate, CreatedById FROM Attachment WHERE Parent.Type =:objectApiName AND CreatedDate >=:creStartDate AND CreatedDate <:creEndDate Order by CreatedDate ASC';
        if (parentIds.size() > 0) {
=======
     //Add by Li Jun 20230703 End
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String queryObject = 'SELECT Id, Name, OwnerId, ParentId, Parent.Name, Parent.Type, Body, CreatedDate, CreatedById FROM Attachment WHERE Parent.Type =:objectApiName AND CreatedDate >=:creStartDate AND CreatedDate <:creEndDate Order by CreatedDate ASC';
        if(parentIds.size() > 0){
>>>>>>> LEXCommunityLiJun
            queryObject = 'SELECT Id, Name, OwnerId, ParentId, Parent.Name, Parent.Type, Body, CreatedDate, CreatedById FROM Attachment WHERE ParentId in:parentIds';
        }
        return Database.getQueryLocator(queryObject);
    }
 
    global void execute(Database.BatchableContext BC, List<Attachment> scope) {
        List<ContentVersion> insertContents = new List<ContentVersion>();
<<<<<<< HEAD
        Map<string, id> nameParentMaps = new Map<string, id>();
        Set<String> attachmentIds = new Set<String>();
        for (Attachment att : scope) {
            nameParentMaps.put(att.Name, att.ParentId);
            attachmentIds.add(att.Id);
            ContentVersion cVersion = new ContentVersion();
            cVersion.ContentLocation = 'S'; //S-Document is in Salesforce. E-Document is outside of Salesforce. L-Document is on a Social Netork.
            cVersion.PathOnClient = att.Name; //File name with extention
            cVersion.Origin = 'C'; //C-Content Origin. H-Chatter Origin.
            cVersion.Title = att.Name; //Name of the file
            cVersion.VersionData = att.Body; //File content
            insertContents.add(cVersion);
        }
        if (insertContents.isEmpty()) {
=======
        Map<string,id> nameParentMaps = new Map<string,id>();  
        Set<String> attachmentIds = new Set<String>();   
        for (Attachment att : scope) {
            nameParentMaps.put(att.Id, att.ParentId);
            attachmentIds.add(att.Id);   
            ContentVersion cVersion = new ContentVersion();
            cVersion.ContentLocation = 'S';     //S-Document is in Salesforce. E-Document is outside of Salesforce. L-Document is on a Social Netork.
            cVersion.PathOnClient = att.Id;   //File name with extention
            cVersion.Origin = 'C';              //C-Content Origin. H-Chatter Origin.   
            cVersion.Title = att.Name;          //Name of the file
            cVersion.VersionData = att.Body;    //File content
            insertContents.add(cVersion);                            
        }
        if(insertContents.isEmpty()){
>>>>>>> LEXCommunityLiJun
            return;
        }
        //Insert insertContents;
        List<Database.SaveResult> saveResultsContents = Database.insert(insertContents, false);
<<<<<<< HEAD
        insertLog(saveResultsContents, attachmentIds);
        set<Id> contentIds = new Set<Id>();
        for (ContentVersion cv : insertContents) {
            contentIds.add(cv.id);
        }
        List<ContentVersion> conDocuments = [
            SELECT ContentDocumentId, Title
            FROM ContentVersion
            WHERE Id IN :contentIds
        ];
        List<ContentDocumentLink> insertDocLinks = new List<ContentDocumentLink>();
        for (ContentVersion cv : conDocuments) {
            ContentDocumentLink cDocLink = new ContentDocumentLink();
            cDocLink.ContentDocumentId = cv.ContentDocumentId; //Add ContentDocumentId
            cDocLink.LinkedEntityId = nameParentMaps.get(cv.Title); //Add attachment parentId
            cDocLink.ShareType = 'V'; //V - Viewer permission. C - Collaborator permission. I - Inferred permission.
=======
        insertLog(saveResultsContents,attachmentIds);
        set<Id> contentIds = new set<Id>();
        for(ContentVersion cv : insertContents){
            contentIds.add(cv.id);
        }
        List<ContentVersion> conDocuments = [SELECT ContentDocumentId, Title,PathOnClient FROM ContentVersion WHERE Id in: contentIds];
        List<ContentDocumentLink> insertDocLinks = new List<ContentDocumentLink>();  
        for(ContentVersion cv : conDocuments){
            ContentDocumentLink cDocLink = new ContentDocumentLink();
            cDocLink.ContentDocumentId = cv.ContentDocumentId;          //Add ContentDocumentId
            cDocLink.LinkedEntityId = nameParentMaps.get(cv.PathOnClient);     //Add attachment parentId
            cDocLink.ShareType = 'V';                                   //V - Viewer permission. C - Collaborator permission. I - Inferred permission.
>>>>>>> LEXCommunityLiJun
            cDocLink.Visibility = 'AllUsers';
            insertDocLinks.add(cDocLink);
        }
        //Insert insertDocLinks;
        List<Database.SaveResult> saveResultsLinks = Database.insert(insertDocLinks, false);
<<<<<<< HEAD
        insertLog(saveResultsLinks, attachmentIds);
    }
 
    global void insertLog(List<Database.SaveResult> saveResults, Set<String> attachmentIds) {
        Transaction_Log__c traLog = new Transaction_Log__c();
        List<Map<String, String>> logMapList = new List<Map<String, String>>();
=======
        insertLog(saveResultsLinks,attachmentIds);
    }
 
    global void insertLog(List<Database.SaveResult> saveResults,Set<String> attachmentIds) {
        Transaction_Log__c traLog = new Transaction_Log__c();
        List<Map<String,String>> logMapList = new List<Map<String,String>>();
>>>>>>> LEXCommunityLiJun
        for (Database.SaveResult result : saveResults) {
            String recordId = result.getId();
            if (!result.isSuccess()) {
                for (Database.Error error : result.getErrors()) {
<<<<<<< HEAD
                    Map<String, String> logMap = new Map<String, String>();
                    String errorMsg = error.getMessage();
                    logMap.put('recordId', recordId);
                    logMap.put('errorMsg', errorMsg);
=======
                    Map<String,String> logMap = new Map<String,String>();
                    String errorMsg = error.getMessage();
                    logMap.put('recordId',recordId);
                    logMap.put('errorMsg',errorMsg);
>>>>>>> LEXCommunityLiJun
                    logMapList.add(logMap);
                }
            }
        }
<<<<<<< HEAD
        if (logMapList.size() > 0) {
            Map<String, String> attachmentIdMap = new Map<String, String>();
            attachmentIdMap.put('attachmentIds', JSON.serialize(attachmentIds));
            logMapList.add(attachmentIdMap);
            traLog.Response__c = JSON.serialize(logMapList);
            traLog.Module__c = 'Attachment COnvert Transaction ';
            insert traLog;
        }
    }
 
    global void finish(Database.BatchableContext BC) {
    }
}
=======
        if(logMapList.size() > 0){
            Map<String,String> attachmentIdMap = new Map<String,String>();
            attachmentIdMap.put('attachmentIds',JSON.serialize(attachmentIds));
            logMapList.add(attachmentIdMap);
            traLog.Response__c = JSON.serialize(logMapList);
            traLog.Module__c = 'Attachment COnvert Transaction ';
            Insert traLog;
        }
    }
 
 
 
    global void finish(Database.BatchableContext BC) {
 
    }
}
>>>>>>> LEXCommunityLiJun