The CreatorCon Call for Content is officially open! Get started here.

How to fill fields via script

Igor Grun
Giga Contributor

Hello,

This is my first post here.

Sorry if I'm posting in the wrong place. If so, please move it to the correct place.

I have a BR that extracts the month and year from the creation date of a record and includes it in two string fields separately.

This BR works well and works as follows:

It fetches the information from the sys_created_on field, extracts the Month and includes it in the u_notification_creation_month field and does the same for the Year and includes it in the u_notification_creation_year field.

As I already had other records created before this BR, I need to somehow get the creation dates of previous records and include them in the new fields I created.

I searched here in the community and did not find the solution.

Could someone give me a code example for this purpose?

Thanks in advance.

2 REPLIES 2

Claude DAmico
Kilo Sage

You would benefit from learning about Fix Scripts. You can use the concept of your existing code and apply it there. Here is an example that will loop through all incidents that don't have a value in either of the new field. Just update the table referenced and modify the rest for the year and month values you want.

var gr = new GlideRecord('incident');
gr.addQuery('u_notification_creation_month', '').addOrdCondition('u_notification_creation_year', '');
gr.query()

while(gr.next()){
    var createdOn = gr.sys_created_on; //This will be the value (UTC/GMT) rather than the display value (your configured timezone) so use gr.sys_created_on.getDisplayValue() if needed in your timezone
    //code to get month and year from createdOn variable

    if(gr.u_notification_creation_month == ''){
        gr.u_notification_creation_month = MONTHVAR;
    }

    if(gr.u_notification_creation_year == ''){
        gr.u_notification_creation_year = YEARVAR;
    }

    gr.update();
}

 

Claude E. D'Amico, III - CSA

Hi Claude,

 

Thanks in advance for your support.

 

While waiting for feedback on this topic, I worked with a developer and we came up with a solution that resolved the issue.

 

A Fix Script was used, reusing parts of the code that I had implemented in BR.

 

Model follows:

 

var gr = new GlideRecord("my_table");

gr.query();

while (gr.next()){
var gdt= new GlideDateTime(gr.sys_created_on);

gr.setValue("u_notification_creation_month",gdt.getMonthLocalTime());
gr.setValue("u_notification_creation_year",gdt.getYearLocalTime());

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

gr.update();
}