How do I get my custom Project Task fields to populate with the Predecessor and Successor tasks

JR42
Giga Guru

Hello!  I need to display the Predecessor and Successor (from Dependencies) on each Project Task.

The idea is that the person looking at the Project Task will be able to easily see if that task has predecessors or successors.

 

I've created two new reference fields, 'Successor Task' (u_successor_task), and 'Predecessor Task' (u_predecessor_task). 

Then I created a Business Rule to run After Insert, Update or Delete.  It uses the following script, which I do not think is correctly finding the Predecessor and Successors from the Dependencies, and definitely isn't population my new fields.

(function executeRule(current, previous /*null when 'insert' or 'delete'*/) {
    // Get the successor (current task) sys_id
    var successorSysId = current.child.toString();
    // Get the predecessor (dependent task) sys_id
    var predecessorSysId = current.parent.toString();

    // Update the Successor's 'Predecessor Task' field
    var successor = new GlideRecord('pm_project_task');
    if (successor.get(successorSysId)) {
        successor.setValue('u_predecessor_task', predecessorSysId); 
        successor.update();
    }

    // Update the Predecessor's 'Successor Task' field, if applicable
    var predecessor = new GlideRecord('pm_project_task');
    if (predecessor.get(predecessorSysId)) {
        predecessor.setValue('u_successor_task', successorSysId); 
        predecessor.update();
    }
})();

 

Does anyone have a solution for this?

1 ACCEPTED SOLUTION

JR42
Giga Guru

I decided to go with a related list instead.

View solution in original post

5 REPLIES 5

AshishKM
Kilo Patron
Kilo Patron

Hi @JR42 , 

Trying to get the logic, so new Project Task is created, it has both the field already filled ( child & parent ). If project task already has the child / parent, then we don't need to Glide again because we can perform dot walking via current object.

And this BR running on pm_project_task table.

 

Also the last line has missing current & previous object.

 

(function executeRule(current, previous /*null when 'insert' or 'delete'*/) {
    // Get the successor (current task) sys_id
    var successorSysId = current.child.toString();
    // Get the predecessor (dependent task) sys_id
    var predecessorSysId = current.parent.toString();

    // Update the Successor's 'Predecessor Task' field
    var successor = new GlideRecord('pm_project_task');
    if (successor.get(successorSysId)) {
        successor.setValue('u_predecessor_task', predecessorSysId); 
        successor.update();
    }

    // Update the Predecessor's 'Successor Task' field, if applicable
    var predecessor = new GlideRecord('pm_project_task');
    if (predecessor.get(predecessorSysId)) {
        predecessor.setValue('u_successor_task', successorSysId); 
        predecessor.update();
    }
})(current, previous);

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Hi Ashish, it is not the child/parent relationship.  All of the Project Tasks have a single parent, that being the Project itself.

I am looking to represent the Predecessor and Successor from the Dependencies created on the Planning Console. If I open a dependency from the planning console, it shows the Predecessor and Successor.  I want to use those values to populate my custom fields in the Project Tasks.

 

2024-02-16 12_56_15-Planning — test _ ServiceNow [DEV] Denovo - DEV INSTANCE.png

For example, if I have three Project Tasks, 001, 002, and 003.  PRJTASK002 has a dependency that says it can't be started until PRJTASK001 is completed, and PRJTASK003 has a Dependency saying it can't start until PRJTASK002 is complete.  This make PRJTASK001 the Predecessor of 002, and PRJTASK003 the Successor of 002.  So if I look at PRJTASK002, I want my 'Predecessor Task' field to have a value of 'PRJTASK001' and my 'Successor Task' field to have a value of 'PRJTASK003'.

I hope this clarifies what I am after.

 

I looked closer.  On the Dependency form (planned_task_rel_planned_task) in my last screenshot, the field shown as Predecessor is actually 'parent' and the Successor is actually 'child'.  But that relationship is only on the planned_task_rel_planned_task table, not on the project tasks table.

JR42
Giga Guru

I decided to go with a related list instead.