Skip to main content

@RestResource

(urlMapping='/GetAttachment/*')

global with sharing class GetAttachment {

@HttpGet

global static String getattachment() {

RestRequest req = RestContext.request;

String parentId = req.params.get('parentid');

ContentVersion cv=[select id,ContentDocumentId,versiondata from Contentversion where id='0682F000000hGnLQAU' ];

Blob csvFileBody =cv.VersionData;

String csvAsString= csvFileBody.toString();

List<String> csvFileLines= csvAsString.split('\n');

system.debug(csvFileLines);

return csvAsString;

}

}

 

 
2 respuestas
  1. 20 abr 2021, 09:13
    Hello,

     

    Try below code,

     

     

    @isTest

    public class GetAttachmentTest {

    @isTest

    static void teste(){

    Account acc= new Account();

    acc.Name ='Test';

    insert acc;

    ContentVersion conVer = new ContentVersion();

    conVer.ContentLocation = 'S'; // S specify this document is in SF, use E for external files

    conVer.PathOnClient = 'pathonclient.jpg'; // The files name, extension is very important here which will help the file in preview.

    conVer.Title = 'title'; // Display name of the files

    conVer.Description = 'description data';

    conVer.VersionData = EncodingUtil.base64Decode('filedata'); // 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;

    if(conDoc != null) {

    //Create ContentDocumentLink

    ContentDocumentLink cDe = new ContentDocumentLink();

    cDe.ContentDocumentId = conDoc;

    cDe.LinkedEntityId = acc.Id; // you can use objectId,GroupId etc

    cDe.ShareType = 'V'; // Inferred permission, checkout description of ContentDocumentLink object for more details

    //cDe.Visibility = 'InternalUsers';

    insert cDe;

    }

    RestRequest req = new RestRequest();

    RestResponse res = new RestResponse();

    req.requestUri ='/services/apexrest/GetAttachment/?parentid='+acc.Id;

    req.httpMethod = 'GET';

    RestContext.request = req;

    RestContext.response = res;

    GetAttachment.getattachment();

    }

    }

    Thanks.

     

     
0/9000