UI Action creating new records

Tom Lapkowicz
Tera Expert

I am working on creating a UI Action form button

 

This action should first look at 8 different fields and make sure they are all populated.  If any of them are not populated then it should display a message saying "All fields must be completed" and take no further action.

 

If all 8 fields are populated then there is some if/then logic to update a field and display a message.  Once it updates the field and displays the message then no further action should be taken.

 

In both cases (if one or more fields isn't populated OR if all 8 fields are populated) it is acting as expected except that it then tries to create a new record.  I don't want it to create a new record.  How can I get this to stop?

 

The UI Action is set up as follows:

Table: dmn_demand

Action Name evaluate_risk_score

Active: checked

Show update: checked

Client: checked

List v2 Compatible: checked

Form button: checked

Onclick: onClick();

Condition: current.active == true && gs.hasRole("itil")

Script:

 

function onClick(event) {
    if (event) {
        event.preventDefault(); // Stops the default form submission
    }

    var requiredFields = [
        'u_is_this_a_new_product_service_or_new_process',
        'u_does_this_project_have_significant_operational_impacts',
        'u_does_this_require_technology_operations_resources',
        'u_does_this_project_span_across_multiple_lines_of_business',
        'u_does_this_project_have_legal_compliance_and_or_fraud_risk',
        'u_is_this_project_the_result_of_an_identified_issue_issue_on_the_issue_tracker',
        'u_does_this_project_have_an_impact_on_the_membership',
        'u_does_this_project_involve_a_third_party_relationship_i_e_vendor'
    ];

    // Check if any required field is "None" or empty
    for (var i = 0; i < requiredFields.length; i++) {
        if (g_form.getValue(requiredFields[i]) === "None" || g_form.getValue(requiredFields[i]) === "") {
            showDialog("Missing Information", "All fields must be completed before Evaluation.");
            return false; // Stop execution
        }
    }

    // Calculate Total Risk Score
    var totalRiskScore = 0;
    requiredFields.forEach(function(field) {
        if (g_form.getValue(field) === "Yes") {
            totalRiskScore += 1;
        }
    });

    // Update the total risk score
    g_form.setValue('u_total_risk_score', totalRiskScore);

    // Get the value of "Is this a new product/service/process?"
    var isNewProductServiceOrProcess = g_form.getValue('u_is_this_a_new_product_service_or_new_process');

    // Determine Risk Score Result
    var riskScoreResult = "";
    if (isNewProductServiceOrProcess === "Yes" || totalRiskScore >= 5) {
        riskScoreResult = "High Risk: Risk Assessment must be conducted";
    } else if (isNewProductServiceOrProcess === "No" && totalRiskScore === 4) {
        riskScoreResult = "Medium Risk: Risk assessment is optional.";
    } else if (isNewProductServiceOrProcess === "No" && totalRiskScore <= 3) {
        riskScoreResult = "Low Risk: Risk assessment does not need to be conducted";
    }

    // Update the Risk Score Result field
    g_form.setValue('u_risk_score_result', riskScoreResult);

    // Display final message in a dialog box
    showDialog("Evaluation Complete", "Evaluation Complete: " + riskScoreResult);

    return false; // Ensures no record is created
}

// Function to show a modal dialog
function showDialog(title, message) {
    var dialog = new GlideModal("custom_dialog");
    dialog.setTitle(title);
    dialog.setBody("<p style='font-size:14px;'>" + message + "</p>", false, false);
    dialog.addCloseButton("OK");
    dialog.render();
}

 

 

2 REPLIES 2

Shivalika
Mega Sage

Hello @Tom Lapkowicz 

 

What's event and where is showing Dialog function called and prevent default function written ? 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

Robert H
Mega Sage

Hello @Tom Lapkowicz ,

 

I have created a UI Action with the exact same configuration as the one mentioned in your post, and pasted your script. I only replaced the field names because I don't have all these custom fields on my end.

 

Then I clicked this action on a Demand where some fields were empty and also on a Demand where everything was filled. In both cases the dialog pops up with either the missing information warning or the completion confirmation. When I closed it it just closed as expected and I was back on the Demand form - nothing else happened, no submit, no "new record", etc. And looking at the script I would also not expect anything else to happen.

 

So something else must be the cause of the issue. Can you please try deactivating this UI Action and creating a new one with the same script, but a different (and unique) "Action name"?

 

PS: the first three lines (with the "event") and the last "return" are not necessary.

 

PPS: Are you familiar with the OOTB assessment surveys that get assigned to the Stakeholders when a Demand gets moved to Screening? The default survey also has questions related to Risks, see below. Based on the answers the Demand scores, including risk, get calculated using configurable weightings. It seems you are trying to achieve something similar, so I thought of mentioning it.

RobertH_0-1743709382744.png

 

 

Regards,

Robert