Need a complete button on Project only visible state in wip to PM's?

Deepika61
Tera Contributor

Hi All,

Actually I have requirement that need "complete "button on Project , that visible only to Project Managers when state is "WIP" and Should only allow to complete the customer project, by clicking on the button, when below conditions are met

1 under related list Project Tasks, where top task is the current project state is either Closed Complete/Incomplete

2 Related stories state  are in either complete/cancelled

3  Epics where parent or top task is current project is complete/cancelled

4 Related Sprints where parent or top task is current project  is complete/cancelled

5 Current Date/time is on or before Planned end date of project.

 

if all the above conditions are met then PM can able to submit complete button

 

Please help me to achieve this

Thanks

Deepika

1 ACCEPTED SOLUTION

Mark Manders
Mega Patron

Validate all fields, tables, states, etc. This script should give you a start.

(function executeUIAction(current) {
    // Initialize an array to hold messages for unmet conditions
    var unmetConditions = [];

    // Function to check related tasks
    function checkRelatedTasks(table, field, validStates) {
        var gr = new GlideRecord(table);
        gr.addQuery(field, current.sys_id);
        gr.query();
        while (gr.next()) {
            if (!validStates.includes(gr.state.toString())) {
                return false;
            }
        }
        return true;
    }

    // Check related project tasks
    if (!checkRelatedTasks('task', 'parent', ['closed_complete', 'closed_incomplete'])) {
        unmetConditions.push("Not all related project tasks are closed.");
    }

    // Check related stories
    if (!checkRelatedTasks('story', 'parent', ['closed_complete', 'closed_incomplete'])) {
        unmetConditions.push("Not all related stories are closed.");
    }

    // Check related epics
    if (!checkRelatedTasks('epic', 'top_task', ['complete', 'canceled'])) {
        unmetConditions.push("Not all related epics are complete or canceled.");
    }

    // Check related sprints
    if (!checkRelatedTasks('sprint', 'top_task', ['complete', 'canceled'])) {
        unmetConditions.push("Not all related sprints are complete or canceled.");
    }

    // Check project end date
    var currentDate = new GlideDateTime();
    var plannedEndDate = new GlideDateTime(current.planned_end_date);
    if (currentDate.after(plannedEndDate)) {
        unmetConditions.push("The current date/time is after the planned end date of the project.");
    }

    // Construct and display the error message if any conditions are unmet
    if (unmetConditions.length > 0) {
        var errorMessage = "The project cannot be completed due to the following reasons:\n" + unmetConditions.join("\n");
        gs.addErrorMessage(errorMessage);
        return false; // Prevent the UI Action from proceeding
    }

    // If all conditions are met, mark the project as complete
    current.state = 'complete'; // Adjust the 'state' field and value as per your instance
    current.update();
    gs.addInfoMessage("Project marked as complete.");
})(current);

 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

View solution in original post

4 REPLIES 4

Pratik Malviya
Tera Guru

Hi @Deepika61 ,

Please check below to achieve your requirement.
https://www.servicenow.com/community/itsm-forum/creat-an-ui-action-button-that-should-check-all-the-...

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Thanks,
Pratik Malviya

Mark Manders
Mega Patron

Do you only want the button to show it it can be clicked, or can it show in WIP state and just give a message when one of the conditions isn't true? Because the second thing can be done easy (script inside your ui action), for just showing the button on all of those conditions, you will need a script include, validating this and set your conditions on the UI action to only show if it returns 'true'.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

@Mark Manders 

Thanks for the response

First thing it will visible  on the form to PM's only when state is Work in Progress

Second thing the PM should allow to click that button if all mentioned conditions are met

 

Can you please provide me script based on this criteria?

 

Thanks

Deepika

Mark Manders
Mega Patron

Validate all fields, tables, states, etc. This script should give you a start.

(function executeUIAction(current) {
    // Initialize an array to hold messages for unmet conditions
    var unmetConditions = [];

    // Function to check related tasks
    function checkRelatedTasks(table, field, validStates) {
        var gr = new GlideRecord(table);
        gr.addQuery(field, current.sys_id);
        gr.query();
        while (gr.next()) {
            if (!validStates.includes(gr.state.toString())) {
                return false;
            }
        }
        return true;
    }

    // Check related project tasks
    if (!checkRelatedTasks('task', 'parent', ['closed_complete', 'closed_incomplete'])) {
        unmetConditions.push("Not all related project tasks are closed.");
    }

    // Check related stories
    if (!checkRelatedTasks('story', 'parent', ['closed_complete', 'closed_incomplete'])) {
        unmetConditions.push("Not all related stories are closed.");
    }

    // Check related epics
    if (!checkRelatedTasks('epic', 'top_task', ['complete', 'canceled'])) {
        unmetConditions.push("Not all related epics are complete or canceled.");
    }

    // Check related sprints
    if (!checkRelatedTasks('sprint', 'top_task', ['complete', 'canceled'])) {
        unmetConditions.push("Not all related sprints are complete or canceled.");
    }

    // Check project end date
    var currentDate = new GlideDateTime();
    var plannedEndDate = new GlideDateTime(current.planned_end_date);
    if (currentDate.after(plannedEndDate)) {
        unmetConditions.push("The current date/time is after the planned end date of the project.");
    }

    // Construct and display the error message if any conditions are unmet
    if (unmetConditions.length > 0) {
        var errorMessage = "The project cannot be completed due to the following reasons:\n" + unmetConditions.join("\n");
        gs.addErrorMessage(errorMessage);
        return false; // Prevent the UI Action from proceeding
    }

    // If all conditions are met, mark the project as complete
    current.state = 'complete'; // Adjust the 'state' field and value as per your instance
    current.update();
    gs.addInfoMessage("Project marked as complete.");
})(current);

 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark