You can invoke the REST service that you have created in Apex by creating a new HTTPPut object in java and using the your REST service URL that you have defined in your REST service.
Here is an example:
public void RestMethod1(){ HttpClient httpclient = new DefaultHttpClient(); System.out.println("Creating HTTP Client");
HttpPut put = new HttpPut("https://instance.salesforce.com/services/apexrest/Account/<sample account id>"); HttpResponse response = null;
// set the token in the header System.out.println("Setting authorization header"); get.setHeader("Authorization", "OAuth 00D50000000IoZ6!AQYAQJ4Q.w9jmFMZJNRlw54y4RuUxfQYGGJzebS3LFL2xEgmvUaWs7xhS6OJrkcO5jw2zPR1I3xhuiNVVXWaiKU_7iibsZ8J");
The URL specified when creating the HTTPPut is what you have defined in the:
@RestResource(urlMapping='/Account/*') at the top of your REST class in APEX along with the standard REST URL for salesforce like https://instance.salesforce.com/services/apexrest
Your original issue said, you wanted to create a REST service in apex and then invoke it from Java, now you are saying you want to invoke external services from within salesforce. Can you please clarify:
Nevertheless, you can invoke any external webservice or even an web service created in apex rest like this:
Http h = new Http(); HttpRequest req1 = new HttpRequest(); req1.setEndpoint('<end point of your REST service>'); req1.setMethod('PUT'); HttpResponse res1 = null; res1 = h.send(req1); system.debug('%%%%%%%%%%%%%%%%%%%%%% ' + res1.getBody());
http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_rest.htm
actually am looking for sample code!
the documentation doesnt have such examples.
I want a method like say
i can declare it in apex but i want to know how to invoke this from java?
Hi,
You can invoke the REST service that you have created in Apex by creating a new HTTPPut object in java and using the your REST service URL that you have defined in your REST service.
Here is an example:
public void RestMethod1(){
HttpClient httpclient = new DefaultHttpClient();
System.out.println("Creating HTTP Client");
HttpPut put = new HttpPut("https://instance.salesforce.com/services/apexrest/Account/<sample account id>");
HttpResponse response = null;
// set the token in the header
System.out.println("Setting authorization header");
get.setHeader("Authorization", "OAuth 00D50000000IoZ6!AQYAQJ4Q.w9jmFMZJNRlw54y4RuUxfQYGGJzebS3LFL2xEgmvUaWs7xhS6OJrkcO5jw2zPR1I3xhuiNVVXWaiKU_7iibsZ8J");
try{
System.out.println("Executing Put request");
response = httpclient.execute(put);
HttpEntity entity = response.getEntity();
}catch(Exception e){
e.printStackTrace();
}
}
The URL specified when creating the HTTPPut is what you have defined in the:
@RestResource(urlMapping='/Account/*') at the top of your REST class in APEX along with the standard REST URL for salesforce like https://instance.salesforce.com/services/apexrest
Hope it is helpful.
Regards,
Sankalp
Blog: forcesecrets.blogspot.com
Thanks but I am looking to invoke external services from within salesforce.
Hi,
Your original issue said, you wanted to create a REST service in apex and then invoke it from Java, now you are saying you want to invoke external services from within salesforce. Can you please clarify:
Nevertheless, you can invoke any external webservice or even an web service created in apex rest like this:
Http h = new Http();
HttpRequest req1 = new HttpRequest();
req1.setEndpoint('<end point of your REST service>');
req1.setMethod('PUT');
HttpResponse res1 = null;
res1 = h.send(req1);
system.debug('%%%%%%%%%%%%%%%%%%%%%% ' + res1.getBody());
Regards,
Sankalp
Let me know ,if that helps you .
HttpPut: Defines the function to be used with the endpoint with HTTP PUT POST
@HttpPut
global static String updateCase(RestRequest req) {
String companyName = req.params.get('companyName');
Account company = [ Select ID, Name, Email__c, BillingState from Account where Name = :companyName];
Attachment a = new Attachment();
a.ParentId = company.Id;
a.Name = 'test.png';
a.Body = req.requestBody;
insert a;
return 'Attachment added';
}
Kind Regards,
Mayank Joshi
can i use @HttpPut with two global static methods in same rest class?
I am getting error like "@HttpPut already used elsewhere in class".
How to resolve this?
Here rest api is used to update record in second salesforce system from first.
https://www.sfdc-lightning.com/2019/01/salesforce-rest-api-integration-to-update-a-record.html