Setting Due Date Field

jcsmith
Kilo Contributor

I am attempting to set the due_date field on the catalog requested item form to pull from a user created field (u_planned_completion_date) on the sc_task table.   Preferably, if the item has multiple tasks, it would use the last Planned Completion Date available.   For some background, I'm doing this to override the auto-calculated "Expected Delivery Date of Completed Order" that the user sees when a request is submitted.   I would like this to be blank until the work is planned and the Planned Completion Date field is filled.

I, honestly, don't even know where to start on this one.   I'm not technically inclined, as I usually work on front-end, user experience with SNOW.   Any and all help would be appreciated.

The closes thing I can think of would be something like:

task.due_date = current.variables.u_planned_completion_date;

But, I'm fairly certain that's not even close.   I'm thinking it's going to have to be a more complicated client script.

Thanks!

16 REPLIES 16

Chuck Tomasi
Tera Patron

Hi Justin,



You can do this with a before business rule to automatically trigger or you can use a UI action to manually trigger it.



The key is to find all child requests and find the greatest value for the u_planned_completion_date field. Something like this:



Standard disclaimer: The following code is untested, requires review and potential modifications (specifically field names)



// Assumption: This is running on the sc_req_item table



(function () {  



        var max = new GlideDateTime('1970-01-01 00:00:00');


      var rec = new GlideRecord('sc_task');  


      rec.addQuery('request_item', current.request_item);


      rec.query();  


 


      while (rec.next()) {


                  if (rec.u_planned_completion_date > max)


                            max = rec.u_planned_completion_date;


      }


      current.u_planned_completion_date = max;


})();  


I'll give that a try and let you know how that works, Chuck.   Thank you!


kristenankeny
Tera Guru

Hi Justin,



You mention that the field you want to reference is on the sc_task, but then the script you provided seems to indicate that the field is actually a variable on the catalog item (which then populates in the variables on the requested item and catalog task. I'm going to work from the assumption that you have a custom field on the sc_task table that staff is updating and that you want that to trickle up to the requested item.



You'll want to create a business rule on the sc_task table. Make it a "before" rule and on "update".


Add a condition that u_planned_completion_date changes


Check off "advanced" so you can do some scripting


In the script try something like this:


var t = new GlideRecord('sc_task');


t.addQuery('request_item',current.request_item);


t.query();


var date = '';


while(t.next()){


if(date == ''){


date = t.u_planned_completion_date;


}


else if(t.u_planned_completion_date > date){


date = t.u_planned_completion_date;


}


}


var r = new GlideRecord('sc_req_item');


r.addQuery('sys_id',current.request_item);


r.query();


if(r.next()){


r.due_date = date;


}


Kristen,


Thanks for the help!   I tried adding that business rule and it was unsuccessful.   It seems to do a strange thing to the due date on the task record, and did not change the due date on the requested item record.   I'm wondering if the date references in the middle of the script should be due_date...but, again, I know nothing about this stuff.   I'm just trying to get stuff done while our scripting person is out for a few weeks.