function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Prasad RaghavendraPrasad Raghavendra 

Calling apex class from external website via POST method

I am trying to figure out a way to expose the functionalities of SalesForce via API so that I can use my business logic with my custom Android app/web-page which calls API via POST.

However, I have been unable to find a comprehensive solution. Any help will be greatly appreciated as I am aware of functionalities of SalesForce. But, I am not aware of exposing the Apex class as an endpoint.

This is what I would like to do as a basic starter:

Write an Apex class which takes two numbers as inputs and returns the sum of two numbers. I do know to do this via Lightning Component and I have read about VisualForce page. However, I have a requirement where I have to integrate SalesForce with vanilla HTML, Javascript and CSS if possible. So, POST would be convenient.

My attempts before asking this question:

I read lots of official documentation. I also got to know about Session ID and how that is being protected in developer console and how I can get it via workarounds. All nice. But, these official documentations did not help me design one Apex class which can do the use case mentioned above. I read lot of community posts which were more helpful. But, I have been unable to cobble them up together and test it via postman. Thanks for the time and consideration.

Raj VakatiRaj Vakati

All you need do is expose your apex method using simple rest api and call this Rest API from the Android App by passing OAUTH Details 
@ RestResource(urlMapping = '/SumData')
 global class SumRESTAPI{
    @HttpPost
    global static Integer returnSUm(Integer i1 , Integer i2) {

       return i1+i2 ; 

    }

 }
Here there is a webservice running at url

https://yoursfinstance/services/apexrest/SumData

when you hit this endpoint after authorization, you can able to return the response to caller 


http://amitsalesforce.blogspot.com/2017/06/test-salesforce-api-by-postman-rest.html
http://amitsalesforce.blogspot.com/2017/06/test-salesforce-api-by-postman-rest.html
https://docparser.com/blog/post-file-salesforce-apex-external-http-webservices/
https://apexcoder.com/2016/12/13/how-to-created-and-use-apex-rest-services-in-salesforce/
http://kalyanlanka.blogspot.com/2014/08/calling-apex-rest-service-using-postman.html
Prasad RaghavendraPrasad Raghavendra
@Raj Vakati, thank you for the answer. I read through http://amitsalesforce.blogspot.com/2017/06/test-salesforce-api-by-postman-rest.html and followed the steps. I got the following response (data removed)

{
    "access_token": "",
    "instance_url": "",
    "id": "",
    "token_type": "",
    "issued_at": "",
    "signature": ""
}

From here, I tried to hit this url via POST https://yoursfinstance/services/apexrest/SumData

However, I am getting the following response.
[
    {
        "message": "Session expired or invalid",
        "errorCode": "INVALID_SESSION_ID"
    }
]

P.S.: I also tried https://jsforce.github.io/ But, I am not able to get this simple addition web-page up and running.
Prasad RaghavendraPrasad Raghavendra
Can I request somebody to let me know where I am going wrong or point me to the right direction? (In case full answer cannot be provided) Thanks for the time and consideration.
Prasad RaghavendraPrasad Raghavendra
By the way, am I supposed to enable REST API in my free developer sandbox? https://developer.salesforce.com/forums?id=906F000000099wxIAA I am able to query in the command-line using jsForce. But, for some reason, I am not able to figure out calling Apex method. (Especially 1. How to call the Apex method via POST 2. Passing two integers in the request (if I have to pass strings and then do some typecasting etc.))
Prasad RaghavendraPrasad Raghavendra
I am not able to get any solution in Java or Javascript. I have asked in StackExchange as well. https://salesforce.stackexchange.com/questions/258643/how-to-do-post-after-getting-authorisation-token-and-instance-url-in-java-or-ja