Skip to main content

How to generate a Listed JSON from multiple Salesforce Objects(say account and contact) and send data in JSON format to mobile client without nesting Using REST API

Sometimes there is a request for listed JSON .Say we have two objects Accounts and Contacts and we have requirement not to nest the response instead send as a collection with key for list of Accounts as Account and key for Contact as Contact.

Below is the JSON format thats expected output,

{
    "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": "mwells@ubermind.com",
            "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 as Super wrapper
 global class supersobjectWrapper{
List Accounts=new List();//A list to hold the Accounts
List Contacts=new List();//A list to hold the Contacts  
}
//This method will be called as the http get request
public static supersobjectWrapper makeResponseString(){  
List  lstacc=new List();
List lstcontacts=new List();
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;
    }
}

Popular posts from this blog

TLS 1.0 has been disabled For Sandbox - Salesforce

Salesforce has finally disabled TLS 1.0 in sandboxes .This is in preparation for disablement later in 2017 for PRODUCTION instances .

Now this would have not impacted your integrations in PRODUCTION org but if your integration is broken in your sandbox ,this means you have only few months to sort this and fix before this affects your integration for PROD live users .
Before we deep drive on how to possibly fix this and work with your external systems to figure solution ,lets first understand what is TLS and why did SFDC moved to 1.x and had to disable TLS 1.0
TLS 1.0 Explained























TLS 1.1 Improvements 
Added protection against cipher-block chaining (CBC) attacks. Support for IANA registration of parameters.

Clearly TLS 1.1 is more secure(Compared to 1.0) and protects salesforce resources against CBC attacks .
Identify if this change broke anything .The things that can be affected in your instances are as below Web requests to Salesforce URLs that require authenticationWeb requests to the login pag…

Opening Modal Using Lightning Component Framework of SFDC

One of my friend from India threw a challenge .The challenge was to open a modal by using latest and greatest lightning components framework and modals design from SLDS .For the love of community I thought of sharing the entire code base that I did .

So here we start ..

Business Use Case - Need a handy SalesLeader board component that can be used to display the Sales revenue generated by each sales rep for current year in the order of decreasing total revenue .On click of the tile ,we will show detail opportunity list aggregating the revenue .

The component can be dragged in lightning design experience or in App builder lightning Page .

Video Demonstration-



SalesLeaderBoard from Mohith Kumar Shrivastava on Vimeo.








Frameworks Used -
Lightning Design Systems (SLDS) for CSSLightning Component Framework for client side logicApex aura enabled class for backend logicApproach
The component hierarchy is very important to imagine or mindmap before we dig deeper- SalesLeaderMain
    -SalesLeaderBoard
    …

Customizing Napili Template Of SFDC For Communities - Community Cloud

Recently I have been busy working with salesforce community cloud platform and I am impressed by the new added features of Spring 16 .

You can watch the Release Readiness webinar for more details on what are added features for community cloud 

Lets talk about something which most of client (Either SI or ISV ) would love to have with the Napili template .The first question that clients ask with Napili is its flexibility to customize and tailor to their specific needs .

The very straight forward questions that people familiar with Napili template ask are as follows 

Can I change the CSS and look and feel ?  

Well yes and No ....The new branding editor you can sparingly use CSS and change look and   feel   and community editor can also help you to some extent to change labels and the color and you can really get closer to what you want .Just note that efforts will be high if we want to change the entire template design.

Will i be able to populate data from custom objects and standard objects a…