2

I am writing a web service that would expose Salesforce fields and this one would be called by an external system.

The scenario is that when a user there would views a record detail page of the external system, then a "field present in there needs to get updated with Salesforce Account OwenerID" for that particular record.

So, when the user clicks the record, the web service needs to get that recordId of the external system (memberId) and query back in Salesforce with the memberID and send the response back in JSON format.

The object that I am working is Account, where in I need to send the Account Name, OwnerID and a Custom Field. And the parameter used in the WHERE condition is the "custom filed".

@RestResource(urlMapping='/GetAccountDetails/*')
global with sharing class GetAccountDetails
{
   @HttpGet
   global static String getManagers(RestRequest req, RestResponse res) {
    String memberID = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
    //Account result = [SELECT Id, Name, Member_c, OwnerID FROM Account WHERE Member__c = :memberID];
    String accountFields = 'SELECT Id, Name, Member_Number__c, OwnerID FROM Account WHERE Member__c';
    String detailsJSON = JSON.serializePretty(Database.query(accountFields));
    system.debug('******'+detailsJSON);
    return detailsJSON;
}
}

And would also like to know how do I invoke this service at the external system, like, by any URL call or any other way.

  • The external system can just make a GET request, but it will have had to authenticate first and supply an authentication token (unless its an open access Force.com site). So you will need to research that - one way is illustrated here How do I setup access for a REST resource apex class using session ID. – Keith C Aug 13 '14 at 14:22
  • @KeithC I am aware of the authentication process involved but my only concern is that how do I make a Salesforce query based upon the recordId(memberId). And how do I let know my external user to pass the parameter for the Salesforce URL: https://{Server Instance}.salesforce.com/services/apexrest/GetAccountDetails – Sapthagiri Aug 13 '14 at 15:47
  • @KeithC And is there a way I could pass a parameter in the web service and use that parameter in the query in the method. – Sapthagiri Aug 13 '14 at 15:49
  • 1
    You have to tell your external user what the URL format is i.e. tell then to do a GET on e.g. https://{Server Instance}.salesforce.com/services/apexrest/GetAccountDetails/12345678. And your code is correct on how to get that value and then use it in a SQL query i.e. you have to parse the URI yourself. – Keith C Aug 13 '14 at 16:31
2

You don't need to pass the Request/Response into the method. I would have written this like:

@RestResource(urlMapping='/GetAccountDetails/*')
global with sharing class GetAccountDetails
{
  @HttpGet
  global static List<Account> getManagers()
  {
    RestRequest req = RestContext.request;
    String memberID = req.requestURI.substring(req.requestURI.lastIndexOf('/') + 1);
    return [SELECT Id, Name, Member_c, OwnerID FROM Account WHERE Member__c = :memberID];
  }
}

This assumes you are passing an additional String into the parameters.

And would also like to know how do I invoke this service at the external system, like, by any URL call or any other way.

You could use a SOAP service if you want, but IMO REST is usually the better way to go.

EDIT: @sfdcfox also pointed out that returning the native object allows the caller to specify a JSON or XML response.

EDIT 2: String version of the query.

String query = 'SELECT Id, Name, Member_c, OwnerID FROM Account WHERE Member__c = :memberID';
return Database.query(query);
/* you could also cast it to Account before returning
List<Account> result = Database.query(query);
return result;
*/
  • Yes, I would also wanted to go with the REST call. My question is how do I let know the external system call this web service of mine. I know that we should be going with a connected app, client id, client secret,etc but how would the URL look like: https://{Server Instance}.salesforce.com/services/apexrest/GetAccountDetails/2000 (where 2000 is the memberID). Am I correct in passing the "string parameter" in the URL. – Sapthagiri Aug 13 '14 at 14:05
  • This is a GET so there can be no JSON involved in the request, just the RestContext.request.requestURI. – Keith C Aug 13 '14 at 14:14
  • 2
    Also, returning a native object as a response lets the caller choose if they want an XML or JSON response without worrying about semantics. – sfdcfox Aug 13 '14 at 14:15
  • @KeithC O, you must be right. I just noticed my other requests did the get differently. Adjusting. – dphil Aug 13 '14 at 14:38
  • 1
    @Sapthagiri I installed a plugin on chrome called Postman - REST Client. It made it really easy to build the calls. Keep in mind that the authentication parameters DO NOT go in the header for your first call. – dphil Aug 13 '14 at 18:18

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.