Automatically create a new issue when project stage is "at risk" and prevent project stage from changing until this issue is resolved

This script automatically create a new issue when the project stage is saved as "at risk" and prevents the project stage from changing until the issue is resolved.

Follow the steps below or download the solutions file, see Creating Solutions for details.

Note:

You will still need to create the custom fields described in Setup 1 — Custom Field

This example consists of a custom field and two scripts:

Important:

This example requires you to create a Project Stage. See the Administrator Guide for more details on Project Stages.

Setup 1 — Custom Field

  1. Set up a Checkbox and a Text Area custom field for Issue.

    Custom field list view showing the project at risk custom field.

Setup 2 — Project After Save

  1. Create a new Project form script deployment.

  2. Enter a Filename and click SAVE. The extension ‘.js’ is automatically appended if not supplied.

  3. Click on the script link to launch the Scripting Studio.

  4. (1) Copy the Program Listing below into the editor, (2) set the After save event, and set proj_at_risk_aftersave as the Entrance Function.

    Scripting Studio tools and settings panel showing the form association, the user event association and the selected entrance function.

Program Listing for Setup 2

          function proj_at_risk_aftersave() {
   var PROJECT_STAGE_AT_RISK = NSOA.context.getParameter('ProjectAtRiskStage');
   var ISSUE_STAGE_OPEN = NSOA.context.getParameter('IssueOpenStage');

    // return if new stage is changed and "at risk"
    var proj = NSOA.form.getNewRecord();
    var old_stage = NSOA.form.getOldRecord().project_stageid;
    var current_stage = proj.project_stageid;
    NSOA.meta.log("debug", "old=" + old_stage + ", new=" + current_stage);
    if (old_stage == current_stage || current_stage != PROJECT_STAGE_AT_RISK)
        return;

    // Check for an existing at-risk event
    var issue = new NSOA.record.oaIssue();
    issue.project_id = proj.id;
    issue.for_at_risk_project__c = '1';

    var readRequest = {
        type: "Issue",
        fields: "id, name, date",
        method: "equal to",
        objects: [issue],
        attributes: [{
            name: "limit",
            value: "1"
        }]
    };

    var arrayOfreadResult = NSOA.wsapi.read(readRequest);
    if (!arrayOfreadResult || !arrayOfreadResult[0])
        NSOA.form.error('', "Internal error analyzing project issues.");

    else if (arrayOfreadResult[0].errors === null &&
        (!arrayOfreadResult[0].objects || arrayOfreadResult[0].objects.length === 0)) {
        issue.owner_id = NSOA.wsapi.whoami().id;
        issue.description = "Projected reported at risk";
        issue.issue_status_id = 1; // Unassigned
        issue.issue_stage_id = ISSUE_STAGE_OPEN;
        issue.date = (new Date()).toISOString().slice(0, 10);
        NSOA.meta.log('debug', JSON.stringify(issue));
        NSOA.wsapi.add(issue);
    }
} 

        

Setup 3 — Project Before Save

  1. Create a new Project form script deployment.

  2. Enter a Filename and click SAVE. The extension ‘.js’ is automatically appended if not supplied.

  3. Click on the script link to launch the Scripting Studio.

  4. (1) Copy the Program Listing below into the editor, (2) set the Before save event, and set proj_at_risk_beforesave_validate as the Entrance Function.

    Scripting Studio tools and settings panel showing the form association, the user event association and the selected entrance function.

Program Listing for Setup 3

          function proj_at_risk_beforesave_validate() {
   var PROJECT_STAGE_AT_RISK = NSOA.context.getParameter('ProjectAtRiskStage');
   var ISSUE_STAGE_OPEN = NSOA.context.getParameter('IssueOpenStage');

    // return if new stage is not changing from "at risk"
    var current_stage = NSOA.form.getOldRecord().project_stageid;
    var new_stage = NSOA.form.getValue('project_stage_id');
    if (!(current_stage == PROJECT_STAGE_AT_RISK && new_stage != PROJECT_STAGE_AT_RISK))
        return;

    // Load issue data
    var issue = new NSOA.record.oaIssue();
    issue.project_id = NSOA.form.getValue('id');
    issue.issue_stage_id = ISSUE_STAGE_OPEN;
    issue.for_at_risk_project__c = '1';

    var readRequest = {
        type: "Issue",
        fields: "id, name, date",
        method: "equal to",
        objects: [issue],
        attributes: [{
            name: "limit",
            value: "1"
        }]
    };

    var arrayOfreadResult = NSOA.wsapi.read(readRequest);

    if (!arrayOfreadResult || !arrayOfreadResult[0])
        NSOA.form.error('', "Internal error analyzing project issues.");

    else if (arrayOfreadResult[0].errors === null && arrayOfreadResult[0].objects)
        arrayOfreadResult[0].objects.forEach(
            function(o) {
                NSOA.form.error('', "Can't change project stage until " +
                    "the following issue is resolved: " + o.name);
            }
        );
}