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
Shubham Gupta 339Shubham Gupta 339 

Create JSON for different EndPont Api.

I have created a metadata type with this field "DMS End Point"(DMS_End_Pint__c) and created 3 different records.

For 2 records 'DMS End Point' is same and for 1 record 'DMS End Point' is different.

Right now the code is written for create json for all records. But my requirement is that, i want to create a seprate JSON for seprate "DMS End point". For now only 2 json should be created, 1 for same 2 records and another for 1 record. But there can be N number of same or different end point records so it should be dynamic.

Here is my code
public class DMSIntegration {

List<DMS_Integration__mdt> lstMataData= new list<DMS_Integration__mdt>();
list<DMS_Integration__mdt> ListEndPoint= new list<DMS_Integration__mdt>();
Map<String, String> fieldMap = new Map<String, String>();
String sObjectFields = '';
String jsonData = '';

   // The no argument constructor 
  public void DMSIntegration() {
      // more code here
  }
  
  //Read the DMS table and need to write the dynamic query
  public void readDMSTable() {
  
//Access Metadata
//lstMataData= [Select Child_Name__c, DMS_End_Pint__c, DMS_Field_Name__c, DMS_Table_Name__c, Object_Name__c, Salesforce_Field_API__c, Salesforce_Field_name__c from DMS_Integration__mdt];

//Getting the fields information
 Map<String, Schema.sObjectField> sObjectFieldMap = DMS_Integration__mdt.sObjectType.getDescribe().fields.getMap(); 
 
 //Map key is the field API name and value is the field data type.
                
                for(Schema.SObjectField sfield: sObjectFieldMap.Values()){
                    Schema.DescribeFieldResult fieldDesc = sfield.getDescribe();
                    fieldMap.put(fieldDesc.getName(), fieldDesc.getType().name());
                }
 
 //Create query with all fields
                for(String field: fieldMap.keySet()){
                    sObjectFields += field+',';
                }
                sObjectFields = sObjectFields.removeEnd(',');
                 
                //Execute the SOQL query
                lstMataData = Database.Query('SELECT '+ sObjectFields +' from DMS_Integration__mdt');
                

system.debug('List of MetaData' +lstMataData);

  }
  
  //Need to write the jason in this
  public void createJSON() {
  
 if(lstMataData!=null || !lstMataData.isEmpty()) {
 
  //Create JSON
                JSONGenerator gen = JSON.createGenerator(true);
                gen.writeStartArray();
                gen.writeStartObject();
                 
                for(DMS_Integration__mdt OrderList :lstMataData ) {
                
                gen.writeFieldName('fields');
                gen.writeStartObject();
                
                for(String field: fieldMap.keySet()){
                    if(OrderList.get(field) != null){
                        gen.writeStringField(field, String.ValueOf(OrderList.get(field)));
                    }
                    else{
                        gen.writeStringField(field, '');
                    }
                }
                gen.writeEndObject();
                }
                gen.writeEndObject();
                gen.writeEndArray();
                //Getting the JSON String Data
                jsonData = gen.getAsString(); 
                system.debug(jsonData);
                
     //UseCase 2
    JSONGenerator genLineItem = JSON.createGenerator(true);
    genLineItem.writeStartObject();     
    genLineItem.writeFieldName('Order');
    genLineItem.writeStartArray();
    for(DMS_Integration__mdt OrderList :lstMataData){
        genLineItem.writeStartObject();
        genLineItem.writeStringField('Object Name', OrderList.Object_Name__c);
        genLineItem.writeStringField('Salesforce Field API', OrderList.Salesforce_Field_API__c);
        genLineItem.writeStringField('Salesforce Field name', OrderList.Salesforce_Field_name__c);
         genLineItem.writeFieldName('OrderLineItem');
         genLineItem.writeStartObject(); 
        genLineItem.writeStringField('Child Name', OrderList.Child_Name__c);
        genLineItem.writeStringField('DMS End Point', OrderList.DMS_End_Pint__c);
        genLineItem.writeStringField('DMS Field Name', OrderList.DMS_Field_Name__c);
        genLineItem.writeStringField('DMS Table Name', OrderList.DMS_Table_Name__c);
        genLineItem.writeEndObject();
        genLineItem.writeEndObject();
    }
    genLineItem.writeEndArray();
    genLineItem.writeEndObject();
    String jsonDataLineItem = genLineItem.getAsString();
    System.debug('jsonData-' + jsonDataLineItem);
   
}
}
}


Thanks,
Shubham
SSP AdminSSP Admin
Hello Shubham Gupta

It looks like our team of experts can help you resolve this ticket. We have Salesforce global help-desk support and you can log a case and our Customer Success Agents will help you solve this issue. You can also speak to them on live chat. Click on the below link to contact our help-desk. Trust me it is a support service that we are offering for free!

https://jbshelpdesk.secure.force.com

Thanks,
Jarvis SFDC team