• Robin Barnwell 3
  • NEWBIE
  • 5 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 3
    Replies
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.
I'm integrating Salesforce with Mailchimp as the default connecting isn't doing what I need.  I've created a simple class to hold the REST Body

public class MailChimpSubscriberJson{
    public class Merge_fields {
        public String FNAME;
        public String LNAME;
    }
    public class JSON2Apex {
        public String id;
        public String email_address;
        public String status;
        public Merge_fields merge_fields;
    }
}

But my assignment works for apart of the sub-class, Merge_fields.  Specifically this line of code is wrong, but I can't see why.  There is data in the Firstname.

                Subscriber.merge_fields.FNAME=MailchimpContact[0].Firstname;

I'm getting this error, common.apex.runtime.impl.ExecutionException: Attempt to de-reference a null object.  I can only think Subscriber.merge_fields.FNAME is incorrect.
I want to connect Salesforce to Mailchimp so I can send an Apex Callout with a REST PUT request to update a subscriber in Mailchimp.  I've connected Postman (https://www.getpostman.com/) to Mailchimp and go the core REST statement defined and working.  As part of this I needed to set-up the OAuth connection.  This was straight forward to do:
1. In Mailchimp, set-up the connecting App, this generates the Client ID and Client Secet to use.
2. In Postman, request a new Access Token, as shown
User-added image
3. In Postman, use the Access Token as authentication in the REST Header, like this

GET /3.0/Lists/ HTTP/1.1
Host: us16.api.mailchimp.com
Authorization: Bearer c36db89lkjh8hkh8l6ae0005bfc3

I'm really struggling to set-up the same for Salesforce.....  
1. Connected Apps aren't relevant as that is for apps that wish to get data from Salesforce, I want the other way around
2. Named Credentials seems to be the right place but this asks for an Authentication Provider, 
User-added image

3. The Authentication Provider picklist doesn't include Mailchimp, unless there is a "generic" one to use
User-added image
So to get it to work I've had to hard code the Access Token I got for Postman into the request in the Apex Callout as shown

public class Mailchimp {

    public static string  getMailChimp() {          
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('https://us16.api.mailchimp.com/3.0/Lists/'); 
    request.setHeader('Authorization', 'Bearer c36dbf7jhv89jbnjnkuf6a7a16ae0005bfc3');
    request.setMethod('GET');
    HttpResponse response = http.send(request);   
    string a = response.getBody();      
    system.debug('Here you go - ' + a);
    Return a;
    }     
}

What am I missing??
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.
I'm integrating Salesforce with Mailchimp as the default connecting isn't doing what I need.  I've created a simple class to hold the REST Body

public class MailChimpSubscriberJson{
    public class Merge_fields {
        public String FNAME;
        public String LNAME;
    }
    public class JSON2Apex {
        public String id;
        public String email_address;
        public String status;
        public Merge_fields merge_fields;
    }
}

But my assignment works for apart of the sub-class, Merge_fields.  Specifically this line of code is wrong, but I can't see why.  There is data in the Firstname.

                Subscriber.merge_fields.FNAME=MailchimpContact[0].Firstname;

I'm getting this error, common.apex.runtime.impl.ExecutionException: Attempt to de-reference a null object.  I can only think Subscriber.merge_fields.FNAME is incorrect.
I want to connect Salesforce to Mailchimp so I can send an Apex Callout with a REST PUT request to update a subscriber in Mailchimp.  I've connected Postman (https://www.getpostman.com/) to Mailchimp and go the core REST statement defined and working.  As part of this I needed to set-up the OAuth connection.  This was straight forward to do:
1. In Mailchimp, set-up the connecting App, this generates the Client ID and Client Secet to use.
2. In Postman, request a new Access Token, as shown
User-added image
3. In Postman, use the Access Token as authentication in the REST Header, like this

GET /3.0/Lists/ HTTP/1.1
Host: us16.api.mailchimp.com
Authorization: Bearer c36db89lkjh8hkh8l6ae0005bfc3

I'm really struggling to set-up the same for Salesforce.....  
1. Connected Apps aren't relevant as that is for apps that wish to get data from Salesforce, I want the other way around
2. Named Credentials seems to be the right place but this asks for an Authentication Provider, 
User-added image

3. The Authentication Provider picklist doesn't include Mailchimp, unless there is a "generic" one to use
User-added image
So to get it to work I've had to hard code the Access Token I got for Postman into the request in the Apex Callout as shown

public class Mailchimp {

    public static string  getMailChimp() {          
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('https://us16.api.mailchimp.com/3.0/Lists/'); 
    request.setHeader('Authorization', 'Bearer c36dbf7jhv89jbnjnkuf6a7a16ae0005bfc3');
    request.setMethod('GET');
    HttpResponse response = http.send(request);   
    string a = response.getBody();      
    system.debug('Here you go - ' + a);
    Return a;
    }     
}

What am I missing??