Fix script to get month and year from Date field (not Date/Time)

Igor Corr_a
Tera Expert

Hello,

I currently have a default value configuration of 2 string fields (year and month), where, when a record is created in the table, the month and year are obtained from the sys_created_on field for reporting purposes. This works well.

 

I now need to change this configuration by taking the month and year by placing them in the same fields but using a Date field. I managed to perform this configuration through a Business Rule.

 

For records that have already been created, I need to create a fix script to take the month and year values from the Date field and place them in the String fields.

 

For some reason, the Fix Script, when executed, is not going through the entire table and leaving some records without making the change.

 

My table contains almost 30 thousand records to be changed.

 

Could you help me if there is an error in my script?

 

Below is more technical information:

 

Field where I want to get the month and year and put them in 2 other fields separately: date_incident, Date type (not Date/Time).

 

Fields where I want to overwrite and/or insert the value of the date_incident field, separating by month and year:

 

u_notification_creation_month, type String
u_notification_creation_year, type String

 

My fix script:

var gr = new GlideRecord("table containing my records");

gr.query();

while (gr.next()){

var gdt = gr.date_incident.getGlideObject();

var year = gdt.getYearLocalTime();
var month = gdt.getMonthLocalTime();

gr.u_notification_creation_month = month;
gr.u_notification_creation_year = year;

gr.autoSysFields(false);
gr.setWorkflow(false);

gr.update();
}

 

1 ACCEPTED SOLUTION

Igor Corr_a
Tera Expert

Hello,

 

Thanks for the feedback.

 

For my case, the solution below worked:

var gr = new GlideRecord("my_table");

gr.addEncodedQuery("date_incident!=NULL");
gr.query();

while (gr.next()){
	
	var dateObj = gr.date_incident;
    var dateStr = dateObj.toString();

    var gd = new GlideDate();
    gd.setDisplayValue(dateStr);

    var realMonth = gd.getMonthNoTZ().toString();
    var realYear = gd.getYearNoTZ().toString();

    gr.setValue('u_notification_creation_month', realMonth);
    gr.setValue('u_notification_creation_year', realYear);

	gr.autoSysFields(false);
	gr.setWorkflow(false);

	gr.update();
}

 

I'm leaving it here because it may help other people.

 

Best regards,

View solution in original post

3 REPLIES 3

Gopal Allu
Tera Expert

Hi @Igor Corr_a ,

 

You can include that script in a self calling function and check. It will mostly update all the records.

 

 

(function (){ 
var gr = new GlideRecord("table containing my records");

gr.query();

while (gr.next()){

var gdt = gr.date_incident.getGlideObject();

var year = gdt.getYearLocalTime();
var month = gdt.getMonthLocalTime();

gr.u_notification_creation_month = month;
gr.u_notification_creation_year = year;

gr.autoSysFields(false);
gr.setWorkflow(false);

gr.update();
}
} }());

 

If it is helpful please make it correct/helpful.

 

Thanks and Regards,

Allu Gopal.

 

Appanna M
Tera Guru

Hello @Igor Corr_a ,

 

you can use gr.updateMultiple(); 

Refer: https://www.servicenow.com/community/developer-forum/extract-month-year-from-date-into-a-string/m-p/...

 

Please mark my answer if its solves your issue.

 

 

Igor Corr_a
Tera Expert

Hello,

 

Thanks for the feedback.

 

For my case, the solution below worked:

var gr = new GlideRecord("my_table");

gr.addEncodedQuery("date_incident!=NULL");
gr.query();

while (gr.next()){
	
	var dateObj = gr.date_incident;
    var dateStr = dateObj.toString();

    var gd = new GlideDate();
    gd.setDisplayValue(dateStr);

    var realMonth = gd.getMonthNoTZ().toString();
    var realYear = gd.getYearNoTZ().toString();

    gr.setValue('u_notification_creation_month', realMonth);
    gr.setValue('u_notification_creation_year', realYear);

	gr.autoSysFields(false);
	gr.setWorkflow(false);

	gr.update();
}

 

I'm leaving it here because it may help other people.

 

Best regards,