- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2023 02:34 AM
Hello team I was trying to transform Problems into CS Task and in the process of that I was copying the work notes and comments.
Script to copy comments and work notes:
/**
* MIBE
* This function will copy all comments and work notes from the a source record to the target record.
* @Param {GlideRecord} targetRecord 'any servicenow table' // the targetRecord record
* @Param {String} source_record_id // the sys_id of the source record
* @returns {String} answer 'the number of comments and work notes copied from the problem'
* usage: javascript:var util = new global.myTransformUtils(); util.copyCommentsAndNotesFromSource(current, current.source_record_id);
*/
copyCommentsAndNotesFromSource: function( /**GlideRecord */ targetRecord, /**string */ source_record_id) {
var answer = 0;
var query = "element_id=" + source_record_id;
var grSJF = new GlideRecord('sys_journal_field');
var isValideQuery = grSJF.isValidEncodedQuery(query);
if (isValideQuery) {
grSJF.addEncodedQuery(query);
grSJF.orderBy('sys_created_on');
//grSJF.setLimit(100);
grSJF.query();
while (grSJF.next()) {
var element = grSJF.getValue('element');
var element_id = grSJF.getValue('element_id');
var value = grSJF.getValue('value');
var sys_created_on = grSJF.getValue('sys_created_on');
var sys_created_by = grSJF.getValue('sys_created_by');
value = gs.getMessage("Created On") + " " + sys_created_on + "\n\r\n" + value;
if (element == 'work_notes') {
targetRecord.work_notes.setJournalEntry(value, sys_created_by);
}
if (element == 'comments') {
targetRecord.comments.setJournalEntry(value, sys_created_by);
}
answer++
targetRecord.update();
}
} else {
gs.info("Invalide query '" + query + "' reported in method copyCommentsAndNotesFromSource() the returned value will be 0");
}
return answer;
}
Script to transform 'problem' record to a 'sn_customerservice_task' record
/**
* MIBE
* This function will create the target record for the current problem.
* @Param {GlideRecord} problem 'problem' // the problem record
* @Param {String} targetTable // the target table in which we will copy the problem.
* @Param {Array} matchingFields // the fields to copy from the problem record to the target record
* @Param {String} csvData // the string containing csvData
* @returns {object} answer {record,cvv} record:'the generated record from problem' csv:'the generated csv data from the problem record'
* usage: javascript:var util = new global.mytransformUtils(); util.copyPRBToTargetRecord(current,"sn_customerservice_task",["sys_created_by","sys_created_on"]);
*/
copyPRBToTargetRecord: function( /**GlideRecord */ problem, /**String */ targetTable, /**Array */ matchingFields, /**String */ csvData) {
var answer = null;
var targetRecord = new GlideRecord(targetTable);
targetRecord.initialize();
targetRecord.autoSysFields(false);
for (var i = 0; i < matchingFields.length; i++) {
var field = matchingFields[i];
var value = problem.getValue(field);
if (field == 'state') {
value = this.generateState(problem,targetTable);
}
if (field == 'impact') {
value = this.generateImpact(problem);
}
if (field == 'urgency') {
value = this.generateUrgency(problem);
}
targetRecord.setValue(field, value);
}
//if(targetTable == 'sn_customerservice_task'){
targetRecord.setValue('correlation_id',problem.getValue('number'));
//}
targetRecord.setValue('u_created_from_problem', problem.getValue('sys_id')); // to keep the track after the transformation.
targetRecord.insert();
answer = {};
if (csvData) {
csvData = this.addToCSVData(targetRecord, csvData);
}
answer.csv = csvData;
answer.record = targetRecord;
targetRecord.autoSysFields(true);
return answer;
}
after the generation of new 'sn_customerservice_task' records I've observed the following display in the activity stream:
but right after I try to add a new work note it shows:
Any idea on why this is happening? and how can I fix it?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2023 08:15 AM
Hello @-O-
I've been trying to clear the cache as suggested here but I couldn't find any history record (sys_history_set or sys_history_line) related to the generated record (sn_customerservice_task).
On the other hand I found that the table sys_audit contains entries for the copied journal notes.
I've also tried to load the record into a sn_hw.HistoryWalker instance. (didn't work as well)
var hw = new sn_hw.HistoryWalker(targetRecord.getTableName(), targetRecord.getUniqueValue());
As a work around I managed to show the activity by adding a new work note at the end of my script for each record.
As for the proposed solution I can't apply it in my case we need an exact replica of the problem record (with separated comments and work notes)
I've also noticed that this behavior is not observable in when generating incident records (it works without the need to add the work note)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2024 05:34 AM
Hello @MIBE , I facing the same issue, Did you find the root cause for this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-11-2024 07:03 AM
Hello @pradeep reddy2
As shown in the Accepted Solution.
The work around was adding a new work note at the end of my script for each record.