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
sailersailer 

JSON Parsing in SOAP API for apex class

Hi Everyone,

I created the class and exposed has. WSDL and I am able to get the session id and and the result,

I need to send as JSON response for third party Application.

 

How do parse the JSON and how to send the response?

 

global class AccountServices
{
    webService static String hello(String Name,String Phone,String Website)
    {
    
      Account accounts = [SELECT id, name,phone,website FROM Account LIMIT 1];
      return 'Hello '+accounts.name+' '+accounts.Phone+' '+accounts.Website+' ! :D';
   
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
Pat PattersonPat Patterson

Create an Apex REST Method to do this:

 

@RestResource(urlMapping='/account')
global class MyService {
    @HttpGet
    global static Account doGet() {
// Get a query param String name = RestContext.request.params.get('name');
// Make it into a SOQL pattern String pattern = '%' + name + '%';
// Get a matching account List<Account> accounts = [SELECT id, name, phone, website FROM Account WHERE name LIKE :name LIMIT 1]; if (accounts.size() > 0) { return accounts[0]; } else { return null; } } }

 Test:

 

curl -H 'X-PrettyPrint: 1' -H 'Authorization: Bearer [SESSION_ID]' https://na9.salesforce.com/services/apexrest/account?name=sfo
{
  "attributes" : {
    "type" : "Account",
    "url" : "/services/data/v28.0/sobjects/Account/001E0000002Jv2mIAC"
  },
  "Name" : "sForce",
  "Phone" : "(415) 901-7000",
  "Website" : "www.sforce.com",
  "Id" : "001E0000002Jv2mIAC"
}

 

 

 

 

All Answers

BinayakBinayak

Guess with the help of SOAP API we cannt do a JSON response directly in SFDC:

 

FROM:http://login.salesforce.com/help/doc/en/integrate_what_is_api.htm

 

 

But we can have a work around (dont know if its optimal soln): try to form the JSON (after getting the result from the query,

and do the JSON.serealize LIKE:

 

List<Account> accounts = [SELECT id, name FROM Account LIMIT 2]; 
String accountsJSON = JSON.serializePretty(accounts);

send this JSON as the return type of the method as the string.

 

           

Pat PattersonPat Patterson

Create an Apex REST Method to do this:

 

@RestResource(urlMapping='/account')
global class MyService {
    @HttpGet
    global static Account doGet() {
// Get a query param String name = RestContext.request.params.get('name');
// Make it into a SOQL pattern String pattern = '%' + name + '%';
// Get a matching account List<Account> accounts = [SELECT id, name, phone, website FROM Account WHERE name LIKE :name LIMIT 1]; if (accounts.size() > 0) { return accounts[0]; } else { return null; } } }

 Test:

 

curl -H 'X-PrettyPrint: 1' -H 'Authorization: Bearer [SESSION_ID]' https://na9.salesforce.com/services/apexrest/account?name=sfo
{
  "attributes" : {
    "type" : "Account",
    "url" : "/services/data/v28.0/sobjects/Account/001E0000002Jv2mIAC"
  },
  "Name" : "sForce",
  "Phone" : "(415) 901-7000",
  "Website" : "www.sforce.com",
  "Id" : "001E0000002Jv2mIAC"
}

 

 

 

 

This was selected as the best answer
sailersailer

HI ,

 

Has the CURL is not enabled in the system i am using google chrome add on test it.

I am getting the Authorization: OAuth 00D90000000mO4Z!AQYAQMyF4TqLDBLlgmDCLGsJfsxl5mcEFNjuMUInJ4Oa8c.5xb5DOIrzPMUkje81t6_MvznkcktP6ZRAZdjYqexN9Y0Y6A0Z


whenver i add the  url to get the response

https://ap1.salesforce.com/services/apexrest/account

 

0:  

{
message: "Could not find a match for URL /account"
errorCode: "NOT_FOUND"
}
 
and suppose if want to give this endpoint to the third party how can i do this??
 
 
 
 
  Thanks for the help in advance

 

 

sailersailer

Hi ,,

 The  above issues was with the package .it got solved i am able to get the response from salesforce.com

and below is the endpoint

https://ap1.salesforce.com/services/apexrest/testvantage/account.

 

I need to automate the response by using java code with the login and Ouath credentials.

how do i need to set and gives this end point to the third party appliaction for pulling the data.

 

@RestResource(urlMapping='/account')
global class TestRestServices {
    @HttpGet
    global static List<Account> getAccounts() {
       List<Account> lst;
       try
       {
        RestRequest req=RestContext.request;
        String namevalue=req.params.get('name');
        lst=[SELECT name,id ,website,phone,type from Account where type='other'];
        return lst;
       }catch(Exception e)
       {
       system.debug('Error'+e.getMessage());
       }
       return lst;
    }
}