This method is used to delete files stored as attachments or under S-Drive folders.
List<ResultObject> deleteFiles(List<ID> wipIds, String objectId)
Parameters:
wipIds: List of Salesforce.com IDs of "file" records (either attachments or S-Drive Folder files).
objectId: Id of the parent object. You can use 15-character or 18-character Salesforce.com ID.
Return Value: The method will return a list of ResultObject (Figure 8) which holds delete status information for each file record.
Example code:
List<Id> wipIds = new List<Id>();
wipIds.add(ID.valueOf(uploadRequestInfos .fileWipId));
List<cg.ResultObject> resultObject = cg.SDriveTools.deleteFiles(wipIds, objectId);
If you enable versioning and add a latest version file id into wipIds list, all versions of that file will also be deleted. You can also delete a single old version file by adding its id into wipId list.
Sample code
// 1. Query files regardless of their parent (e.g., marked for deletion)
List filesToDelete = [SELECT Id, cg__Account__c FROM cg__AccountFile__c WHERE add your criteria for deletion here LIMIT 1000];
// 2. Group File IDs by their Parent Account ID
Map> filesByAccount = new Map>();
for (cg__AccountFile__c file : filesToDelete) {
if (!filesByAccount.containsKey(file.cg__Account__c)) {
filesByAccount.put(file.cg__Account__c, new List());
}
filesByAccount.get(file.cg__Account__c).add(http://file.Id );
}
// 3. Iterate through the map and call deleteFiles for each parent group
for (Id accountId : filesByAccount.keySet()) {
List wipIds = filesByAccount.get(accountId);
System.debug('accId ' + accountId + ' wipId ' + wipIds.toString());
// Call the S-Drive API for this specific parent account group
List results = cg.SDriveTools.deleteFiles(wipIds, String.valueOf(accountId));
// 4. Audit results for this batch
for (cg.ResultObject res : results) {
if (res.status=='fail') {
System.debug('Error deleting file ' + res.wipFileId + ' on Account ' + accountId + ': ' + res.errorMessage);
}
}
}