0

Below is my code that my batch calls to create JSON to call and send emails through sendgrid. I am trying to loop through the list of contacts and add them to the CC object's array. i have attempted a few different placements of gen.writeStartObject()'s and gen.writeEndObject()'s. I keep receiving errors on the line i put the **** on below. Some errors are Can not start an object, expecting field name, but now i am just receiving an unhandled salesforce exception and am unsure of where to go from here (System.UnexpectedException: Salesforce System Error: 1055502610-35413 (-1103676269) (-1103676269)).

Public Static String jsonGenerator(map<Account, list<Dunning_Record__c>> 
emailActionMap, map<id, list<Contact>> accountIdToContactsListMap){
                JsonGenerator gen = JSON.createGenerator(true);

                gen.writeStartObject();
                gen.writeFieldName('personalizations');
                gen.writeStartArray();

                string clientAREmail = '';
                string clientARDisplayName = '';

                //start for loop for emails
                for(Account acct : emailActionMap.keySet()) {
                    for(Dunning_Record__c dunRec : emailActionMap.get(acct)) {
                        clientAREmail = dunRec.Dunning_Action__r.Dunning_Profile__r.Client_AR_Email__c;
                        clientARDisplayName = dunRec.Dunning_Action__r.Dunning_Profile__r.Client_AR_Display_Name__c;
                        gen.writeStartObject();
                        gen.writeFieldName('to');
                        gen.writeStartArray();

                        gen.writeStartObject();
                        gen.writeObjectField('email', accountIdToContactsListMap.get(acct.id)[0].Email);
                        gen.writeObjectField('name', accountIdToContactsListMap.get(acct.id)[0].Name);
                        gen.writeEndObject();

                        gen.writeEndArray();

                        if(dunRec.Dunning_Action__r.Copy_Account_Owner__c && accountIdToContactsListMap.get(acct.id).size() > 1) {

                            gen.writeFieldName('cc');
                            gen.writeStartArray();    
                            System.debug(gen.getAsString());
                            for(Contact con : accountIdToContactsListMap.get(acct.id)) {
                                if(con.Email == accountIdToContactsListMap.get(acct.id)[0].Email) {
                                    continue;
                                } else {
                      ****getting error here       gen.writeStartObject();
                                    gen.writeObjectField('email', con.Email);
                                    gen.writeObjectField('name', con.Name);
                                    gen.writeEndObject();
                                }
                            }
                            gen.writeEndArray();
                            gen.writeEndObject();
                        }

                        system.debug('acctid ==== ' + acct.id);
                        gen.writeFieldName('dynamic_template_data');
                        gen.writeStartObject();
                        gen.writeStringField('userFullName', acct.MS_Credit_Rep_Name__c);
                        gen.writeStringField('accountNumber', acct.MS_Internal_ID__c);
                        gen.writeStringField('balance', '$'+string.valueOf(acct.MS_AR_Outstanding__c));
                        gen.writeStringField('subject', dunRec.Dunning_Action__r.Email_Subject__c);
                        gen.writeStringField('bankAccountName', dunRec.Dunning_Action__r.Dunning_Profile__r.Bank_Account_Name__c);
                        gen.writeStringField('remittanceAddress', dunRec.Dunning_Action__r.Dunning_Profile__r.Program_Remittance_Address__c);
                        gen.writeStringField('body', dunRec.Dunning_Action__r.Email_body__c);
                        gen.writeStringField('imageURL', dunRec.Dunning_Action__r.Dunning_Profile__r.Hosted_Logo_URL__c);

                        gen.writeEndObject();
                        gen.writeEndObject();
                    }
                }

Sample Request Body:

{
    "personalizations": [{
        "to": [{
            "email": "[email protected]"
        }],
        "cc": [{
            "email": "[email protected]"
        }, {
            "email": "[email protected]"
        }, {
            "email": "[email protected]"
        }],
        "substitutions": {
            "%fname%": "recipient",
            "%CustomerID%": "CUSTOMER ID GOES HERE"
        },
        "subject": "YOUR SUBJECT LINE GOES HERE"
    }]
}
7
  • 2
    Why not just use OOTB serialization/deserialization? It is much simpler.
    – Adrian Larson
    Commented Mar 21, 2019 at 22:04
  • i was following examples i found in this article developer.salesforce.com/… i am very new to JSON and callouts in general so i dont have a whole lot of background knowledge to know what i should be using, my apologies.
    – Logie C
    Commented Mar 21, 2019 at 22:29
  • Do you have an example of what the output JSON should look like? If so, please edit your question to include it.
    – Adrian Larson
    Commented Mar 21, 2019 at 22:36
  • Unfortunately i could not find an example of someone using multiple emails in the cc portion of the json. I only have a sample request body, but i can add that into my post if needed, found here sendgrid.com/docs/API_Reference/api_v3.html.
    – Logie C
    Commented Mar 21, 2019 at 22:43
  • That link is just to the documentation home page and contains no sample request.
    – Adrian Larson
    Commented Mar 21, 2019 at 23:29

1 Answer 1

0

I was able to get my code to work properly using the below code:

Public Static String jsonGenerator(map<Account, list<Dunning_Record__c>> emailActionMap, map<id, list<Contact>> accountIdToContactsListMap){
        JsonGenerator gen = JSON.createGenerator(true);

        gen.writeStartObject();
        gen.writeFieldName('personalizations');
        gen.writeStartArray();

        string clientAREmail = '';
        string clientARDisplayName = '';

        //start for loop for emails
        for(Account acct : emailActionMap.keySet()) {
            for(Dunning_Record__c dunRec : emailActionMap.get(acct)) {
                clientAREmail = dunRec.Dunning_Action__r.Dunning_Profile__r.Client_AR_Email__c;
                clientARDisplayName = dunRec.Dunning_Action__r.Dunning_Profile__r.Client_AR_Display_Name__c;
                gen.writeStartObject();
                gen.writeFieldName('to');
                gen.writeStartArray();

                gen.writeStartObject();
                gen.writeObjectField('email', accountIdToContactsListMap.get(acct.id)[0].Email);
                gen.writeObjectField('name', accountIdToContactsListMap.get(acct.id)[0].Name);
                gen.writeEndObject();

                gen.writeEndArray();

                if(dunRec.Dunning_Action__r.Copy_Account_Owner__c && accountIdToContactsListMap.get(acct.id).size() > 1) {
                    gen.writeFieldName('cc');
                    gen.writeStartArray();
                    for(Contact con : accountIdToContactsListMap.get(acct.id)) {
                        if(con.Email != accountIdToContactsListMap.get(acct.id)[0].Email) {
                            gen.writeStartObject();
                            gen.writeObjectField('email', con.Email);
                            gen.writeObjectField('name', con.Name);
                            gen.writeEndObject();
                        }
                    }
                    gen.writeEndArray();
                }


                system.debug('acctid ==== ' + acct.id);
                gen.writeFieldName('dynamic_template_data');
                gen.writeStartObject();
                gen.writeStringField('userFullName', acct.MS_Credit_Rep_Name__c);
                gen.writeStringField('accountNumber', acct.MS_Internal_ID__c);
                gen.writeStringField('balance', '$'+string.valueOf(acct.MS_AR_Outstanding__c));
                gen.writeStringField('subject', dunRec.Dunning_Action__r.Email_Subject__c);
                gen.writeStringField('bankAccountName', dunRec.Dunning_Action__r.Dunning_Profile__r.Bank_Account_Name__c);
                gen.writeStringField('remittanceAddress', dunRec.Dunning_Action__r.Dunning_Profile__r.Program_Remittance_Address__c);
                gen.writeStringField('body', dunRec.Dunning_Action__r.Email_body__c);
                gen.writeStringField('imageURL', dunRec.Dunning_Action__r.Dunning_Profile__r.Hosted_Logo_URL__c);
                // red wing image 'https://marketing-image-production.s3.amazonaws.com/uploads/0ffa10cf6be54433e3ec8e4404614d8adb31d807e61c3533980b7667b8042c499c6120bec8e26c925fcdde11a048e336b4219f23144a821201eeae9b279b35c4.png'
                gen.writeEndObject();
                gen.writeEndObject();
            }
        }
        //end email loop
        gen.writeEndArray();

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .