How to print the JSON objects as a single string and in a specified format

SAS21
Tera Guru

Need to print the Work Notes Comments as a single string in the below JSON format

02/13/2024 04:32:09 - Dexter Hines \n(User Response)\nHi User,\ntest for incident comments.

 

var jsonObj = {
            'createdOn' : createdOn,
            'user' : user.name.getDisplayValue().toString(),
            'inc_comments' : val,
        };
        var data = JSON.stringify(jsonObj);
            //gs.info(createdOn + "-" + user.name + '\n' + data);
            gs.info(data);
        }
    }
 
the o/p in the bg script is coming as 
{"createdOn":"04/15/2024 02:28:33","user":"Dexter Hines","inc_comments":"(User Response)\r\nHi User\r\n test incident comments. ."}
 
Is it possible to print it in the following format ?  02/13/2024 04:32:09 - Dexter Hines \n(User Response)\nHi User,\ntest for incident comments.
 
Thank you,
Bhavana
1 ACCEPTED SOLUTION

Mohan raj
Mega Sage

 

Hello @SAS21,

To achieve the desired format, you can utilize getJournalEntry(1) for the work notes field.

Example output:

 

 
*** Script: 2018-12-12 23:30:24 - System Administrator (Work notes) Changed the priority of the Incident
 

Based on your code, you'll need to utilize the object variable to retrieve the value. Please refer to the code snippet below:

 

var inc = new GlideRecord('incident');
if (inc.get('57af7aec73d423002728660c4cf6a71c')) {
    var jsonObj = {
        'createdOn': inc.sys_created_on.toString(),
        'user': inc.caller_id.name.toString(),
        'inc_comments': 'Testing in PDI'
    };
    gs.print(jsonObj.createdOn+' - '+jsonObj.user+'\n'+jsonObj.inc_comments);
	// gs.print(inc.work_notes.getJournalEntry(1));
}

 

Mohanraj_0-1713354418068.png

 

If my response helps you to resolve the issue close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.

View solution in original post

4 REPLIES 4

Community Alums
Not applicable

Hi @SAS21 ,

You can refer below code : 

 

var jsonObj = {
            'createdOn' : 'createdOn',
            'user' : 'Test',
            'inc_comments' : 'val',
        };
var data = JSON.stringify(jsonObj);
 gs.info(data);

var finalData = data.replaceAll('\r', ' ');
gs.print(finalData);

 

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak

Meloper
Kilo Sage

 

for (var key in jsonObj ){
    if(jsonObj.hasOwnProperty(key){
    gs.info(jsonObj[key])
     }
}

 

Mohan raj
Mega Sage

 

Hello @SAS21,

To achieve the desired format, you can utilize getJournalEntry(1) for the work notes field.

Example output:

 

 
*** Script: 2018-12-12 23:30:24 - System Administrator (Work notes) Changed the priority of the Incident
 

Based on your code, you'll need to utilize the object variable to retrieve the value. Please refer to the code snippet below:

 

var inc = new GlideRecord('incident');
if (inc.get('57af7aec73d423002728660c4cf6a71c')) {
    var jsonObj = {
        'createdOn': inc.sys_created_on.toString(),
        'user': inc.caller_id.name.toString(),
        'inc_comments': 'Testing in PDI'
    };
    gs.print(jsonObj.createdOn+' - '+jsonObj.user+'\n'+jsonObj.inc_comments);
	// gs.print(inc.work_notes.getJournalEntry(1));
}

 

Mohanraj_0-1713354418068.png

 

If my response helps you to resolve the issue close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.

dgarad
Giga Sage

Hi @SAS21 

can you try the below code.

var jsonObj = {
            'createdOn' : '04/15/2024 02:28:33',
            'user' : 'Dexter Hines',
            'inc_comments' : '(User Response)\r\nHi User\r\n test incident comments.',
        };

var modifiedData= jsonObj.createdOn +'-'+jsonObj.user +jsonObj.inc_comments;
gs.info(modifiedData);
If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad