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.

Check Conflict to run with Request Approval (move to Assess) UI Action

beccintech
Tera Contributor

I am trying to get the Check Conflict UI Action combined with the Request Approval (move to assess) UI action for Change Requests with the condition that if a conflict is detected, the script does not advance the state to Assess and it does not trigger approvals. At the moment the script I have does do the conflict checking but it still proceeds with changing the state to Assess and approvals are triggered. I am needing help getting the condition correct to prevent this when a conflict is detected. Script I have so far is attached below. Thanks.

function moveToAssess() {
    // START MODIFICATIONS
    // Check Conflict
    var conflictMsg = $("conflict_msg");
    if (conflictMsg) conflictMsg.remove();
    var fields = "";
    var i = 0;
    if (g_form.getControl("start_date") && g_form.getValue("start_date") == "") {
        fields += ", " + g_form.getLabelOf("start_date");
        i++;
    }
    if (g_form.getControl("end_date") && g_form.getValue("end_date") == "") {
        fields += ", " + g_form.getLabelOf("end_date");
        i++;
    }
    if (g_form.getControl("cmdb_ci") && g_form.getValue("cmdb_ci") == "") {
        fields += ", " + g_form.getLabelOf("cmdb_ci");
        i++;
    }
    if (i > 1) {
        g_form.addErrorMessage(formatMessage(getMessage("To check conflicts, the following fields need values: {0}"), fields.substring(2)), "conflict_msg");
        return false;
    } else if (i === 1) {
        g_form.clearMessages();
        g_form.addErrorMessage(formatMessage(getMessage("To check conflicts, the following field needs a value: {0}"), fields.substring(2)), "conflict_msg");
        return false;
    }

    //Code that runs without 'onclick'
    //Ensure call to server-side function with no browser errors
    if (typeof window == 'undefined')
        _checkConflicts();

    if (g_form.getValue('conflict_status') == 'Conflict') {
        return;
	}
	
        g_form.setValue("state", "-4");
        gsftSubmit(null, g_form.getFormElement(), 'state_model_request_assess_approval');
}
	// END MODIFICATIONS 
if (typeof window == 'undefined')
    updateAndRedirect();

function updateAndRedirect() {
    current.update();
    action.setRedirectURL(current);
}

function _checkConflicts() {

    //Array which stores fields that dont have a value 
    var fieldArray = [];

    //Put Start date label in array if it doesnt have a value
    if (current.start_date.nil())
        fieldArray.push(current.start_date.getLabel());

    //Put End date label in array if it doesnt have a value
    if (current.end_date.nil())
        fieldArray.push(current.end_date.getLabel());

    //Put CMDB CI label in array if it doesnt have a value
    if (current.cmdb_ci.nil())
        fieldArray.push(current.cmdb_ci.getLabel());

    //If there are values stored in the array then print message 
    if (fieldArray.length > 0) {
        var fieldMsg = '';
        for (var i = 0; i < fieldArray.length; i++) {
            fieldMsg += fieldArray[i] + ", ";
        }
        fieldMsg = fieldMsg.substring(0, fieldMsg.length - 2);
        gs.addErrorMessage(gs.getMessage("To check conflicts, the following fields need values: {0}", fieldMsg));
        current.update();
        action.setRedirectURL(current);
        return;
    }
    //else If there are NO values stored in the array execute the action
    else if (fieldArray.length == 0) {
        suppressMaintenanceScheduleMessages = true;
        var conflictDetector = new ChangeCheckConflicts(current);
        var conflictResults = conflictDetector.check();

        current.conflict_status = 'No Conflict';
        var msg;

        if (conflictResults < 0)
            msg = gs.getMessage('Configuration Item needed for conflict analysis');
        else if (conflictResults == 0)
            msg = gs.getMessage('There are <FONT COLOR="green">NO CONFLICTS</FONT>');
        else {
            msg = gs.getMessage('There <FONT COLOR="tomato">ARE CONFLICTS</FONT> - See "Conflicts" related list');
            current.conflict_status = 'Conflict';
        }

        gs.addInfoMessage(msg);
        current.update();
        action.setRedirectURL(current);
    }
}

 

2 REPLIES 2

Rajesh Chopade1
Mega Sage

Hi @beccintech 

To resolve this, you need to stop the flow when a conflict is found. The main problem is that the state is changed regardless of conflict detection.

Try below script once:

function moveToAssess() {
    // START MODIFICATIONS
    // Check Conflict
    var conflictMsg = $("conflict_msg");
    if (conflictMsg) conflictMsg.remove();
    var fields = "";
    var i = 0;
    
    if (g_form.getControl("start_date") && g_form.getValue("start_date") === "") {
        fields += ", " + g_form.getLabelOf("start_date");
        i++;
    }
    if (g_form.getControl("end_date") && g_form.getValue("end_date") === "") {
        fields += ", " + g_form.getLabelOf("end_date");
        i++;
    }
    if (g_form.getControl("cmdb_ci") && g_form.getValue("cmdb_ci") === "") {
        fields += ", " + g_form.getLabelOf("cmdb_ci");
        i++;
    }

    if (i > 1) {
        g_form.addErrorMessage(formatMessage(getMessage("To check conflicts, the following fields need values: {0}"), fields.substring(2)), "conflict_msg");
        return false;
    } else if (i === 1) {
        g_form.clearMessages();
        g_form.addErrorMessage(formatMessage(getMessage("To check conflicts, the following field needs a value: {0}"), fields.substring(2)), "conflict_msg");
        return false;
    }

    //Call server-side function to check conflicts
    if (typeof window == 'undefined')
        _checkConflicts();

    // Prevent advancing if there's a conflict
    if (g_form.getValue('conflict_status') === 'Conflict') {
        g_form.addErrorMessage('Conflict detected. Cannot move to Assess.');
        return false;
    }

    // Proceed if no conflict
    g_form.setValue("state", "-4");
    gsftSubmit(null, g_form.getFormElement(), 'state_model_request_assess_approval');
}

 

i hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

rajesh

Hi @Rajesh Chopade1 , thanks for replying. I tried this and it didn't work. The UI Action completes the conflict check and still proceeds with moving the state to Assess and generating approval requests.