State not updating in UI Action

JennieD1
Tera Expert

Hi there! I have a business requirement for a form to have a button to change the state based on two conditions. 1. The form must have an attachment and 2. The form must have two mandatory questions answered. This application is in a scoped app. Thus far, I have created a UI Action and a script include. The logic I have works whenever I start a new record. My problem is, if I save a record and answer one of the conditions, but not the other, it is going ahead and updating the state. Can anyone help me with this? 

As you can see in the below image, my state has updated from open to work in progress, but only the attachment condition has been fulfilled. The 'Created & Assigned' and 'Test Fields' should still be mandatory and the state should still be open. 

JennieD1_0-1696254350552.png

UI Action

function changeState() {
     var ga = new GlideAjax("x_755950_disclos_0.getAttachmentsDisclosure");
     ga.addParam('sysparm_name', 'checkAttachment');
     ga.addParam('sysparm_sysID', g_form.getUniqueValue());
     ga.addParam('sysparm_tableName', g_form.getTableName());
     ga.getXML(validateForm);

     function validateForm(response) {
         var answer = response.responseXML.documentElement.getAttribute("answer");
         if (answer == "false") {
             g_form.addErrorMessage("You need to add an attachment");
             g_form.setMandatory('u_create_and_assigned', true);
             g_form.hideFieldMsg('u_create_and_assigned');
             g_form.showFieldMsg('u_create_and_assigned', "Please select an answer before changing the state.");
             g_form.setMandatory('test_field', true);
             g_form.hideFieldMsg('test_field');
             g_form.showFieldMsg('test_field', "Please select an answer before changing the state.");
         } else {
             g_form.setValue('state', 2);
             g_form.setMandatory('u_create_and_assigned', false);
             g_form.setMandatory('test_field', false);

         }

     }


     // gsftSubmit(null, g_form.getFormElement(), 'check_fields');
 }

Script Include:

var getAttachmentsDisclosure = Class.create();
getAttachmentsDisclosure.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

checkAttachment: function() {
    var sysID = this.getParameter('sysparm_sysID');
    var tableName = this.getParameter('sysparm_tableName');
    var attachment = new GlideSysAttachment();
    var agr = attachment.getAttachments(tableName, sysID);
    if (agr.next()) {
        return true;
    } else {
        return false;
    }
    },
    type: 'getAttachmentsDisclosure'

});

 

1 ACCEPTED SOLUTION

MackI
Kilo Sage

 

function changeState() {
    var attachmentConditionMet = false;
    var mandatoryFieldsConditionMet = false;

    // Check the attachment condition
    var ga = new GlideAjax("x_755950_disclos_0.getAttachmentsDisclosure");
    ga.addParam('sysparm_name', 'checkAttachment');
    ga.addParam('sysparm_sysID', g_form.getUniqueValue());
    ga.addParam('sysparm_tableName', g_form.getTableName());
    ga.getXML(function (response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == "false") {
            g_form.addErrorMessage("You need to add an attachment");
        } else {
            attachmentConditionMet = true;
            checkAndUpdateState();
        }
    });

    // Check the mandatory fields condition
    var uCreateAndAssignedValue = g_form.getValue('u_create_and_assigned');
    var testFieldValue = g_form.getValue('test_field');
    
    if (!uCreateAndAssignedValue || !testFieldValue) {
        g_form.addErrorMessage("Please answer both mandatory questions");
    } else {
        mandatoryFieldsConditionMet = true;
        checkAndUpdateState();
    }

    function checkAndUpdateState() {
        if (attachmentConditionMet && mandatoryFieldsConditionMet) {
            // Both conditions are met, change the state
            g_form.setValue('state', 2);
            g_form.setMandatory('u_create_and_assigned', false);
            g_form.setMandatory('test_field', false);
        } else {
            // At least one condition is not met, do not change the state
            g_form.setValue('state', 1); // Set the state back to 'Open' if needed
            g_form.setMandatory('u_create_and_assigned', true);
            g_form.setMandatory('test_field', true);
        }
    }
}

 

Let me know if the above things works ...

MackI | ServiceNow Developer | 2 *Mainline Certification | LinkedIn Top IT Operation Voice 2023 | Sydney,Australia

