Client Script to set form due_date based on "start_date" variable

Bcrant
Tera Contributor

Afternoon.

 

Hoping to get some guidance on a client script to set due_date on a task from to 3 days prior to a variable date input on employee start date. Any assistance is appreciated as my initial attempts with onLoad client script haven't produced desired result.

 

Thanks!

Bcrant_0-1675379334543.png

 

1 ACCEPTED SOLUTION

Yes before will be good one.

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

6 REPLIES 6

-O-
Kilo Patron
Kilo Patron

If this is a Catalog Item, you should do this in the Flow or Workflow that runs for it.

If this is a Record Producer, you should do this in an insert before business rule.

It is easier to do it server side, plus it is not nice to modify values onLoad - it will be registered as a change and the user will get a data loss prompt even though no modification have been made - confusing.

Pavankumar_1
Mega Patron

Hi @Bcrant ,

Use before update/insert  business rule on the sc_req_item table and give specific condition when it needs to update.

Use below script on business rule

 

 

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

    var startdate = current.variables.start_date;

    var dt = new GlideTime();
    var time = dt.getDisplayValue().split(' ')[1];//get time
    var finaldatetime = startdate + ' ' + time; //it will give start date and time

    var gdt = new GlideDateTime(finaldatetime); //using finaldatetime to subtract 3 days from start date
    gdt.addDays(-3); //substract 3 days
    current.due_date = gdt;//update on due date field

})(current, previous);

 

 

Screenshot (632).png

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

current.update() - really?

Hi @-O- ,

I know it is not recommended but we can these lines when we can't avoid current.update()

current.setWorkflow(false);
current.update();
current.setWorkflow(true); 

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar