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"
}]
}