public without sharing class CG_WorkOrderFilesCopier{
public void processWorkOrders(List<SVMXC__Service_Order__c> workOrdersToProcess) {
MAP<String,AHP_Countries__c> validCountryList=new MAP<String,AHP_Countries__c>();
//SET<ID> WOIDset=new SET<ID>();
list<AHP_Countries__c> validCountries=[Select ID,Name,Timezone__c from AHP_Countries__c];
for(AHP_Countries__c ac: validCountries){
validCountryList.put(ac.Name,ac);
}
List<SVMXC__Service_Order__c> specialCountryWorkOrders = new List<SVMXC__Service_Order__c>();
for (SVMXC__Service_Order__c workOrder : workOrdersToProcess) {
if (validCountryList.containsKey(workOrder.SVMXC__Site__r.CG_Country__c)) {
specialCountryWorkOrders.add(workOrder);
}
}
List<ContentDocumentLink> insertLinkList = new List<ContentDocumentLink>();
List<ContentDocumentLink> linkList;
Set<Id> caseIds = new Set<Id>();
Map<Id, SVMXC__Service_Order__c> woInCaseMap = new Map<Id, SVMXC__Service_Order__c>();
//Checking if WO is from the Valid Countries -
//List<SVMXC__Service_Order__c> woList=[SELECT ID,SVMXC__Case__c,CG_Parent_Work_Order__c,SVMXC__Site__r.CG_Country__c FROM SVMXC__Service_Order__c where SVMXC__Site__r.CG_Country__c IN :validCountryList.keySet()];
//.debug('WOLIST SIZE...'+woList.size());
//(woList!=null && woList.size()>0){
//(SVMXC__Service_Order__c wo : woList){
//WOIDset.add(wo.ID);
//}
//}*/
// if (!specialCountryWorkOrders.isEmpty()) {
if (workOrdersToProcess == null || workOrdersToProcess.size() == 0) {
System.debug(' No workOrders to process ... Returning ...');
return;
}
/* List<ContentDocumentLink> insertLinkList = new List<ContentDocumentLink>();
List<ContentDocumentLink> linkList;
Set<Id> caseIds = new Set<Id>();
Map<Id, SVMXC__Service_Order__c> woInCaseMap = new Map<Id, SVMXC__Service_Order__c>();*/
for (SVMXC__Service_Order__c workOrderToProcess : specialCountryWorkOrders) {
// if(validCountryList.containsKey(workOrderToProcess.SVMXC__Site__r.CG_Country__c)) {
System.debug('UTC_SM_WorkOrderAttachmentsCopier.processWorkOrders() -> workOrderToProcess[' + workOrderToProcess + ']');
if (workOrderToProcess.SVMXC__Case__c == null || workOrderToProcess.CG_Parent_Work_Order__c != null) {
continue;
}
//
caseIds.add(workOrderToProcess.SVMXC__Case__c);
woInCaseMap.put(workOrderToProcess.SVMXC__Case__c, workOrderToProcess);
// }
}
System.debug('UTC_SM_WorkOrderAttachmentsCopier.processWorkOrders() -> caseIds[' + caseIds + ']');
System.debug('UTC_SM_WorkOrderAttachmentsCopier.processWorkOrders() -> woInCaseMap[' + woInCaseMap + ']');
if (caseIds.size() > 0) {
linkList = [SELECT Id, ContentDocumentId, LinkedEntityId
FROM ContentDocumentLink
WHERE LinkedEntityId IN :caseIds];
/* Set<Id> existingContentDocumentIds = new Set<Id>();
for (ContentDocumentLink link : linkList) {
existingContentDocumentIds.add(link.ContentDocumentId);
}*/
System.debug('UTC_SM_WorkOrderAttachmentsCopier.processWorkOrders() -> linkList[' + linkList + ']');
for (ContentDocumentLink link : linkList) {
// if (link.ContentDocumentId != null && !existingContentDocumentIds.contains(link.ContentDocumentId)) {
System.debug('UTC_SM_WorkOrderAttachmentsCopier.processWorkOrders() -> Building Attachments for WO: [' + woInCaseMap.get(link.LinkedEntityId).Id + ']');
ContentDocumentLink newLink = new ContentDocumentLink(
ContentDocumentId = link.ContentDocumentId,
LinkedEntityId = woInCaseMap.get(link.LinkedEntityId).Id,
ShareType = 'V'
);
insertLinkList.add(newLink);
// }
}
if (insertLinkList.size() > 0) {
system.debug('insertLinkList@@@@@@@'+insertLinkList);
insert insertLinkList;
}
}
}
//}
}
Hi @mounika Gottula,
You can check below for further troubleshooting the issue:
- Verify if the query to retrieve ContentDocumentLink records (linkList) returns results by adding debug statements to show the size of linkList.
- Check conditions inside the loop that constructs ContentDocumentLink records to see if they allow valid records for insertion.
- Make sure the user executing the code has permissions to insert ContentDocumentLink records and access related objects.
Thanks.