how to create a business rule for add days in a field date

Lucas11
Tera Contributor

Hey guys. I need to automatically calculate the contract end date.
I need to make a business rule where it takes the "Start date" field:

Captura de Tela 2022-12-07 às 11.01.56.png

Add the days in the "Term of contract" field:

Captura de Tela 2022-12-07 às 11.02.57.png

Where 1 year adds up to 365 days and 2 years adds up to 730 days.

 

And put the result in date format in the "End date" field:

Captura de Tela 2022-12-07 às 11.04.40.png

Fields:

  • Start date | starts
  • End date | ends
  • Contract term | u_contract_term

Table: 

  • Contract | ast_contract

I don't even know where to start in scripting, I'm terrible at scripting.
I don't have anything yet.

Thank you so much ❤️

1 REPLY 1

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Lucas11 ,

You can try below code :- in BR

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

    var StartDate = new GlideDateTime(current.starts);
    if (current.contract_term == '1_year') { //write down proper field name and backend value for 1 year
        StartDate.addDaysUTC(365);
    } else if (current.contract_term == '2_year') { //write down proper field name and backend value for 2 year
        StartDate.addDaysUTC(730);
    }
    current.ends = StartDate;
    current.update();
})(current, previous);

  


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy