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

Hi,

the time when comments is added is the current system time

just convert it to PDT using link I shared

something like this; just enhance it

(function executeRule(current, previous /*null when async*/ ) {

	var time = new GlideDateTime();
	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);

	// get only comment
	var comment = current.field_a.getJournalEntry(1);
	var dateRE = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*\n/;
	comment = comment.replace(dateRE, '');

	current.field_b = comment + ' PDT Time ' + time;

})(current, previous);

Regards
Ankur

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

I also want previous comments(history) added in field B like below-

2022-04-11 05:27:09 PDT
test5

2022-04-11 05:25:39 PDT
test4

2022-04-11 05:23:09 PDT
test3

Hi,

then query sys_journal_field; get the created on time; convert that to PDT and print it with value

Regards
Ankur

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

could you please provide the script?

I already shared the logic yesterday.

query sys_journal_field; get the created on time; convert that to PDT and print it with value

Regards
Ankur

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