3

I am using site.com to host a mini site which works fine. I need another delivery method other than manually uploading my .zip file to my site as an asset.

I have found a way to expose an endpoint referencing this fairly old document: https://developer.salesforce.com/blogs/developer-relations/2012/02/quick-tip-public-restful-web-services-on-force-com-sites.html.

I successfully created a small program to retrieve and send back information when hitting the endpoint from the CLI:

    @RestResource(urlMapping='/myservice')
global class MyService {
    @HttpGet
    global static String doGet() {

        // the site name provided
        String name = RestContext.request.params.get('name');

        // get the site information
        Site site = [SELECT Name FROM Site WHERE Name =: name Limit 1];
        String label = [SELECT MasterLabel FROM Site where Name =: site.Name].MasterLabel;
        Id admin = [SELECT AdminId FROM Site where name =: site.Name].AdminId;

        // get the site admin information
        User contact = [SELECT id FROM User where id =: admin Limit 1];
        String contactFirstName = [SELECT FirstName FROM User where id =: contact.Id].FirstName;
        String contactLastName = [SELECT LastName FROM User where id =: contact.Id].LastName;


        return 'The contact for the '+label+' site is '+contactFirstName+' '+contactLastName;
    }
}

Is it possible to extend this type of service to accept uploading a document?

  • I think you are going to want an @HttpPost method.... – John Thompson May 12 '17 at 2:21
2

Here is an example code of how to do it to accept binary data

@HttpPost
global static String insertContentVersion(RestRequest req) {
      ContentVersion cv = new ContentVersion(
                // data fields
                versionData = req.requestBody,
                title = 'Test',
                description = 'Description',
            );
  insert cv;
  return cv.Id
}

The req.requestBody is the base64 ecode of the binary data you want in the request body .

You can change the code to accept document object as well along same lines

Note that this will have a limit of 3MB because of apex limits .

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.