- 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
12-01-2023 07:51 AM - edited 12-01-2023 07:52 AM
Journal fields (I mean) the activity formatter does not display their value the journal entries from the sys_journal_field but from a cache - the history records (tables sys_history_set and sys_history_line).
Inserting into sys_journal_field directly will not rebuild that cache.
You'd be better of setting the fields like you'd normally do.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 07:49 AM
Hello @-O-
Thank you for your comment, but can you explain more how I can achieve this:
1-The requirement is to copy all the comments and work notes from the source record (problem) to the target record (sn_customerservice_task) (in the same order + having the same user as commenter)
I didn't find a method to do that other than this way:
/* traget record (a gliderecord of the target table in our case it's a 'sn_customerservice_task' record) */
if (element == 'work_notes') {
targetRecord.work_notes.setJournalEntry(value, sys_created_by);
}
if (element == 'comments') {
targetRecord.comments.setJournalEntry(value, sys_created_by);
}
targetRecord.update();
Is there a way to rebuild the cache?
Is there any way to trigger 'rebuilding that cache' manualy from my script?
Note: I run the following logic in a fix script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 03:48 PM
Clearing the cache can be done by deleting the sys_history_set record for the target record (to which you copy/add the journal entries).
This will cascade delete the sys_history_line records.
Then - as far as I remember - you just need to load the record into a sn_hw.HistoryWalker instance.
That should rebuild the cache.
Though the cache is automatically re-built when a user opens the record next time, so you don't really need to do the rebuilding, just the removal.
But you would also need to make sure the audit (table sys_audit) contains entries for the copied journal notes - as that is in fact what is used to build the stream.
However, I would push back hard against this approach.
I would recommend creating a summary of journal entries and I would add that summary to some field or to the target records activity stream so it could be clearly seen what was copied and what is genuinely the current records journal stream.
One can use GlideSPScriptable to get the journal stream as a JSON:
var stream = new GlideSPScriptable().getStream('incident', '1c741bd70b2322007518478d83673af3');
gs.debug(JSON.stringify(stream, null, '\t'));
It is true though that this has a small negative side to it: what TZ to use to include the dates.
Cause once those added, remain as were added even if a user in a different TZ opens the record.
As a workaround UTC could be used - which should be indicated too.
- 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)