how to restrict not moving to next state it should be in same state when my conditions not satisfy

Teja3
Tera Contributor

pls check below on change client script on problem table
when ever user select source as incident problem etc and under related list if no records associated then it should restrict not moving to assigned state and my code saving its moving to next state

 
// //Function to check associated records when state is in new
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var checks = g_form.getValue('state');
    g_form.addInfoMessage("check" + checks);
    if (checks == '1') //when state isnew
    {
        var ga = new GlideAjax('CheckAssociatedRecords1');
        ga.addParam('sysparm_name', 'checkAssocaited1');
        ga.addParam('sys_id', g_form.getUniqueValue());
        ga.addParam('source', g_form.getValue('u_source'));
        ga.getXML(removePending);
        // Prevent form submission
    }
}

function removePending(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");

    if (answer == 'true') {
        var source = g_form.getValue('u_source');
        if (source == '0') //source is IT Incident
        {
            g_form.addInfoMessage("Since the Source is selected as Incident, please associate Incident in the IT Incident list of the relevant tabs below");
        } else if (source == '1') //Source is Event
        {
            g_form.addInfoMessage("Since the Source is selected as Event, please associate alert in the Alerts list of the relevant tabs below");
        } else if (source == '4') // Source is IT Problem
        {
            g_form.addInfoMessage("Since the Source is selected as IT Problem, please associate problem  in the IT Problems list of the relevant tabs below");
        } else if (source == '8') //Source is IT Release
        {
            g_form.addInfoMessage("Since the Source is selected as IT Release, please associate release in the ITPPM RM Release list of the relevant tabs below");
            //alert("Since the Source is selected as IT Release, please associate release in the ITPPM RM Release list of the relevant tabs below");
        }
    }
    if(answer!='true'){
        g_form.setValue('state','1');
    }
}

below is script include

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

    checkAssocaited1: function() {
        var rec = this.getParameter('sys_id');
        var src=this.getParameter('source');



        if (src== '0') //when source is IT incident
        {
            var inc = new GlideRecord('itinc');
            inc.addQuery('problem', rec);
            inc.query();
            if (inc.getRowCount() < 1) {
                return true;
            }
            //it_problem

        } else if (src== '1') // when source is Event
        {
            var event = new GlideRecord('alert');
            event.addQuery('incident', rec);
            event.query();
            if (event.getRowCount() < 1) {
                return true;
            }
        } else if (src== '4') // when source is IT problem
        {
            var prb = new GlideRecord('problem');
            prb.addQuery('problem', rec);
            prb.query();
            if (prb.getRowCount() < 1) {
                return true;
            }
        } else if (src== '8') // When source is IT Release
        {
            var rel = new GlideRecord('ppm_rm_release');
            rel.addQuery('parent', rec);
            rel.query();
            if (rel.getRowCount() < 1) {
                return true;
            }

        }

    },

    type: 'CheckAssociatedRecords1'
});
7 REPLIES 7

Moin Kazi
Kilo Sage
Kilo Sage

Hi @Teja3 ,

 

You are using an OnChange client script, which does not execute on the server side. This means that the record will be saved without any issues. If you want to prevent the record from being saved when there are no records present in your related list, you'll need to use a Business Rule(onBefore Update) instead.

You can use gs.setAbortAction(true); to prevent the current action from completing in business rule.

 

[Notes - One benefit of writing a Business Rule is that it executes in list view as well. So, if someone changes the state in the list view, the Business Rule will run, and if the condition is met, it will prevent the record from being updated.]

 

Hope this help you

 

Regards

Moin

 

Teja3
Tera Contributor

I've tried using business rule and used setabortaction its preventing once but if we refresh the form it remains same state if we save it then its moving to next state this should not be the case
how can i prevent this

Could you please provide details about what you wrote in the Business Rule, when it triggers, and under what circumstances the record should not be saved?

Teja3
Tera Contributor

Before insert and update
state is new and assigned to changes

(function executeRule(current, previous) {

    if (current.state == '1') { // Checks if state is new

        var source = current.u_source;

        var hasRelatedRecords = false;

        var infomessage = '';

 

        if (source == '0') {

            // Check if there are associated incidents under the related list of IT Incident tab

            var incidentGr = new GlideRecord('incident');

            incidentGr.addQuery('problem', current.sys_id);

            incidentGr.query();

            if (incidentGr.next()) {

                hasRelatedRecords = true;

            }

            infomessage = "Since the Source is selected as IT Incident, please associate an Incident in the IT Incident list of the relevant tabs below.";

        } else if (source == '1') {

            // Check if there are associated alerts under the related list of the alerts tab

            var alertGr = new GlideRecord('em_alert');

            alertGr.addQuery('incident', current.sys_id);

            alertGr.query();

            if (alertGr.next()) {

                hasRelatedRecords = true;

            }

            infomessage = "Since the Source is selected as Event, please associate an alert in the Alerts list of the relevant tabs below.";

        } else if (source == '4') {

            // Check if there are associated problems under the related list of IT Problem tab

            var problemGr = new GlideRecord('problem');

            problemGr.addQuery('problem', current.sys_id);

            problemGr.query();

            if (problemGr.next()) {

                hasRelatedRecords = true;

            }

            infomessage = "Since the Source is selected as IT Problem, please associate a problem in the IT Problems list of the relevant tabs below.";

        } else if (source == '9') {

            var servicereq = new GlideRecord('sc_req_item'); // Check if there are associated service requests under the related list of the service request tab

            servicereq.addQuery('parent', current.sys_id);

            servicereq.query();

            if (servicereq.next()) {

                hasRelatedRecords = true;

            }

            infomessage = "Since the source is selected as Service Request, please associate a request in the Service Request of the relevant tabs below.";

        }

 

        if (!hasRelatedRecords && current.state == '1') {

            // Revert the state to the previous value

            gs.addInfoMessage(infomessage);

           current.setAbortAction(true);

   

        }

    }

   

 

})(current, previous);