0

I want prepare JSON body dynamically for all the contact fields with mask values, i have queried all the contact fields dynamically assigned to list now i have to prepare JSON body for the fields with mask values.

For example contact name is Anil it should return:

{"Name":"True"}

And if contact Phone is null it should return:

{"Phone":"False"}

How can I achieve this in apex?

0

Try this. You need to call the createJSON() method You need a wrapper class to cater the requirement like below. Or you may create formula field on the contact for each field and select those fields using SOQL and convert that list to a JSON.

public Class createContactJSON{
    public createContactJSON(){
        createJSON();
    }
    public String createJSON(){
        String JSONstr = '';
        List<Contact> conList = [SELECT Name,Phone FROM Contact];
        List<contactWrapper> conWrapList = new List<contactWrapper>();
        for(Contact tempContact : conList){
            contactWrapper tempcontactWrapper = new contactWrapper();
            tempcontactWrapper.Name = tempContact.Name <> NULL ? true:false;
            tempcontactWrapper.Phone = tempContact.Phone <> NULL ? true:false;
            conWrapList.add(tempcontactWrapper);
        }
        JSONstr = JSON.serialize(conWrapList);
        System.debug('\n******'+JSONstr +'\n******');
        return JSONstr;
    }   
    public class contactWrapper{
        public Boolean Name {get;set;}
        public Boolean Phone {get;set;}
    }
}
3
  • We can do the same using List<Map<String, Boolean>> no need of wrapper here. Jun 22 '17 at 13:46
  • Hi Avijit, Thanks lot for your response. This will help for me but contact fields i am querying dynamically through Schema.getGlobalDescribe(). Below is the code i am using for dynamic query. Suppose if user creates some new fields in the contact how send those fields in the JSON body without modifying the code.
    – anil kumar
    Jun 22 '17 at 14:32
  • SObjectType Type = Schema.getGlobalDescribe().get('Contact'); Map<String,Schema.SObjectField> mfields = Type.getDescribe().fields.getMap(); system.debug('@@@'+mfields.values()); String sQuery = 'SELECT '; for(Schema.SObjectField s:mfields.values())sQuery +=s+','; sQuery=sQuery.removeend(','); sQuery+=' from contact'; system.debug('sQuery '+sQuery);
    – anil kumar
    Jun 22 '17 at 14:34
2

This will accomplish that for all fields:

Contact c = new Contact(LastName = 'Anil');

Map<String, Boolean> m = new Map<String, Boolean>();
for (String f: Contact.SObjectType.getDescribe().fields.getMap().keySet()) {
    // Could be changed to output the string "True" or "False" if necessary
    m.put(f, c.get(f) != null);
}
String jsonString = JSON.serializePretty(m);

System.debug(jsonString);

The output looks like this:

{
  ...
  "firstname" : false,
  "lastname" : true,
  "accountid" : false,
  "masterrecordid" : false,
  "isdeleted" : false,
  "id" : false
}
-1
    SObjectType Type = Schema.getGlobalDescribe().get('Contact');
    Map<String,Schema.SObjectField> mfields = 
    Type.getDescribe().fields.getMap();
    system.debug('@@@'+mfields.values());
    String sQuery = 'SELECT ';
    for(Schema.SObjectField s:mfields.values())sQuery +=s+',';

    sQuery=sQuery.removeend(',');
    sQuery+=' from contact where anil5454__Send_To = true';
    system.debug('sQuery '+sQuery);

    list<contact> conlist=database.query(sQuery);
    i want prepare dynamic JSON body for the list of contacts(conlist), if 
    the field value is exist i need to send it as True, if it is not exist i 
    need to send it as false.  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.