- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 07-17-2020 07:44 AM
We recently had a requirement come through asking us to bypass the current behavior for the Actual Start and Actual End Dates on the project task form. Today, these dates are auto populated via scripts that will grab the Planned Start Date and Planned End Date and populate these date fields accordingly. Our requirement was to populate the Actual Start/Actual End with today's date and time. Here is the reasoning why ServiceNow uses the Planned Start/Planned End Dates:
https://hi.service-now.com/kb_view.do?sysparm_article=KB0780931
We did not want to deactivate any current scripts for these fields so we had to come up with a way around this.
We ended up creating 2 clients script, 1 script include (borrowed this from MB), and 2 business rules and updating 1 Project property.
The client scripts are for the details view of the project task, the script include is from a handy script that BM created to calculate dates, the 2 business rules are for the planning console, and the property activates business rules on the planning console.
Here are the two client scripts, they affect the details view of the project task view:
Name: Set Start Date
Table: Project Task (pm_project_task)
UI Type: All
Type: onChange
Field name: State
Order: 10000
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate){
if (isLoading || newValue === '') {
return;
}
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDateTime');
if(newValue=='2'){ //state == Work In Progress
ajax.getXML(doSomething);
}
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('work_start',answer);
}
}
Name: Set End Date
Table: Project Task (pm_project_task)
UI Type: All
Type: onChange
Field name: State
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate){
if (isLoading || newValue === '') {
return;
}
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDateTime');
if(newValue=='3'){ //state == Closed Complete
ajax.getXML(doSomething);
}
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('work_end',answer);
}
}
Set the order of this script to 500: Set State, End Date on Actual Start Chang
The AJAX script referenced above is a handy script from MB: https://community.servicenow.com/community?id=community_question&sys_id=38c40be9dbd8dbc01dcaf3231f96...
The two business rules look like this, they affect the planning console:
Name: Update Actual Start Date with Now Date
Table: Project Task (pm_project_task)
Active: true
Advanced: true
When to run:
When: before
Order: 7000
Update: true
Filter: State changes to Work In Progress
Script:
(function executeRule(current, previous){
var now = new GlideDateTime();
current.work_start= now;
})(current,previous);
Name: Update Actual End Date with Now Date
Table: Project Task (pm_project_task)
Active: true
Advanced: true
When to run:
When: before
Order: 7000
Update: true
Filter: State changes to Closed Complete
Script:
(function executeRule(current, previous){
var now = new GlideDateTime();
current.work_end= now;
})(current,previous);
Last but not least, navigate to Project Administration>Settings>Preferences-Project
Check the box next to the property 'Enable firing of Business Rules on save from Planning Console' and Save your changes.
This will auto-save any changes made in the planning console, thus firing off the business rules above.
That's it! Hopefully this helps others and hopefully we are not breaking any other OOTB functionality. If you have any problems or recommendations, feel free to respond.
- 2,814 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
- Ensure Client callable is checked.
- Ensure Order = 500 is set for client script on the pm_project_task table.
