new GlideDateTime(current.sys_created_on not copying to variables and posting back to records

Jeffrey Barton
Tera Contributor
I wrote the following business rule script to read the created date from the record and then update two custom fields with the created date but it doesn't seem to be working. 
 
Where did I go wrong?
 
(function executeRule(current, previous /*null when async*/ ) {
    var gcdt = new GlideDateTime(current.sys_created_on);
    var lreview = new GlideDateTime(Current.u_last_review);
    var anreview = new GlideDateTime(current.u_annual_standard_change_review_date);
    // set variable gcdt to current created date in system
    lreview = gcdt;
    anreview = gcdt;

})(current, previous);
5 REPLIES 5

Elijah Aromola
Mega Sage

There are a few things to point out with your script. Take a look at the comments below.

(function executeRule(current, previous /*null when async*/ ) {
    var gcdt = new GlideDateTime(current.sys_created_on);
    var lreview = new GlideDateTime(Current.u_last_review); // should be current with a lowercase c
    var anreview = new GlideDateTime(current.u_annual_standard_change_review_date);
  
    // set variable gcdt to current created date in system
    lreview = gcdt;
    anreview = gcdt;
  
  
    // where are you setting the custom fields? 
    // current.fieldName = lreview;
    // current.fieldName = anreview; 

})(current, previous);

 

I added them on the form as new fields.  it's all on the current form.

Riya Verma
Kilo Sage
Kilo Sage

Hi @Jeffrey Barton ,

 

Hope you are doing great.

 

 

(function executeRule(current, previous /*null when async*/ ) {
    var gcdt = new GlideDateTime(current.getValue('sys_created_on'));
    var lreview = new GlideDateTime(current.getValue('u_last_review'));
    var anreview = new GlideDateTime(current.getValue('u_annual_standard_change_review_date'));
    // set variable gcdt to current created date in system
    lreview = gcdt;
    anreview = gcdt;

 

})(current, previous);

 

or try using  below code :

 

(function executeRule(current, previous /*null when async*/ ) {
    var gcdt = new GlideDateTime(current.sys_created_on.toString());
    var lreview = new GlideDateTime(Current.u_last_review.troString());
    var anreview = new GlideDateTime(current.u_annual_standard_change_review_date.toString());
    // set variable gcdt to current created date in system
    lreview = gcdt;
    anreview = gcdt;

 

})(current, previous);

 

Reason being  - GlideDateTime accpets date in string format in input parameter.

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

it still didn't update the fields.