Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

When Impact and urgency changes Priority should change on modal

devarshkhed
Tera Contributor
When i am changing the impact and urgency priority is not updating.
 
function onClick(g_form) {

    var priorityMap = {
        1: "1 - Critical",
        2: "2 - High",
        3: "3 - Moderate",
        4: "4 - Low"
    };

    function calculatePriority(impact, urgency) {
        impact = parseInt(impact, 10);
        urgency = parseInt(urgency, 10);

        if (impact === 1 && urgency === 1) return 1;
        if ((impact === 1 && urgency === 2) || (impact === 2 && urgency === 1)) return 2;
        if ((impact === 1 && urgency === 3) ||
            (impact === 2 && urgency === 2) ||
            (impact === 3 && urgency <= 2)) return 3;

        return 4;
    }

    function getFieldValueSafe(list, name, fallback) {
        if (!list) return fallback;
        for (var i = 0; i < list.length; i++) {
            if (list[i].name === name) return list[i].value;
        }
        return fallback;
    }

    var fields = [{
            type: 'choice',
            name: 'impact',
            label: 'Impact',
            value: g_form.getValue('impact'),
            choices: [{
                    displayValue: 'Extensive/Widespread',
                    value: '1'
                },
                {
                    displayValue: 'Significant/Large',
                    value: '2'
                },
                {
                    displayValue: 'Moderate/Limited',
                    value: '3'
                },
                {
                    displayValue: 'Minor/Localized',
                    value: '4'
                }
            ],
            mandatory: true
        },
        {
            type: 'choice',
            name: 'urgency',
            label: 'Urgency',
            value: g_form.getValue('urgency'),
            choices: [{
                    displayValue: 'High',
                    value: '1'
                },
                {
                    displayValue: 'Medium',
                    value: '2'
                },
                {
                    displayValue: 'Low',
                    value: '3'
                }
            ],
            mandatory: true
        },
        {
            type: 'string',
            name: 'priority_display',
            label: 'Priority',
            value: priorityMap[calculatePriority(g_form.getValue('impact'), g_form.getValue('urgency'))],
            readonly: true
        },
        {
            type: 'textarea',
            name: 'u_change_priority_reason',
            label: 'Reason for Priority Change',
            mandatory: true
        }
    ];

    g_modal.showFields({
        title: "Change Priority",
        fields: fields,
        size: "md",
        variant: "dialog"
    }).then(function(fieldValues) {

        var impact = getFieldValueSafe(fieldValues.updatedFields, "impact", g_form.getValue("impact"));
        var urgency = getFieldValueSafe(fieldValues.updatedFields, "urgency", g_form.getValue("urgency"));
        var reason = getFieldValueSafe(fieldValues.updatedFields, "u_change_priority_reason", "");

        var newPriority = calculatePriority(impact, urgency);

        g_modal.confirm("Confirm Update", "Do you want to update the case priority?", function(confirmed) {
            if (!confirmed) return;

            var updated = {
                impact: impact,
                urgency: urgency,
                priority: newPriority,
                priority_display: priorityMap[newPriority]
            };

            var ga = new GlideAjax("CaseUtils");
            ga.addParam("sysparm_name", "updateCasePriority");
            ga.addParam("sysparm_sys_id", g_form.getUniqueValue());
            ga.addParam("sysparm_fields", JSON.stringify(updated));
            ga.addParam("sysparm_reason", reason);

            ga.getXMLAnswer(function(answer) {
                if (answer === "success") {
                    g_form.refresh();
                } else {
                    g_modal.alert("Update failed: " + answer);
                }
            });
        });
    });
}


8 REPLIES 8

Dr Atul G- LNG
Tera Patron
Tera Patron

It’s OOTB for Incident and Problem, but not for Change. I don’t think it’s applicable for Change anyway, because Change priority doesn’t mainly depend on urgency — it’s driven more by risk and impact.

If you still want to implement it, please use the Priority Lookup.

 

https://youtu.be/zusa_YSlEvk

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************
CISITSM #ITSM #csa #ServiceNow #TechnoFuncational Disclaimer: These videos are from my training batch. These videos did not promote any ServiceNow Sales pitch or marketing. These videos are only for knowledge purposes & basic on my experience & Knowledge. Redistribution or copying of functionality

I am implementing this on the CSM Workspace using a Workspace client script. The modal displays four fields: Impact, Urgency, Priority, and Reason for priority change. When I change the Impact or Urgency, I expect the Priority to update automatically. However, in my current implementation, the Priority only updates after clicking OK and then reflects directly on the form. I want the Priority to update dynamically on the modal itself whenever Impact or Urgency changes.

Ankur Bawiskar
Tera Patron
Tera Patron

@devarshkhed 

what's your business requirement?

are you saying within the modal in workspace the dependency should work?

what debugging did you do from your side in above script?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I am implementing this on the CSM Workspace using a Workspace client script. The modal displays four fields: ImpactUrgencyPriority, and Reason for priority change. When I change the Impact or Urgency, I expect the Priority to update automatically. However, in my current implementation, the Priority only updates after clicking OK and then reflects directly on the form. I want the Priority to update dynamically on the modal itself whenever Impact or Urgency changes.

 

Everything is working fine except populating the Priority based on Impact and Urgency in the modal. It updates correctly on the form, but not on the modal when I change Impact or Urgency.