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
Robin Barnwell 3Robin Barnwell 3 

JSON Class Definition

Is this possible in Apex..........

I have set-up a Customer Setting and can access the values through a simple For list:
List<MailchimpInterests__c> Interests = MailchimpInterests__c.getall().values();
for (MailchimpInterests__c X : interests) {
        system.debug(x.interestname__c);   
}
 
I want to use the values in the List to populate a JSON Class.  So rather than having to "Hard Code" it like this:
                public class Interests {
                                public Boolean 5d73033083;
                                public Boolean 5f4a42fad6;
                                public Boolean 81fa33ee25;
                                public Boolean eb61f508b8;
                                public Boolean 67f41d1b49;
                                public Boolean 1b911e7209;
                                public Boolean 2c689eb6b3;
                }

I would prefer some magic happened like this:
                public class Interests {
                                My dynamic List of Values from the Custom Setting
                }

Is this possible?  The reason being the interest list will change over time.
Robin Barnwell 3Robin Barnwell 3
Solved.  The class defininition looks like this with a Map to hold the Key / Value pair
    public class JSON2Apex {
        public String id;
        public String email_address;
        public String status;
        public Merge_fields merge_fields = new Merge_fields();
        public Map<String, Boolean> interests = new Map<String, Boolean>();
    }

Assignment is via using the class e.g.
List<MailchimpInterests__c> Interests = MailchimpInterests__c.getall().values();
           for (MailchimpInterests__c CurrentInterest : Interests) { 
                      if (MailchimpContact[0].Newsletters__c.contains(CurrentInterest.interestname__c)) {
                    Subscriber.Interests.put(CurrentInterest.interestname__c, True);           
           } else {
                    Subscriber.Interests.put(CurrentInterest.interestname__c, False);               
}    
}

Getting there.