Add a number of months to the creation date

mustapharabat
Mega Expert

Hello everyone

I want to add the number of months indicated in the field "nbmois" in order to obtain:

- in the field "quit date" = Field "Created" + Field "nbmois"

In the image below, we will have 12/04/2018 13:28:51

Add months to StartDate.PNG

I create one "Business rule" but it doesn't work:

QuitDate();

function QuitDate()

{

var startDate = current.sys_created_on; // the name of field "Created" is "sys_created_on"

var n = current.u_nbmois; // the name of field "nbmois" is "u_nbmois"

current.u_quit_date = startDate.addMonthsUTC(n);// the name of field "Quit Date" is "u_nbmois"

                return;

}

Add months to StartDate_2.PNG

This may be due to a "variable type" problem?:

Add months to StartDate_3.PNG

But I do not have the opportunity to put in the Type "Date/Time"

find_real_file.png

Thank you for you precious help

1 ACCEPTED SOLUTION

QuitDate();



function QuitDate()


{


var n = current.u_nbmois; // the name of field "nbmois" is "u_nbmois"


var startDate = current.sys_created_on; // the name of field "Created" is "sys_created_on"


var endDate = new GlideDate();


endDate.setValue(startDate);


endDate.addMonthsUTC(n);


current.u_quit_date = endDate.getDate();// the name of field "Quit Date" is "u_nbmois"


//current.update() //this depends on business rule, uncomment if it is after business rule


}


View solution in original post

4 REPLIES 4

mustapharabat
Mega Expert

I modified the script but it still does not work!



find_real_file.png


Community Alums
Not applicable

Hi Mustapha,



Please replace your script with the one i am attaching below.


This script worked on my instance.


Hope this works for you too..





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




// Add your code here





var startDate = current.sys_created_on;


var n = current.u_nbmois;



current.u_quit_date = startDate.getDisplayValue();


current.u_quit_date.getGlideObject().addMonthsUTC(n);




})(current, previous);




Thanks,


Ajinkya Pendhari



P.S. : Please mark this answer as helpful or correct if it meets your requirement.


QuitDate();



function QuitDate()


{


var n = current.u_nbmois; // the name of field "nbmois" is "u_nbmois"


var startDate = current.sys_created_on; // the name of field "Created" is "sys_created_on"


var endDate = new GlideDate();


endDate.setValue(startDate);


endDate.addMonthsUTC(n);


current.u_quit_date = endDate.getDate();// the name of field "Quit Date" is "u_nbmois"


//current.update() //this depends on business rule, uncomment if it is after business rule


}


Thank you so much


It works!