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

how to return JSON for Account ad respective contacts

Hi Friends,
How to get all accounts and recpective contacts in JSON format? Also I need to return Product name from a custom object and this product name field is a text field and its containing products name. But problem I m facing is its returning all duplicate products also but I need unique one. Suppose I have hundred records so product name can be multiple time. Please check the code and let me know the changes i need in code
 public static string AccountContactProductList(){
        SalesAIRecommendations.Response item = new SalesAIRecommendations.Response();
                    item.status = 'SUCCESS';
                    item.code = 1000;
                    item.message = 'SUCCESS';
                    item.mappingId = 0;
                    item.sessionToken = null;
                    item.userType = null;
           = null;
                    item.emailId = null;
                    item.data1 = new List<SalesAIRecommendations.Data1>();
        SalesAIRecommendations.Data1 data1 = new SalesAIRecommendations.Data1();
        data1.Accounts = new List<SalesAIRecommendations.Accounts>();
        set<Id> accountids= new set<Id>();
        List<Account> accountlist= [select Id,Name FROM Account];
                       for(Account a : accountlist){
       List<Contact> contactlist= [select Id,,, Name From contact WHERE AccountId In:accountids];
        for(contact con:contactlist ){
        SalesAIRecommendations.Accounts Accounts = new SalesAIRecommendations.Accounts();
                                Accounts.accountId =;
                                Accounts.accountName =;
        data1.Contacts = new List<SalesAIRecommendations.Contacts>();
        for(contact con:contactlist ){
        SalesAIRecommendations.Contacts Contacts = new SalesAIRecommendations.Contacts();
                                Contacts.contactId = con.Id;
                                Contacts.contactName = con.Name;
        data1.Recommendation = new Set<SalesAIRecommendations.Recommendation>();
        Set<Recommendation__c> Product_RecommendationList = new Set<Recommendation__c>([SELECT Product_Recommendation__c FROM Recommendation__c ORDER BY Product_Recommendation__c ASC]);
        system.debug('ProductsList#######:' +Product_RecommendationList);
        for (Recommendation__c PR :Product_RecommendationList){
        SalesAIRecommendations.Recommendation Product_Recom = new SalesAIRecommendations.Recommendation();
                            Product_Recom.Product_Recommendation = PR.Product_Recommendation__c;
        system.debug('{"Account_Contact_List":' + JSON.serialize(item) + '}');
        return '{"Account_Contact_List":' + JSON.serialize(item) + '}';
Chellappa NagarajanChellappa Nagarajan

Hi Rajan,
Looks like there are two questions in the same post.
I will try to answer the question related to Product.
You can use Aggregates for querying unique product names from your custom object.
List<AggregateResult> prodList = [select Product_Recommendation__c from Recommendation__c where Product_Recommendation__c <> null group by Product_Recommendation__c order by Product_Recommendation__c ASC ];
Set<String> Product_RecommendationList = new Set<String>();
For(AggregateResult aggR : prodList)
Use the Set in the JSON construct for Products. Hope this helps.
See the below idea link, the DISTINCT or UNIQUE keyword is not yet available in Salesforce.