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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

comments time should be shown only in PDT? but for which field? field A ?

If field A then I doubt this would be possible since it's journal field and would show time in activity log as per user's settings

Regards
Ankur

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

Whatever comments are added in field A are copied in field B. The requirement is to show the comments time(in PDT timezone) along with PDT timezone for field B.

Hi,

okay then refer this blog on how to convert date/time from 1 timezone to another

you already know the current time when comments are added; just convert that to PDT and concatenate the comments and add for field B

Convert timezone

If my blog helps please mark it helpful and also bookmark it

regards
Ankur

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

What i want to do is- I want to fetch the Time and Timezone from field A, convert the time and timezone to PDT and then paste the comments' converted Time and Timezone in field B.

For eg-

This is field A comments:-  2022-04-04 13.51.03 IST 

                                                 network issue

I want to convert above time and timezone and paste it in field B like below-

Expected output-

This should be field A comments:-  2022-04-04 01.21.03 PDT

                                                              network issue

In this way I want to show comments time and PDT time zone in field B.

Below is the BR for copying comments from field A to field B-

- Before, insert, update

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

var gr = new GlideRecord('incident_alert');

gr.addQuery('sys_id',current.sys_id);

gr.query();

while gr.next()

{

gr.field_b= current.field_a.getJournalEntry(-1);

gr.update():

}

})(current, previous);

 

How can this be implemented?

Just an idea, can we create one more field, paste the Time and Timezone of field A in new field, convert it to PDT and then paste it to field B ?

Is this possible? If yes, then how can we achieve this?