0

Hi i am new to the requirement as i need to create the JSON from the APEX in the Below format: JSON Format:

{
    "securiandocnumber": [
        "3970118"
        "3987556"
    ],
    "caseId": "123"
}

Below is the class i have created requesting you to help me out to create the JSON:

 public class SFG_CasePostWebService {

    public SFG_CasePostWebService(){
    }
    @future(callout=true)
    public static void frameCaseWebService(){

        //Query out the data from Salesforce database.
        List<Case> caseList = new List<Case>();
        caseList=[Select id From Case];
        Case_Associated_Document__c[] caseAssoDoc = [SELECT Id, Document_Id__c FROM Case_Associated_Document__c WHERE Service_Request__r.Id = :caseList];

        //Formation of the JSON 
    } 
    public class CaseDetails {
        public String[] securianDocNumbers;
        public String caseId;

    }
  }  

Forget to mention : Document_Id__c == securiandocnumber caseId = Id(From Case)

2 Answers2

1

Few Things I add-in your code.

  • You created a CaseDetails apex class which is correct, I add a constructor which is missing
  • Create a Map<String,List<String> for to store caseId related to document.
  • Loop through the Map and Add data into List of CaseDetails List<CaseDetails>
  • After that, for serializing the list, I used JSON.serialize.

So here are my final changes

    public class SFG_CasePostWebService {

    public SFG_CasePostWebService(){
    }
    @future(callout=true)
    public static void frameCaseWebService(){

        //Query out the data from Salesforce database.
        List<Case> caseList = new List<Case>();
        caseList=[Select id From Case];
        Map<String,List<String>> m = new Map<String,List<String>>();
            for(Case_Associated_Document__c caseAssoDoc = [SELECT Id,Service_Request__r.Id, Document_Id__c FROM Case_Associated_Document__c WHERE Service_Request__r.Id = :caseList]){
                if(m.containsKey(caseAssoDoc.Service_Request__r.Id))
                    m.get(caseAssoDoc.Service_Request__r.Id).add(caseAssoDoc.Document_Id__c);
                else
                    m.put(caseAssoDoc.Service_Request__r.Id,new List<String>{caseAssoDoc.Document_Id__c});
            }
        List<CaseDetails> lst = new List<CaseDetails>();
        for( String obj : m.keySet() ){
            lst.add(new CaseDetails(obj,m.get(obj)));
        }
        //you will get the serialize JSON Data.
        System.debug('lst::'+JSON.serialize(lst));
    } 
    public class CaseDetails {
        public String caseId;
        public List<String> securianDocNumbers;

        CaseDetails(String caseId, List<String> securianDocNumbers){
            this.caseId = caseId;
            this.securianDocNumbers = securianDocNumbers;
        }
    }
  }  
Sarvesh
  • 998
  • 13
  • 29
1

By looking at your CaseDetails Class and the JSON String, It seems there is a comma missing in the JSON. It should be as below.

{
    "securiandocnumber": [
        "3970118",
        "3987556"
    ],
    "caseId": "123"
}

Now to create a JSON String you can use serialize(objectToSerialize) method from JSON Class. You have to instantiate the CaseDetails class fill it with all details then use JSON.serialize to get the JSON String.

CaseDetails cd = new CaseDetails();
//TBD - Fill all the fields in the CaseDetails Object
String jsonString = JSON.serialize(cd);
Saroj Bera
  • 14,699
  • 2
  • 17
  • 35