Show comments in PDT timezone

Rose17
Tera Contributor

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?

@Ankur Bawiskar  Your help is highly appreciable.

Thanks in advance.

 

1 ACCEPTED SOLUTION

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

16 REPLIES 16

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?

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Not working. It is not even printing value in field B. I added info messages and tried to print arr and comm_plan.u_field_b. Somehow the values are not shown/printed in field B don't know why.

below is the output-

find_real_file.png

 

find_real_file.png

 

find_real_file.png

 

find_real_file.png

 

 

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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?