- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2022 11:54 PM
Hi All,
I have two fields on incident form Say A(type- journal_input) and B(type- string). The current behaviour whatever comments we add in field A gets copied in field B.
The requirement is- When some user type some comments in field A, irrespective of that users timezone, the comments should show time(in PDT timezone) along with PDT timezone in field A and same should be shown in field B for anyone who is viewing that record.
In short, irrespective the timezone/ system timezone of the user who added comments and the user timezone/system timezone who is viewing that incident record, the comments should show time(in PDT timezone) along with PDT timezone.
Is it possible to achieve this?
If yes, how can we implement this?
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2022 08:42 AM
try this
(function executeRule(current, previous /*null when async*/ ) {
var arr = [];
// get only comment
var comm_plan = new GlideRecord('incident_alert');
comm_plan.addQuery('sys_id', current.sys_id);
comm_plan.query();
while (comm_plan.next()){
var journal = new GlideRecord('sys_journal_field');
journal.addQuery('element', 'u_field_a');
journal.orderByDesc('sys_created_on');
journal.query();
while (journal.next()) {
var time = new GlideDateTime(journal.sys_created_on);
var targetTimezone = 'PDT'; // ensure you give correct timezone Abbreviation
var tz = Packages.java.util.TimeZone.getTimeZone(targetTimezone);
time.setTZ(tz);
var timeZoneOffSet = time.getTZOffset();
time.setNumericValue(time.getNumericValue() + timeZoneOffSet);
var comment = journal.value;
arr.push(time + ' PDT- ' + '\n' + comment + '');
}
}
comm_plan.u_field_b = arr.toString();
comm_plan.update();
})(current, previous);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2022 07:29 AM
I tried this script-
(function executeRule(current, previous /*null when async*/ ) {
var time = new GlideDateTime();
var targetTimezone = 'US/Pacific'; // ensure you give correct timezone Abbreviation
var tz = Packages.java.util.TimeZone.getTimeZone(targetTimezone);
time.setTZ(tz);
var timeZoneOffSet = time.getTZOffset();
time.setNumericValue(time.getNumericValue() + timeZoneOffSet);
// get only comment
var comm_plan = new GlideRecord('incident_alert');
comm_plan.addQuery('sys_id', current.sys_id);
comm_plan.query();
while (comm_plan.next())
{
var journal = new GlideRecord('sys_journal_field');
journal.addQuery('element', 'u_field_a');
journal.orderByDesc('sys_created_on');
journal.query();
while (journal.next()) {
var comment = current.u_field_a.getJournalEntry(1);
var dateRE = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*\n/;
comment = comment.replace(dateRE, '');
comm_plan.u_field_b = time + ' PDT- ' + '\n' + comment;
comm_plan.update();
}
}
})(current, previous);
But it's not working. Could you help me where i am going wrong?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2022 08:42 AM
try this
(function executeRule(current, previous /*null when async*/ ) {
var arr = [];
// get only comment
var comm_plan = new GlideRecord('incident_alert');
comm_plan.addQuery('sys_id', current.sys_id);
comm_plan.query();
while (comm_plan.next()){
var journal = new GlideRecord('sys_journal_field');
journal.addQuery('element', 'u_field_a');
journal.orderByDesc('sys_created_on');
journal.query();
while (journal.next()) {
var time = new GlideDateTime(journal.sys_created_on);
var targetTimezone = 'PDT'; // ensure you give correct timezone Abbreviation
var tz = Packages.java.util.TimeZone.getTimeZone(targetTimezone);
time.setTZ(tz);
var timeZoneOffSet = time.getTZOffset();
time.setNumericValue(time.getNumericValue() + timeZoneOffSet);
var comment = journal.value;
arr.push(time + ' PDT- ' + '\n' + comment + '');
}
}
comm_plan.u_field_b = arr.toString();
comm_plan.update();
})(current, previous);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2022 01:05 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2022 01:34 AM
Sorry but I have already provided enough script and solution for your question.
Please enhance and debug further from your side.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2022 02:07 AM
I made some modifications so now it is printing value in field B but it is showing the all history(comments).
For eg- if i have incident as inc1 having comments as test1 and test2, and when i update another incident inc2 with comments as- test3, then it should show only test3 in field B but it shows test1 and test2 as well. It means it shows all comments of field A, it is not showing comments to incident specific. Can we add query condition in sys journal field table to print comments to incident specific?