0

Right now, auto emails are sending out with "Case Resolution" in the email because the field name is being pulled in. I don't want to change the field name. I want to replace the field reference with the text "Case Update". Can anyone help with that? Here is a piece of the code that seems to be pulling in the field name. Thank you!

JSONGenerator gen = JSON.createGenerator(true);
    gen.WriteStartObject();
    gen.WriteFieldName('to');
    gen.WriteStartArray();

    for(Case c : newCaseList){
        gen.WriteString(c.Email__c);
    }

    gen.WriteEndArray();

    gen.WriteFieldName('sub');          
    gen.WriteStartObject();
    gen.WriteFieldName('-Name-');
    gen.WriteStartArray();

    for(Case c : newCaseList){
       gen.WriteString(c.Calculated_Contact_Name__c);
    }   

    gen.WriteEndArray();

    gen.WriteFieldName('-CaseId-');
    gen.WriteStartArray();

    for(Case c : newCaseList){
        gen.WriteString(c.CaseNumber);
    }

    gen.WriteEndArray();

    gen.WriteFieldName('-CaseThreadId-');
    gen.WriteStartArray();

    for(Case c : newCaseList){
        gen.WriteString(c.Case_Thread_ID__c);
    }   

    gen.WriteEndArray();  

    gen.WriteFieldName('-FromEmail-');
    gen.WriteStartArray();

    for(Case c : newCaseList){
        gen.WriteString(c.From_Email_LOB__c);
    }   

    gen.WriteEndArray();

    gen.WriteFieldName('-CaseCreator-');
    gen.WriteStartArray();

    for(Case c : newCaseList){
        gen.WriteString(c.Creator_Name__c);
    }  

    gen.WriteEndArray();        
    gen.WriteEndObject();
    gen.WriteEndObject();
    gen.Close();
6
  • Can we get a more complete code sample? Also, I'm a bit concerned for your "gen" class. It might be an unnecessary layer of abstraction. Salesforce enforces strict governor limits and unnecessary layers of abstraction add to the CPU load which is a no-no.
    – gNerb
    Sep 11 '17 at 20:13
  • Hi Toby. The apex class is nearly 400 lines long, so I am not sure which parts to show you.
    – Heather
    Sep 11 '17 at 20:38
  • I know, the code is a cluster :( That's why we're trying to simplify and eventually move away from it.
    – Heather
    Sep 11 '17 at 20:39
  • From what I can tell you are using your gen class to generate the email to be sent. That would be were most of the code we need to see is.
    – gNerb
    Sep 11 '17 at 20:52
  • Oh! I edited the code in, at the top :) Thanks so much for having a look!
    – Heather
    Sep 11 '17 at 20:55
0

From what I can tell after playing around with it for a while. You appear to be doing everything correctly. My money is on a data issue.

Try debugging the data before and after you write it to the JSONGenerator:

system.debug('Case Resolution: ' + c.Resolution);
gen.writeString(c.resolution);
system.debug(gen.getAsString);

If the debug before you write it is wrong, it's a data issue. If the debug after you write it is wrong, it's a bug in the SF provided JSONGenerator class. If that's the case, consider creating the data and then using JSON.serialize instead.

List<Object> email = new List<Object>();

Map<String, List<Object>> toField = new Map<String, List<Object>>();
toField.put('to', new List<String>());

For (Case c : newCaseList) {
    toField.get('to').add(c.Email__c);
}

email.add(toField);

system.debug(JSON.serialize(email));

Lastly, if both debugs are correct, the issue is 1) not included in the code provided -or- 2) exists in an external system you may be making a call to.

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.