0

I want to know how to use a wrapper class in apex, without using the visual force page.

Actually I have a task; To display the Account whose activstatus is checked(custom fields) and its contacts Using the REST services and output should be in JSON.

Without using any id or name when I execute the Get method, the system should display all the accounts and their contacts as per the condition.

2 Answers 2

2

Assuming the scenario is as simple as you have mentioned in the question you could add the query parameter in the requesturl in your REST call:

url : "SELECT+name,account.name,account.id,Name+from+contact+where+account.activstatus=true"

also set the contentType as "application/json":

 content-Type: "application/json"

This should give you a result in the json format. But you will not get all the records at once, you will get in batches (max 2000). Each response will contain url to which you can make your next request to so as to get the next batch of data..

1

http://cloudyworlds.blogspot.in/2012/09/how-to-generate-wrapped-data-from.html

The above blog link may help you.The sample code is given below you can build on top of the below code

{
"Accounts": [
    {
        "attributes": {
            "type": "Account",
            "url": "/services/data/v25.0/sobjects/Account/001W0000006XDG9IAO"
        },
        "CreatedDate": "2012-08-09T08:29:43.000+0000",
        "LastModifiedDate": "2012-08-09T15:24:42.000+0000",
        "IsDeleted": true,
        "Id": "001W0000006XDG9IAO"
    },
    {
        "attributes": {
            "type": "Account",
            "url": "/services/data/v25.0/sobjects/Account/001W0000006XGEuIAO"
        },
        "CreatedDate": "2012-08-09T15:44:29.000+0000",
        "LastModifiedDate": "2012-08-09T15:50:31.000+0000",
        "IsDeleted": false,
        "Id": "001W0000006XGEuIAO"
    },
    {
        "attributes": {
            "type": "Account",
            "url": "/services/data/v25.0/sobjects/Account/001W0000006QtKyIAK"
        },
        "Phone": "12131",
        "CreatedDate": "2012-08-02T14:46:48.000+0000",
        "LastModifiedDate": "2012-08-09T15:43:20.000+0000",
        "IsDeleted": true,
        "Id": "001W0000006QtKyIAK"
    },
    {
        "attributes": {
            "type": "Account",
            "url": "/services/data/v25.0/sobjects/Account/001W0000006XGI4IAO"
        },
        "CreatedDate": "2012-08-09T15:50:07.000+0000",
        "LastModifiedDate": "2012-08-14T11:33:05.000+0000",
        "FirstName": "Test",
        "IsDeleted": false,
        "Id": "001W0000006XGI4IAO",
        "LastName": "Thomas"
    },
    {
        "attributes": {
            "type": "Account",
            "url": "/services/data/v25.0/sobjects/Account/001W0000006XGIDIA4"
        },
        "CreatedDate": "2012-08-09T15:44:55.000+0000",
        "LastModifiedDate": "2012-08-14T10:15:39.000+0000",
        "FirstName": "shgdshg",
        "IsDeleted": false,
        "Id": "001W0000006XGIDIA4",
        "LastName": "Hello"
    },
    {
        "attributes": {
            "type": "Account",
            "url": "/services/data/v25.0/sobjects/Account/001W0000006XGhwIAG"
        },
        "CreatedDate": "2012-08-09T16:08:14.000+0000",
        "LastModifiedDate": "2012-08-09T16:08:19.000+0000",
        "FirstName": "deleted",
        "IsDeleted": true,
        "Id": "001W0000006XGhwIAG",
        "LastName": "check"
    },

  "Contacts": [
        {
        "attributes": {
            "type": "Contact",
            "url": "/services/data/v25.0/sobjects/Contact/003W0000007mNoqIAE"
        },
        "AccountId": "001W0000006XGI4IAO",
        "HCP__c": "001W0000006XGIDIA4",
        "FirstName": "Test",
        "IsDeleted": false,
        "Id": "003W0000007mNoqIAE",
        "LastName": "Thomas"
    },
    {
        "attributes": {
            "type": "Contact",
            "url": "/services/data/v25.0/sobjects/Contact/003W0000007mNp1IAE"
        },
        "AccountId": "001W0000006XGIDIA4",
        "HCP__c": "001W0000006XGEuIAO",
        "FirstName": "shgdshg",
        "IsDeleted": false,
        "Id": "003W0000007mNp1IAE",
        "LastName": "Hello"
    },
    {
        "attributes": {
            "type": "Contact",
            "url": "/services/data/v25.0/sobjects/Contact/003W0000007mNpGIAU"
        },
        "Phone": "3213",
        "AccountId": "001W0000006XGEuIAO",
        "HCP__c": "001W0000006XGIDIA4",
        "IsDeleted": false,
        "Id": "003W0000007mNpGIAU",
        "LastName": "ueiru"
    },
    {
        "attributes": {
            "type": "Contact",
            "url": "/services/data/v25.0/sobjects/Contact/003W0000007wAkwIAE"
        },
        "Phone": "206-999-1111",
        "AccountId": "001W0000006XJ3fIAG",
        "Email": "[email protected]",
        "HCP__c": "001W0000006XGIDIA4",
        "FirstName": "Mark",
        "IsDeleted": false,
        "Id": "003W0000007wAkwIAE",
        "LastName": "Wells"
    }
  ]}

For this we will have to use customized wrapping ,

Following is the source code that will automatically handle the generation of JSON in the expected format

  global class SFA_AccountContactListedResponse{
 //Wrapper class to warp the various  List<sObject> as Super wrapper
  global class supersobjectWrapper{
  List<Account> Accounts=new List<Account>();//A list to hold the Accounts
  List<Contacts> Contacts=new List<Contact>();//A list to hold the Contacts  
 }
 //This method will be called as the http get request
  public static supersobjectWrapper makeResponseString(){  
 List<Account>  lstacc=new List<Account>();
 List<Contact> lstcontacts=new List<Contact>();
 lstacc=[Select Id,CreatedDate,LastModifiedDate,Isdeleted from Acccount ];
 lstcontacts=[Select id,CreatedDate,LastModifiedDate,Isdeleted from Contact];
 supersobjectWrapper superWrap=new supersobjectWrapper();//Instantiating thesuperclass
 superWrap.Contacts= lstcontacts ;
  superWrap.Accounts= lstacc;
  return superWrap;
 }
 }

//The above class will be called as REST API using GET HTTP call,

  @RestResource(urlMapping='/GetAcc/*')
  global with sharing class SFA_ListedJsonGenerator{ 
  @HttpGet
 global static SFA_AccountGroupSyncManagerRevised.supersobjectWrapper    returnJsonpacket(){ 
   RestRequest req = RestContext.request;
   SFA_AccountGroupSyncManagerRevised.supersobjectWrapper result;  
   result=SFA_AccountContactListedResponse.supersobjectWrapper;
    return result;
   }
 }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .