Update program budget automatically

Prakash_S
Tera Contributor

I have a requirement to auto update the program budget based on project associated to it and notify to program manager is there is any change in budget.

1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

Hi @Prakash_S ,

 

You can follow below steps.

1. create a notification on program table and trigger it on update and insert.

2. Add condition that budget changes.

RunjayPatel_0-1735575714699.png

RunjayPatel_1-1735575777209.png

 

3. Create a after business rule on project table and use below script.

(function executeRule(current, previous /*null when async*/) {
    // Ensure the project is linked to a program
    if (!current.primary_program) {
        return;
    }

    var programID = current.primary_program;
    var totalBudget = 0;

    // Query all projects linked to the program
    var projectGR = new GlideRecord('pm_project');
    projectGR.addQuery('primary_program', programID);
    projectGR.query();

    while (projectGR.next()) {
        
        totalBudget += parseFloat(projectGR.getValue('budget_cost') || 0);
    }

    // Update the program's total budget if it has changed
    var programGR = new GlideRecord('pm_program');
    if (programGR.get(programID)) {
        var previousBudget = parseFloat(programGR.getValue('budget_cost') || 0);

        if (previousBudget !== totalBudget) {
            programGR.setValue('budget_cost', totalBudget);
            programGR.update();
            
                    }
    }
})(current, previous);

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

 

 

 

 

View solution in original post

1 REPLY 1

Runjay Patel
Giga Sage

Hi @Prakash_S ,

 

You can follow below steps.

1. create a notification on program table and trigger it on update and insert.

2. Add condition that budget changes.

RunjayPatel_0-1735575714699.png

RunjayPatel_1-1735575777209.png

 

3. Create a after business rule on project table and use below script.

(function executeRule(current, previous /*null when async*/) {
    // Ensure the project is linked to a program
    if (!current.primary_program) {
        return;
    }

    var programID = current.primary_program;
    var totalBudget = 0;

    // Query all projects linked to the program
    var projectGR = new GlideRecord('pm_project');
    projectGR.addQuery('primary_program', programID);
    projectGR.query();

    while (projectGR.next()) {
        
        totalBudget += parseFloat(projectGR.getValue('budget_cost') || 0);
    }

    // Update the program's total budget if it has changed
    var programGR = new GlideRecord('pm_program');
    if (programGR.get(programID)) {
        var previousBudget = parseFloat(programGR.getValue('budget_cost') || 0);

        if (previousBudget !== totalBudget) {
            programGR.setValue('budget_cost', totalBudget);
            programGR.update();
            
                    }
    }
})(current, previous);

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------