Search code examples
salesforcesalesforce-lightning

Attachment Migration from legacy Salesforce org (Classic version) to new Salesforce org (Lightning verison)


we are working on a data migration project between two salesforce platform. The legacy salesforce is still in classic and it has 3000 + attachment to either case or account object. The goal is to migrate the object with its attachment and store it as file in the lightning version. I tried SFDMU tool and enable (both org) upload file as File not Attachment settings. However, it still shows as attachment in target org which is not searchable and showing on the UI.
we try to avoid to install any new management package or application in the legacy org, the reason we pick SFDMU because the developer can install and run locally.

Can anyone provide some guidance? Thank you so much!


Solution

  • Can you fix them after upload?

    This is ugly and evil but should work good enough in practice. You may have to tweak it to set sharing/visibility fields on the created content document. The 6291456 is 6MB

    List<Attachment> source = [SELECT Id
    FROM Attachment
    WHERE Parent.Type IN ('Case', 'Account') AND BodyLength < 6291456
    ORDER by BodyLength DESC
    LIMIT 50];
    
    /*  This looks evil (query in a loop, insert in a loop)
        but the idea is that the actual file payload will be kept in the loop as a local variable
        so should be used, inserted and discarded without adding up to heap size and hitting the limit. A proper insert outside of the loop would blow it.
    */
    for(Attachment a : source){
        Attachment temp = [SELECT Parentid, Name, Body
        FROM Attachment
        WHERE Id = :a.Id];
        insert new ContentVersion(
            Title = temp.Name,
            PathOnClient = temp.Name,
            VersionData = temp.Body,
            FirstPublishLocationId = temp.ParentId
        );
    }
    delete source;
    Assert.fail('Just checking, please rollback');
    

    It's not exact science, in my test org the top 50 attachments it found had sizes from 6280972 to 5654213 but after running this the warnings were like this, slightly smaller max heap size.

      Number of SOQL queries: 51 out of 100 ******* CLOSE TO LIMIT
      Number of query rows: 100 out of 50000
      Number of DML statements: 51 out of 150
      Number of DML rows: 100 out of 10000
      Maximum CPU time: 6822 out of 10000 ******* CLOSE TO LIMIT
      Maximum heap size: 5785151 out of 6000000 ******* CLOSE TO LIMIT