10 Replies · Latest reply on Sep 14, 2017 1:06 AM by souren007

    change task closure when change record closed

      hi,

       

      When a change record is closed the associated change task is closing automatically but I don't want to close the change task automatically. How should I restrict that? is there any OOB script or workflow running behind to close the related change task automatically?

      Can anyone help me with the solution?

        • Re: change task closure when change record closed
          Dave Smith

          Why is the change record closing if there's still a task in progress?  Doesn't this logic strike you as odd?

          • Re: change task closure when change record closed
            souren007

            Hi Asha,

             

            As Dave suggested keeping the change task active closing change request does not make any sense. Because we use change tasks as an activity before closing change request.

             

            Still if you wish to change it, check there must be a business rule configured on change_request table. You can achive it by modifying it.

             

            Regards,

            Souren

              • Re: change task closure when change record closed
                Asha M

                Hi Sourne,

                 

                Thank you

                  • Re: change task closure when change record closed
                    Asha M

                    I didn't find any script in business rule related to change task autoclosure.

                      • Re: change task closure when change record closed
                        souren007

                        Hi,

                         

                        Usually, it's a OOB feature(configured in Task table) where the child records get's closed when you close the parent ticket.

                         

                        I tried resolve this problem by GlideAjax and GlideRecord which is not recommended to use in client script. But couldn't find it as GlideAjax doesn't work onSubmit client side as it is a asynchronous call. The form gets submitted when the query is running on server.  It's possible to use it if you try onChange client script.

                         

                        Anyway I'm proving both the solutions:

                        1. onSubmit GlideRecord on change_request table

                         

                        function onSubmit() {

                        //Type appropriate comment here, and begin script below

                        var sysID = g_form.getUniqueValue();

                        var changeState  = g_form.getValue('state');

                        if(changeState=='3')

                        {

                        var chg = new GlideRecord('change_task');

                        chg.addQuery('active',true);

                        chg.addQuery('change_request',sysID);

                        chg.query();

                        if(chg.next())

                        {

                        alert("Please close the change task");

                        return false;

                        }

                        }

                        }

                        The above solution will work properly but not recommended for performance.

                         

                        2. onChange GlideAjax on change_request table:

                         

                        Script Include:

                         

                        Name: GetChangeTask

                        Client callable: true

                         

                        var GetChangeTask = Class.create();

                        GetChangeTask.prototype = Object.extendsObject(AbstractAjaxProcessor, {

                        getOpenChangeTask: function(){

                        var recordId = this.getParameter('sysparm_sys');

                        var gr = new GlideAggregate('change_task');

                        gr.addQuery('change_request', recordId);

                        gr.addQuery('state','!=', '3'); //not closed complete

                        gr.query();

                        if(gr.next())

                        {

                        return true;

                        }

                        },

                        type: 'GetChangeTask'

                        });

                         

                        Client Script:

                         

                        variable:  state

                         

                        function onChange(control, oldValue, newValue, isLoading, isTemplate) {

                           if (isLoading || newValue === '') {

                              return;

                           }

                         

                         

                           //Type appropriate comment here, and begin script below

                        var sysID = g_form.getUniqueValue();

                        //var changeState  = g_form.getValue('state');

                        if(newValue=='3')

                        {

                        var ajax = new GlideAjax('GetChangeTask');

                        ajax.addParam('sysparm_name','getOpenChangeTask');

                        ajax.addParam('sysparm_sys', sysID);

                        ajax.getXML(doSomething);

                         

                        function doSomething(response){

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

                        if (answer)

                        {

                        alert("Please close the change task");

                        return false;

                        }

                        }

                        }

                          

                        }

                         

                        This solution will work only onChange client script. But in your case it's required to have a onSubmit script, where the first solution will work properly.

                         

                        Thanks and Regards,

                        Souren

                  • Re: change task closure when change record closed
                    Mihir Mohanta

                    Create a before update business rule in change_request table like below image.It will restrict the change request to close complete if its associated task still open.

                     

                     

                     

                     

                    Script:

                     

                    (function executeRule(current, previous /*null when async*/) {

                    // Add your code here

                    var rec = new GlideRecord('change_task');

                    rec.addQuery('change_request',current.sys_id);

                    var qc = rec.addQuery('state', 1);

                        qc.addOrCondition('state', 2);

                    qc.addOrCondition('state', -5);

                    rec.query();

                    while(rec.next()) {

                    gs.addErrorMessage('You have to complete change tasks before change request completes');

                    current.setAbortAction(true);

                    }

                    })(current, previous);

                    Thanks,
                    Mihir