The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Set a date field on project task to a dynamic value using a template.

Chandler2
Tera Guru

So, I am creating project and project tasks using a template. There I want to select the date of the project task to few days ahead of the project start date. The date field option is a simple calendar selection. How can I make it dynamic like set the value to 10 days from now?

 

Chandler2_0-1667911774519.png

 

 

Thanks

9 REPLIES 9

@Theo BOULANGER  I think my question was little unclear.

There is a template to create project which create and project tasks. Now while creating project, we have to select a start date. Now If I select start date as 01-11-2022 then all the project tasks also get created with same start date.

My requirement is that one of the project task (based on condition like, name is xyz) should have start date which is 1 month ahead. For eg: I choose 17-11- 2022 in the template so that particular task xyz should have a date 17-12-2022. 

 

Now below is the BR that i used on after insert BR and it works but I want to use a script include as you suggested. Just thinking on how will I get the Top Project info in script include.

 

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

// Add your code here
var currentTime = new GlideDateTime(current.start_date);
currentTime.addMonthsUTC(1);

var gr = new GlideRecord("pm_project_task");
gr.addQuery("top_task", current.sys_id);
gr.addEncodedQuery('short_description=xyz');
gr.query();
if (gr.next()) {
gr.start_date = currentTime;
}
gr.update();

})(current, previous);

If it is working with BR then I don't think you need to have script inlcude.

 

However, if you still need it then you can pass current object in script include as below,

new ScriptInlcludeName().functionName(current);

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

@Abhijit4  Can you please elaborate. 

 I can put below code in the function in script include but how I will get the start date from the corresponding parent project record?

var gr = new GlideRecord("pm_project_task");
gr.addQuery("top_task", current.sys_id);
gr.addEncodedQuery('short_description=xyz');
gr.query();
if (gr.next()) {
gr.start_date = currentTime;
}
gr.update();
},

You need to call script include function via BR as shown below :

BR script :

new global.SetStartDate().SetStartDate(current);

 

Script Include :

Abhijit4_0-1668770062641.png

Please mark answer as Correct or Helpful based on impact.

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

@Chandler2 

 

Script include :

 

getDateMonthN1: function() {
 
var query = 'yourquery';

var rec = new GlideRecord('pm_project_task');
rec.addEncodedQuery(query);
rec.query();
if(rec.next()){
    var currentTime = new GlideDateTime(rec.start_date)
    currentTime.addMonthsUTC(1);
}

return currentTime
 
}
You can keep your BR as you did, if you want to use this script Include above, this is the base you need but you'll need to put some parameter in this fonction.
 
Regards,