Some days ago I got a message asking me if it was possible to move from Attachments to Files in Salesforce using the #clicksnotcode tool VisualFlow and after some researches I would say no, this is not possible without code. But if you find the way, please, please, please, leave me a message so that I can update this post.
So, what is this post about? My researches and how I got that the answer is no.
First of all, the use case. As you know we cannot create Attachments in Lightning Experience, only Files are available in LEX, so anything that we want to keep should be also migrated.
- Manually? That could be a really expensive option, and tired, to be honest.
- Via code? Yes!! I like this option. At the end, a File is a Salesforce Object, ContentVersion , and as you can find on this blog, with a really simple piece of code you can create a new File record. Below piece adds the query to an attachment. After that, find the image with the new file and also the result of a query done in Workbench.
List<Attachment> attList = [SELECT Body,BodyLength,Name,ParentId FROM Attachment limit 1]; Attachment att = attList.get(0); String fileContent = att.body.toString(); ContentVersion conVer = new ContentVersion(); conVer.PathOnClient = 'MigratedFile.txt'; conVer.Title = 'MigratedFile.txt'; conVer.Description = att.Name + ' migrated'; conVer.VersionData = EncodingUtil.base64Decode(fileContent); conVer.FirstPublishLocationId = '001B000000hYespIAC'; insert conVer;
- Any other way? Trying to find an answer, I found this amazing blog where Doug mention a tool delivered by Summer ’17 that helps you convert Attachment into Files.
And what about VisualFlow?
I created something really simple, 4 steps flow. But it has a welcome and a thanks screen, so actually the job is done in 2 steps.
First one is Record Lookup where I retrieve the single attachment that I have in my organization. Please, keep in mind that if you have a list of Attachments, and use a Record Lookup, it will return a single record, but if your idea is to retrieve all attachments, you need to use a Fast Record Lookup step.
This step is going to store data into the variable that I created, and all of them are similar, Text variables.
The second step is to create the ContentVersion record, assigning the variable data that I got on the previous step.
And that is all. Yes, so simple.
However, when I run it I got an error and this email where I can see that there is an issue on VersionData field.
The reason is simple. Attachment Body returns a Blob that I need to convert into String in order to be able to encode and pass to the Content file.
.... String fileContent = att.body.toString(); .... conVer.VersionData = EncodingUtil.base64Decode(fileContent); ....
Is there a way to fix it? No as far as I could find. My first idea was to check what the variable was returning, adding a middle step to show the body value
And as I expected that is not my body text
So the next thought was to try to create a formula in flow or make any transform, something that could help me, but I couldn’t find any.
So I think that the only way to do it is via some Apex code. We can create a class that has InvocableMethod annotation and do the transform there and call the class as it was an step, but, to be honest, why do I want to do it when I can run the whole transform from an apex class as I explained before?
At the end, I would advice to create your own ApexClass that execute the process to do the migration if you do not want to use the tool that Salesforce provides.