- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2021 11:21 AM
Hi Everyone,
I want to extract the Month & Year from the date field 'opened_at' in the Task table into a custom string like u_month & u_year. Does someone knows how can I achieve this?
Many Thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-21-2021 05:40 AM
Are you getting any value for the month?
I just ran a slightly modified version in my usual test setup; a "display" business rule and outputting to an error message, like this:
var gdt = new GlideDateTime(current.opened_at);
var gMonth = gdt.getMonth();
var gYear = gdt.getYear();
//current.u_month = gMonth.getValue();
//current.u_year = gYear.getValue();
gs.addErrorMessage("The year is " +gYear+ " and the month is " +gMonth);
With this I got a value of 1 for month when I opened an Incident record created in January. So, the month value will be 1 thru 12. Now if this is not suitable for your use and you would prefer it to be text, you will need to translate the values in the business rule.
And as for when to run the business rule. I have date calculating business rules that are running as "before" and also some running as "after" depending on the entire functionality in the script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2021 12:27 PM
Something like this should do the trick:
var gdt = new GlideDateTime(current.opened_at);
var gMonth = gdt.getMonth();
var gYear = gdt.getYear();
current.u_month = gMonth.getValue();
current.u_year = gYear.getValue();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2021 04:53 PM
Hi Ian,
I tried It many times with a business rule for the Month but it's not working, Do you know what could be wrong?
u_month is a string
When is before
(function executeRule(current, previous /*null when async*/) {
var gdt = new GlideDateTime(current.opened_at);
var gMonth = gdt.getMonth();
current.u_month = gMonth.getValue();
})(current, previous);
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-21-2021 05:40 AM
Are you getting any value for the month?
I just ran a slightly modified version in my usual test setup; a "display" business rule and outputting to an error message, like this:
var gdt = new GlideDateTime(current.opened_at);
var gMonth = gdt.getMonth();
var gYear = gdt.getYear();
//current.u_month = gMonth.getValue();
//current.u_year = gYear.getValue();
gs.addErrorMessage("The year is " +gYear+ " and the month is " +gMonth);
With this I got a value of 1 for month when I opened an Incident record created in January. So, the month value will be 1 thru 12. Now if this is not suitable for your use and you would prefer it to be text, you will need to translate the values in the business rule.
And as for when to run the business rule. I have date calculating business rules that are running as "before" and also some running as "after" depending on the entire functionality in the script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-08-2021 06:39 PM
Many thanks Ian, it finally works as a Display rule.
Kind Regards