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
Avi646Avi646 

How to create a httpPut method in apex rest

i want to create a method (via PUT) of the Apex REST web service  to receive a list of string and also the code to invoke it from java.

Avi646Avi646

actually am looking for sample code!

the documentation doesnt have such examples.

 

I want a method like say

public static void doPut(list<String> listObj){}

 i can declare it in apex but i want to know how to invoke this from java?




 

Sankalp JhingranSankalp Jhingran

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

Avi646Avi646

Thanks but I am looking to invoke external services from within salesforce.

Sankalp JhingranSankalp Jhingran

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

 

Mayank_JoshiMayank_Joshi

 

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

 

nil.m.7nil.m.7

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?

farukh sk hdfarukh sk hd
Hope this will help you.

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