Add days to date

JG16
Tera Contributor

hi,

 

In Business Rule How can i add years/days to date .

I have tried like below 

var createdDate = new GlideDateTime(attestationGR.sys_created_on);

var duedate = createdDate.addDays(365); //default 1 year

but it throws the below error. Please help me. I cannot use addLocaldays. how to  go with adddaysUTC  function to align time or another way ?

ERROR : Function addDays is not allowed in scope sn_hr_core. User either addDaysUTC() or addDaysLocalTime() instead

 

Thanks 

JG

 

 

 

 

 

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

Hi JG,

that is correct addDays() method is not allowed in scoped application; instead use addDaysUTC()

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

My concern is how to convert UTC time  to  server time,suppose if server is in UK/US ?  

Hi Ankur,

 

I tried by using addDaysUTC() and addDaysLocalTime() in business rule of scoped application but still, it is not giving the value, it says undefined.

   // Add your code here
    var date = new GlideDateTime(current.sys_updated_on);

    var gdt = date.addDaysLocalTime(5);
    var val;
    gs.info('test record ' + date.addDaysLocalTime(5));


    var gr = new GlideRecord('sn_compliance_control_sla');
    gr.addEncodedQuery('u_control.sys_id=' + current.sys_id + '^u_state=draft^ORu_state=in_progress');
    gr.orderByDesc('sys_updated_on');
    gr.query();
    if (gr.next()) {
        if (previous.state == 'attest') {
            gr.u_state = 'cancelled';
            gr.update();
        } else if (previous.state == 'control_manager_review' || previous.state == 'compliance_review' || previous.state == 'compliance_manager_review' || previous.state == 'review' || previous.state == 'retired') {
            gr.u_state = 'closed';
            gr.update();
        } else if (gr.u_close_date < new GlideDateTime()) {
            gr.u_state = "breached";
            gr.u_active = false;
            gr.update();

        }

    } else {
        gr = new GlideRecord('sn_compliance_control_sla');
        gr.initialize();
        gr.u_name = current.name;
        gr.u_control = current.sys_id;
        gr.u_start_date = current.sys_updated_on;
        gr.u_close_date = gdt;
        gr.u_state = 'draft';
        gr.u_active = true;

        gr.insert();
        val = gr.sys_id.toString();
        gs.info('test record id= ' + val);
        current.setValue('u_sla', val);
        current.update();
    }
there is no error showing, but this is not working as expected.
 
 
Please suggest some solution for this issue.
shaik_chan_1_0-1733230042094.png

 

Omkar Mone
Mega Sage

Hi 

Try using addDaysLocalTime(); 

For further clarification please visit this link.

 

Let me know if it works.

 

Regards,

Omkar Mone.

www.dxsherpa.com