View solution in original post

28 REPLIES 28

can u try placing an alert in ur UI code to check wht u receive from script include.

 

Thanks,

Danish

MackI
Kilo Sage

 

function changeState() {
    var attachmentConditionMet = false;
    var mandatoryFieldsConditionMet = false;

    // Check the attachment condition
    var ga = new GlideAjax("x_755950_disclos_0.getAttachmentsDisclosure");
    ga.addParam('sysparm_name', 'checkAttachment');
    ga.addParam('sysparm_sysID', g_form.getUniqueValue());
    ga.addParam('sysparm_tableName', g_form.getTableName());
    ga.getXML(function (response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == "false") {
            g_form.addErrorMessage("You need to add an attachment");
        } else {
            attachmentConditionMet = true;
            checkAndUpdateState();
        }
    });

    // Check the mandatory fields condition
    var uCreateAndAssignedValue = g_form.getValue('u_create_and_assigned');
    var testFieldValue = g_form.getValue('test_field');
    
    if (!uCreateAndAssignedValue || !testFieldValue) {
        g_form.addErrorMessage("Please answer both mandatory questions");
    } else {
        mandatoryFieldsConditionMet = true;
        checkAndUpdateState();
    }

    function checkAndUpdateState() {
        if (attachmentConditionMet && mandatoryFieldsConditionMet) {
            // Both conditions are met, change the state
            g_form.setValue('state', 2);
            g_form.setMandatory('u_create_and_assigned', false);
            g_form.setMandatory('test_field', false);
        } else {
            // At least one condition is not met, do not change the state
            g_form.setValue('state', 1); // Set the state back to 'Open' if needed
            g_form.setMandatory('u_create_and_assigned', true);
            g_form.setMandatory('test_field', true);
        }
    }
}

 

Let me know if the above things works ...

MackI | ServiceNow Developer | 2 *Mainline Certification | LinkedIn Top IT Operation Voice 2023 | Sydney,Australia

This is working, but it is not updating the form as the UI action button is still there. 

JennieD1_0-1696269742318.png

 

Okay! Progress, I got it to update the state, but how do I get it to redirect to the updated form after I hit the ui action button?

I was able to get the UI Action to work thanks to your code!!! I made a couple of changes. I added  g_form.save(); in the if statement of the checkandUpdateState() function and I also called the checkandupdatestate() function in the first if statement of the Check Mandatory Fields condition  in order to make the two fields mandatory.

 

Final Code with changes below!

 

function changeState() {
    var attachmentConditionMet = false;
    var mandatoryFieldsConditionMet = false;

    // Check the attachment condition
    var ga = new GlideAjax("x_755950_disclos_0.getAttachmentsDisclosure");
    ga.addParam('sysparm_name', 'checkAttachment');
    ga.addParam('sysparm_sysID', g_form.getUniqueValue());
    ga.addParam('sysparm_tableName', g_form.getTableName());
    ga.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == "false") {
            g_form.addErrorMessage("You need to add an attachment");
        } else {
            attachmentConditionMet = true;
            checkAndUpdateState();
        }
    });

    // Check the mandatory fields condition
    var uCreateAndAssignedValue = g_form.getValue('u_create_and_assigned');
    var testFieldValue = g_form.getValue('test_field');

    if (!uCreateAndAssignedValue || !testFieldValue) {
        g_form.addErrorMessage("Please answer both mandatory questions");
        checkAndUpdateState();
    } else {
        mandatoryFieldsConditionMet = true;
        checkAndUpdateState();
    }

    function checkAndUpdateState() {
        if (attachmentConditionMet && mandatoryFieldsConditionMet) {
            // Both conditions are met, change the state
            g_form.setValue('state', 2);
            g_form.setMandatory('u_create_and_assigned', false);
            g_form.setMandatory('test_field', false);
            g_form.save();


        } else {
            // At least one condition is not met, do not change the state
            g_form.setValue('state', 1); // Set the state back to 'Open' if needed
            g_form.setMandatory('u_create_and_assigned', true);
            g_form.setMandatory('test_field', true);
        }

    }

}