[SalesForce] salesforce web service call another web service

I have 2 questions

  1. Can salesforce webservice call another web service? If yes does the webserice needs to be marked as future method?

  2. Can I Call static salesforce webservice from trigger how does itbehave? What happens if call future method from it.

Best Answer

  1. A web service inside salesforce can:

    • Execute the methods in the callout directly if within the same salesforce org. If the method does not use any of the request headers you can call the method directly
    • Make a callout to another webserive - does not need to be marked as @future
  2. You can call a static method in a restResource annotated method just like any other class. The only caveat is that the method cannot use or reference anything in the RestContext or RestReponse classes as they would be unavailable from a trigger...The annotation just allows the method to be mapped as a rest resource available to external systems

Basically treat it like any other static method, unless it utilizes the Rest classes in the method then you would treat it like any other callout

Consider this example:

@RestResource(URLMapping='/test/*')
global class myTestRest{

@HTTPPost
global static string myMethod(ID i){
     Account a = [Select Name From Account Where ID = :i];
     return a.name;
}

}

you would simply use mytestRest.myMethod(<AN ACCOUNT ID>); in your trigger directly

However if this line exists in your method:

RestRequest req = RestContext.request;

Then you will not be able to call the method directly unless you account for the possibility that RestContext or req could be NULL everywhere it is used in the method. (I am not 100% sure if RestContext would dereference null error if called directly or if the RestContext.request would just return null)

Related Topic