5

I want to build a custom sync framework and combine serval object's data to a Json string. For example

List<Account> accounts = [select Id, Name, ... from Account where ...]; 

The sOBject is dynamic, it can be Account, Contact or CustomObject__c and the fields is also dynamic. the field between select and from clause is flexible. That means I don't know there will be how many fields. I want to convert the select result to this format:

   {
   "records": [
    {
        "values": [
            "00190000014IoCoAAK | 00081 | Super Mart | ...",
            "00190000014EoCAABK | 00082 | Test Mart | ...",
        ],
        "objName": "Account",
        "fields": "Id,AccountNumber,Name, ..."
    },
    {
        "values": [
            "0039000001JDBt7AAH | 00190000014IoCoAAK | Ues1 | ..."
        ],
        "objName": "Contact",
        "fields": "Id,AccountId,Name, ..."
    }
 ] 
}

How can I build a Apex calss to handle this Json?

5

Here is the sort of code I would write for this:

public class Records {

    public Record[] records;

    public Records() {
        records = new Record[] {};
        records.add(new Record(Account.SObjectType, new SObjectField[] {
                Account.Id,
                Account.Name,
                ...
                }));
        records.add(new Record(Contact.SObjectType, new SObjectField[] {
                Contact.Id,
                Contact.AccountId,
                Contact.Name,
                ...
                }));
    }

    public String toJsonString() {
        return JSON.serializePretty(this);
    }
}

public class Record {

    public String objName;
    public String fields;
    public String[] values;

    public Record(SObject sob, SObjectType type, SObjectField[] fields) {

        objName = String.valueOf(type);

        String[] names = new String[] {};
        for (SObjectField field : fields) {
            names.add(String.valueOf(field));
        }
        fields = String.join(names, ',');

        for (SObject sob : Database.query(''
                + ' select ' + fields
                + ' from ' + objName
                + ' ...'
                )) {
            String[] rowValues = new String[] {};
            for (SObjectField field : fields) {
                rowValues.add(String.valueOf(sob.get(field)));
            }
            values.add(String.join(rowValues, ' | '));
        }
    }
}

where the string would be created using:

String jsonString = new Records().toJsonString();

The custom formatting is handled by Apex code and then the standard serialize method handles the standard JSON conversion.

| improve this answer | |
  • If there are so many object and data to process, it will reach the HeadSize limit, How can I process it? – BruceYue May 28 '15 at 4:01
  • @BruceYue To avoid the heap size limit and other governor limits you will have to limit the number of records you work with at once and use multiple requests. – Keith C May 28 '15 at 7:43
  • If I have a child query in the Database.query(), and I also want to build the child object's data as the example I posted, How can I do? – BruceYue Jun 3 '15 at 8:11
  • @BruceYue Add a class for the child object and define a field in Record Child[] children. Then in Records constructor you can use getSObject and use that data to create the child objects. – Keith C Jun 3 '15 at 8:44
  • Thanks Keith, but this is not what i'm asking, I didn't want to break my current json structure. Just want to use a child query and build data to the records, object with name, fields and values. – BruceYue Jun 4 '15 at 1:48

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.