Parsing JSON obj to notification body

Avee90
Tera Contributor
Hi Folks,

  I'm trying to pass incident details to mail body. I've to pass incident record fields to mail body based on assignment group. I'm storing incident fields and values in JSON obj and trying to pass to mail body using ${event.parm2}. I'm getting the data from parm 2 but as obj form. Can anyone help to get the JSON obj data plain text( one field by one field). I have given  event queue bellow and previewed mail body.

gs.eventQueue(
'Event_name', grGroup, arrayGrp, JSON.stringify(parsedArr[y].arr));
1 ACCEPTED SOLUTION

Amit Pandey
Kilo Sage

Hi @Avee90 

 

Please concatenate both the field name and value within the loop.

 

var incidentFieldsJSON = JSON.parse(event.parm2);

// Construct plain text representation of the incident fields
var plainTextBody = "Incident Details:\n";
for (var key in incidentFieldsJSON) {
    if (incidentFieldsJSON.hasOwnProperty(key)) {
        plainTextBody += key + ": " + incidentFieldsJSON[key] + "\n";
    }
}

email.setBody(plainTextBody);

 

 

Please mark my answer helpful and correct.

 

Regards,

Amit

View solution in original post

3 REPLIES 3

Amit Pandey
Kilo Sage

Hi @Avee90 

 

If you're passing a JSON object as a parameter and want to display its contents in plain text format within the email body, you'll need to parse the JSON object and extract its fields and values.

 

 var incidentFieldsJSON = JSON.parse(event.parm2);
var plainTextBody = "Incident Details:\n";

    for (var key in incidentFieldsJSON) {
        if (incidentFieldsJSON.hasOwnProperty(key)) {
            plainTextBody += incidentFieldsJSON[key] + "\n";
        }
    }

    email.setBody(plainTextBody);

Please try this and let me know if it works.

 

Regards,

Amit

Thank you,
It is working but getting only values not getting field names which are in JSON obj.
Output: INC3258000 8 3 2023-12-15 18:31:09 Possible Phishing Incident 0
It should be:

Incident:INC3258000

State: 8
Priority: 3
created:  2023-12-15 18:31:09
Short Description:Possible Phishing Incident
Something like
          

Amit Pandey
Kilo Sage

Hi @Avee90 

 

Please concatenate both the field name and value within the loop.

 

var incidentFieldsJSON = JSON.parse(event.parm2);

// Construct plain text representation of the incident fields
var plainTextBody = "Incident Details:\n";
for (var key in incidentFieldsJSON) {
    if (incidentFieldsJSON.hasOwnProperty(key)) {
        plainTextBody += key + ": " + incidentFieldsJSON[key] + "\n";
    }
}

email.setBody(plainTextBody);

 

 

Please mark my answer helpful and correct.

 

Regards,

Amit