0

How can i upload a file from my client PC via Apex ? I need it for a test class so i cannot make api callout. I try this but without success

String yourFiles = 'Lets assume this is your binary string of the files';

ContentVersion conVer = new ContentVersion();
conVer.ContentLocation = 'E'; // S specify this document is in SF, use E for external files
conVer.PathOnClient = 'C:\myPC\Accounts1472.csv'; // The files name, extension is very important here which will help the file in preview.
conVer.Title = 'Accounts1472 '; // Display name of the files
conVer.VersionData = EncodingUtil.base64Decode(yourFiles); // converting your binary string to Blog
insert conVer;

Any suggestions please ?

1

I solved the issue by doing the following: 1) Get to https://onlinecsvtools.com/convert-csv-to-base64 and convert the content of my CSV 2) Since i did not find solution to insert directly from my client pc i used the Salesforce ContentLocation but i inserted my content.

String yourFiles = 'RUFfQ/Om86XIM6kzpgxMTMJzqHOn86Uzp==';

        ContentVersion conVer = new ContentVersion();
        conVer.ContentLocation = 'S'; // S specify this document is in SF, use E for external files
        conVer.PathOnClient = 'ionicLogo.csv'; // The files name, extension is very important here which will help the file in preview.
        conVer.Title = 'Accounts '; // Display name of the files
        conVer.VersionData = EncodingUtil.base64Decode(yourFiles); // converting your binary string to Blog
        insert conVer;

        // First get the content document Id from ContentVersion
        Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;

        //Create ContentDocumentLink
        ContentDocumentLink cDe = new ContentDocumentLink();
        cDe.ContentDocumentId = conDoc;
        cDe.LinkedEntityId = batch.Id; // you can use objectId,GroupId etc
        cDe.ShareType = 'I'; // Inferred permission, checkout description of ContentDocumentLink object for more details
        cDe.Visibility = 'AllUsers';
        insert cDe;
|improve this answer|||||
0

Apex code cannot read your local file system. You would need some API call into Salesforce to send the contents of a file on your local machine.

|improve this answer|||||
  • or a VF page/LWC – cropredy Oct 16 '19 at 18:56
  • Thanks for your response. I actually need it for test class so i cannot make API Callout. – Giorgos Galaios Oct 17 '19 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.