Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

what could be the reason for the below error??

KM SN
Tera Expert

KMSN_0-1743160632313.png


The script I am using is 

var latestcomment = fd_data._2__create_catalog_task.catalog_task.work_notes.getJournalEntry(1);
gs.log("comment - recent work notes ");
var comments = JSON.parse(latestcomment);
gs.log("comment - recent work notes after parse");

for(var i=0; i<comments.length; i++){
 if(comments[i]['Response']=='Reject'){
    gs.log("comment - its coming to for loop "+comments[i]['Name']);
    var rejected_comments = comments[i]["Comments"];
    gs.log("Rejected Comments are "+rejected_comments);
}
}
return rejected_comments;


The recent comment will look like:
[{"Name":"Gopinath ","Response":"Approve","Comments":"No longer Required"}, {"Name":"Gopinath Thangamani","Response":"Reject","Comments":"Approving it for further "}]

3 REPLIES 3

Vasantharajan N
Tera Sage
Tera Sage

@KM SN - line "var comments = JSON.parse(latestcomment);" is causing the problem as you are trying to get the j=worknotes journal entry which is not in the JSON format by default. Please parse only the comment which is expected to be JSON. 


Thanks & Regards,
Vasanth

@KM SN - You can use the below syntax to parse the journal entry which holds JSON message. 

var latestcomment = fd_data._2__create_catalog_task.catalog_task.work_notes.getJournalEntry(1).split("\n")[1];

gs.log("comment - recent work notes ");
var comments = JSON.parse(latestcomment);
gs.log("comment - recent work notes after parse");

for(var i=0; i<comments.length; i++){
 if(comments[i]['Response']=='Reject'){
    gs.log("comment - its coming to for loop "+comments[i]['Name']);
    var rejected_comments = comments[i]["Comments"];
    gs.log("Rejected Comments are "+rejected_comments);
}
}
return rejected_comments;

 

I've added the expected JSON In worknotes and used the syntax provided above to parse the JSON 

VasantharajanN_0-1743161961350.png

 


Thanks & Regards,
Vasanth

J Siva
Kilo Patron
Kilo Patron

Hi @KM SN 

Just modify your script as below.

var latestcomment = fd_data._2__create_catalog_task.catalog_task.work_notes.getJournalEntry(1);
gs.log("comment - recent work notes ");
//---------------------START-------------------------------
var arr = latestcomment.split("(Additional comments)\n");
var comments = JSON.parse(arr[1].toString());
//---------------------END-------------------------------
gs.log("comment - recent work notes after parse");

for(var i=0; i<comments.length; i++){
 if(comments[i]['Response']=='Reject'){
    gs.log("comment - its coming to for loop "+comments[i]['Name']);
    var rejected_comments = comments[i]["Comments"];
    gs.log("Rejected Comments are "+rejected_comments);
}
}
return rejected_comments;

 

Hope this helps.
Regards
Siva