I would like to pull only the day, month and year of the creation date of the column ''created" according to the script sent below, but it is not getting all the fields

1231
Tera Contributor

find_real_file.png

 

var task = new GlideRecord('task_sla');
task.addQuery('active', false);

task.query();

while (task.next()) {
   
    var task_sysid = task.task;
    //gs.print(task_sysid);
    var inc = new GlideRecord('incident')
    //inc.addQuery('active', false)
    inc.addQuery('sys_id', '=', task_sysid)
    inc.query();
    //
    while (inc.next()) {
        
        task.u_dia_fc = inc.u_dia_fc;
        task.u_mes_fc = inc.u_mes_fc;
        task.u_ano_fc = inc.u_ano_fc;

      
        task.update();
    }
}
1 ACCEPTED SOLUTION

You need to replace 'gr' by 'current' if you are using this code in a business rule.

 var gdt = new GlideDateTime(current.sys_created_on); 

Also, you have wrongly assigned the values below -

    date[0] = u_ano_ab;

    date[1] = u_mes;

    date[2] = u_dia;

Instead, you need to fetch the value for date, month and year as -

current.<yourFieldNameForYear> = date[0];

current.<yourFieldNameForMonth> = date[1];

current.<yourFieldNameForDate> = date[2];

 

For example, if u_dia should store the date so it will be -

current.u_dia = date[2];

 

Please mark my answer as correct or helpful if applicable.

View solution in original post

4 REPLIES 4

Aishwarya Thaku
Tera Expert
Tera Expert

Hi,

 

The screenshot does not clearly define what is your expected output but I assume with the title that you need to fetch the date, month, and year from the Created Date.  For that, you can try the below code:

var gdt = new GlideDateTime(gr.sys_created_on); //say sys_created_on is the column name

var date = gdt.getLocalDate();

//date variable will contain the only the date part of the date/time field


date =date.toString();
date = date.split("-");

date[0] will be the year

date[1] will be the month

date[2] will be the date

 

Please mark my answer as correct or helpful if applicable.

 

Thanks,

Aishwarya

I did according to what was informed in a business rule and it didn't work, according to what was informed

 

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

    var gdt = new GlideDateTime(gr.sys_created_on); //say sys_created_on is the column name

    var date = gdt.getLocalDate();

    //date variable will contain the only the date part of the date/time field


    date = date.toString();
    date = date.split("-");

    date[0] = u_ano_ab;

    date[1] = u_mes;

    date[2] = u_dia;

})(current, previous);

You need to replace 'gr' by 'current' if you are using this code in a business rule.

 var gdt = new GlideDateTime(current.sys_created_on); 

Also, you have wrongly assigned the values below -

    date[0] = u_ano_ab;

    date[1] = u_mes;

    date[2] = u_dia;

Instead, you need to fetch the value for date, month and year as -

current.<yourFieldNameForYear> = date[0];

current.<yourFieldNameForMonth> = date[1];

current.<yourFieldNameForDate> = date[2];

 

For example, if u_dia should store the date so it will be -

current.u_dia = date[2];

 

Please mark my answer as correct or helpful if applicable.

Aman Kumar S
Kilo Patron

Hey,

Try these:

var dt = new GlideDateTime();
gs.info( dt.getDayOfMonth() +"   "+ dt.getMonth() +"   "+dt.getYear());

//Output :13   5   2022

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

Best Regards
Aman Kumar