I want to automatically enter the fiscal year for catalog items.

N_Y_1014
Tera Contributor

Hello.

I would like to automatically enter the year in a single line text type variable in a catalog item.

 

Example:
FY2023

 

What kind of script would be needed?
I would appreciate it if you could provide a sample of a similar process.

5 REPLIES 5

Samaksh Wani
Giga Sage
Giga Sage

Hello @N_Y_1014 

 

You need to use onLoad() Catalog Client Script :-

 

 

 

function onLoad(){

var date = new GlideDate();
var year = date.getFullYear();

var result = "FY" +year;
g_form.setValue('fieldName',result);

}

 

 

 

Plz Mark my Solution as Accept and Give me thumbs up, if you find it helpful.

 

Regards,

Samaksh

SwarnadeepNandy
Mega Sage

Hello @N_Y_1014,

To automatically enter the year in a single line text type variable in a catalog item, you can use a Catalog Client Script to set the value of the variable based on the current date. A Catalog Client Script is a script that runs on the client side when a user interacts with a catalog item or record producer.

Here is an example of how to do this:

  • Create a single line text type variable with the name year in your catalog item.
  • Create a Catalog Client Script for your catalog item with the following properties:
    • Type: OnLoad
    • Name: Set year value
    • Script:

 

function onLoad() {
  //Get the current date
  var today = new Date();
  //Get the current year
  var currentYear = today.getFullYear();
  //Format the year as FYyyyy
  var yearValue = "FY" + currentYear;
  //Set the value of the year variable
  g_form.setValue("year", yearValue);
}

 

  • Save the Catalog Client Script and test your catalog item in Service Portal. You should see that the year variable is automatically populated with the value FY2023.

Hope this helps.

 

Kind Regards,

Swarnadeep Nandy

N_Y_1014
Tera Contributor

@SwarnadeepNandy 

@Samaksh Wani 

 

Thank you for your response.

 

I would like to give a fiscal year instead of using the year as it is.

For example, we would like to show April 2023 to March 2024 as FY2023.

Hello @N_Y_1014 

 

This is the fiscal year logic integrate both logic

 

var gdt = new GlideDateTime(gs.nowDateTime());

if(gdt.getMonth()+1 <=3)

return gdt.getYear();

else

return gdt.getYear()+1;

 

 

Let me know if any help required.