I am trying to convert our existing attachments to contentVersions using batch. But problem I am facing is that some of the attachments are of size greater than 12Mb. So when I try to run my batch I get heap size exceeded exception. Problem is even if a query a attachment body my heap size will exceed the limit. How can I solve this problem. I have some custom logic on attachment records so most of app exchange apps will not work. Can I somehow convert attachments without having to query body from attachment or any other way to not solve this problem.
Attachment attach = [SELECT Id, Name, Body, ContentType, ParentId From Attachment LIMIT 1]; //Insert ContentVersion 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 = attach.Name;//File name with extention cVersion.Origin = 'H';//C-Content Origin. H-Chatter Origin. cVersion.OwnerId = attach.OwnerId;//Owner of the file cVersion.Title = attach.Name;//Name of the file cVersion.VersionData = attach.Body;//File content Insert cVersion; //After saved the Content Verison, get the ContentDocumentId Id conDocument = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cVersion.Id].ContentDocumentId; //Insert ContentDocumentLink ContentDocumentLink cDocLink = new ContentDocumentLink(); cDocLink.ContentDocumentId = conDocument;//Add ContentDocumentId cDocLink.LinkedEntityId = attach.ParentId;//Add attachment parentId cDocLink.ShareType = 'I';//V - Viewer permission. C - Collaborator permission. I - Inferred permission. cDocLink.Visibility = 'InternalUsers';//AllUsers, InternalUsers, SharedUsers Insert cDocLink;