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

@devarshkhed 

so basically filtering is working fine and you want to set value on Priority choice in modal rather than asking user to select this?

is that what you are asking?

I doubt there is a way to set value in g_modal field

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

so basically filtering is working fine and you want to set value on Priority choice in modal rather than asking user to select this? - : on modal it should set on the basis of impact and urgency

Ankur Bawiskar
Tera Patron
Tera Patron

@devarshkhed 

check this link and see response from simonezini, it might help

Filter a reference field in g modal in Workspace 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

devarshkhed
Tera Contributor

Not Working