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.

Add Edit button in Affected CIs related list on a change task on CSM and SOW workspaces

Elena Spıru
Mega Guru

Hello,

 

I have a requirement to add an Edit button in Affected CIs related list on a change task on CSM workspace and to have the same functionality like the backend Edit button.

Elena9_0-1724153359635.png

 

Currently we have a custom Edit button on change task that applies a filter to only show Affected CIs that are present in the parent (change request)
Example:

Change request CHG0000011 has only 1 affected CI and one change task CTASK0010054

Elena9_1-1724153485073.png

If I open the change task CTASK0010054 I can see all the details and in the Affected CIs related list the custom Edit button

Elena9_2-1724153567195.png

If I click on the Edit button, I have an applied filter to only show Affected CIs that are present in the parent (CHG0000011):

Elena9_3-1724153644738.png

 

I need to create a button that does the same logic in CSM and SOW workspaces.
Does anybody know how to achieve that?

Thank you,
Elena

1 ACCEPTED SOLUTION

Elena Spıru
Mega Guru

Managed to solve it!
Here is the solution:
Backend EDIT:
Insert&stay the existing OOTB "Edit..." button to which I've added the following logic:

ElenaSpru_0-1725974007946.png

Code:

var uri = action.getGlideURI();
var path = uri.getFileFromPath();
var type = parent.getValue('type');
uri.set('sysparm_m2m_ref', current.getTableName());
uri.set('sysparm_stack', 'no');

//get the change task sys id
var m2mObject = uri.toString('sys_m2m_template.do');
var firstSplit = m2mObject.split('collectionID=');
var secondSplit = firstSplit[1].split('&sysparm_collection_key');
var changeTaskSysId = secondSplit[0] + '';

//get the change request sys id
var changeRequest;
var grGetChangeRequest = new GlideRecord('change_task');
if (grGetChangeRequest.get(changeTaskSysId)) {
changeRequest = grGetChangeRequest.change_request + '';
affectedCIChangeTask();
}

function affectedCIChangeTask() {
var parentAffectedCIs = new changeTaskUtils().getCIsFromChangeRequest(changeRequest);

if (parentAffectedCIs) {
uri.set('sysparm_query', 'sys_idIN' + parentAffectedCIs);
uri.set('jvar_no_filter', 'true');
}
}
action.setRedirectURL(uri.toString('sys_m2m_template.do'));

Script include changeTaskUtils:

 

ElenaSpru_3-1725974080491.png

Code:

var changeTaskUtils = Class.create();
changeTaskUtils.prototype = {
    initialize: function() {},

    getCIsFromChangeRequest: function(changeRequestId) {      
        var cis = [];  
     
        var grTaskCi = new GlideRecord('task_ci');
        grTaskCi.addQuery('task', changeRequestId);
        grTaskCi.query();
        while (grTaskCi.next()) {
            cis.push(grTaskCi.ci_item + '');
        }      
        return cis + '';
    },

    type: 'changeTaskUtils'
};



Workspaces EDIT:
Created a declarative action "Edit...":

ElenaSpru_4-1725974215771.png

 

 

ElenaSpru_5-1725974227047.png

Code:

function executeAction() {
    var changeRequest = g_form.getValue('change_request'); // sys id of 'change_request' field

    var _strLink =
        '/sys_m2m_template.do?' +
        'sys_is_list=true&' +
        'sys_is_related_list=true&' +
        'sysparm_collectionID=' + g_form.getUniqueValue() + '&' +
        'sysparm_referring_url=' + location.pathname + '&' +
        'sysparm_stack=no&' +
        'sys_target=task_ci&' +
        'sysparm_m2m_ref=task_ci&' +
        'sysparm_collection=task&' +
        'sysparm_collection_key=task&' +
        'sysparm_collection_label=Affected CIs&' +
        'sysparm_collection_related_field=ci_item&' +
        'sysparm_collection_related_file=cmdb_ci&';

    var changeReqAffectedCis = new GlideAjax('AssociateCIsToChangeUtil');
    changeReqAffectedCis.addParam('sysparm_name', 'getCIsFromChangeRequest');
    changeReqAffectedCis.addParam('sysparm_changeRequestId', changeRequest);
    changeReqAffectedCis.getXMLAnswer(updateFilter);


    function updateFilter(answer) {
        if (answer) {
            if (changeRequest) {
                _strLink +=
                    'sysparm_query=sys_idIN' + answer + '&' + // Applies the filter
                    'jvar_no_filter=true';
            }
        }

        var win = top.window.open(_strLink, '_self');
        win.focus();
    }
}
Script include AssociateCIsToChangeUtil:

ElenaSpru_7-1725974303003.png

Code:

    getCIsFromChangeRequest: function() {
        var cis = [];
        var changeRequestId = this.getParameter('sysparm_changeRequestId') + '';

        var parentAffectedCIs = new changeTaskUtils().getCIsFromChangeRequest(changeRequestId); //logic is in the server side script include
        return parentAffectedCIs;
    },
This way, inside a change task Affected CIs related list you can only see the Affected CIs present on the parent change request.


 

 

View solution in original post

2 REPLIES 2

Elena Spıru
Mega Guru

Managed to solve it!
Here is the solution:
Backend EDIT:
Insert&stay the existing OOTB "Edit..." button to which I've added the following logic:

ElenaSpru_0-1725974007946.png

Code:

var uri = action.getGlideURI();
var path = uri.getFileFromPath();
var type = parent.getValue('type');
uri.set('sysparm_m2m_ref', current.getTableName());
uri.set('sysparm_stack', 'no');

//get the change task sys id
var m2mObject = uri.toString('sys_m2m_template.do');
var firstSplit = m2mObject.split('collectionID=');
var secondSplit = firstSplit[1].split('&sysparm_collection_key');
var changeTaskSysId = secondSplit[0] + '';

//get the change request sys id
var changeRequest;
var grGetChangeRequest = new GlideRecord('change_task');
if (grGetChangeRequest.get(changeTaskSysId)) {
changeRequest = grGetChangeRequest.change_request + '';
affectedCIChangeTask();
}

function affectedCIChangeTask() {
var parentAffectedCIs = new changeTaskUtils().getCIsFromChangeRequest(changeRequest);

if (parentAffectedCIs) {
uri.set('sysparm_query', 'sys_idIN' + parentAffectedCIs);
uri.set('jvar_no_filter', 'true');
}
}
action.setRedirectURL(uri.toString('sys_m2m_template.do'));

Script include changeTaskUtils:

 

ElenaSpru_3-1725974080491.png

Code:

var changeTaskUtils = Class.create();
changeTaskUtils.prototype = {
    initialize: function() {},

    getCIsFromChangeRequest: function(changeRequestId) {      
        var cis = [];  
     
        var grTaskCi = new GlideRecord('task_ci');
        grTaskCi.addQuery('task', changeRequestId);
        grTaskCi.query();
        while (grTaskCi.next()) {
            cis.push(grTaskCi.ci_item + '');
        }      
        return cis + '';
    },

    type: 'changeTaskUtils'
};



Workspaces EDIT:
Created a declarative action "Edit...":

ElenaSpru_4-1725974215771.png

 

 

ElenaSpru_5-1725974227047.png

Code:

function executeAction() {
    var changeRequest = g_form.getValue('change_request'); // sys id of 'change_request' field

    var _strLink =
        '/sys_m2m_template.do?' +
        'sys_is_list=true&' +
        'sys_is_related_list=true&' +
        'sysparm_collectionID=' + g_form.getUniqueValue() + '&' +
        'sysparm_referring_url=' + location.pathname + '&' +
        'sysparm_stack=no&' +
        'sys_target=task_ci&' +
        'sysparm_m2m_ref=task_ci&' +
        'sysparm_collection=task&' +
        'sysparm_collection_key=task&' +
        'sysparm_collection_label=Affected CIs&' +
        'sysparm_collection_related_field=ci_item&' +
        'sysparm_collection_related_file=cmdb_ci&';

    var changeReqAffectedCis = new GlideAjax('AssociateCIsToChangeUtil');
    changeReqAffectedCis.addParam('sysparm_name', 'getCIsFromChangeRequest');
    changeReqAffectedCis.addParam('sysparm_changeRequestId', changeRequest);
    changeReqAffectedCis.getXMLAnswer(updateFilter);


    function updateFilter(answer) {
        if (answer) {
            if (changeRequest) {
                _strLink +=
                    'sysparm_query=sys_idIN' + answer + '&' + // Applies the filter
                    'jvar_no_filter=true';
            }
        }

        var win = top.window.open(_strLink, '_self');
        win.focus();
    }
}
Script include AssociateCIsToChangeUtil:

ElenaSpru_7-1725974303003.png

Code:

    getCIsFromChangeRequest: function() {
        var cis = [];
        var changeRequestId = this.getParameter('sysparm_changeRequestId') + '';

        var parentAffectedCIs = new changeTaskUtils().getCIsFromChangeRequest(changeRequestId); //logic is in the server side script include
        return parentAffectedCIs;
    },
This way, inside a change task Affected CIs related list you can only see the Affected CIs present on the parent change request.


 

 

Hi @Elena Spıru ,
First of all, thanks for the article! I'm using this approach on another related list, and it works well overall. However, I’ve encountered an issue with the inner buttons of the sys_m2m_template window (Save and Cancel).
When I click the Save button, the window should close and refresh the related list of the SOW, and when I click Cancel, it should simply close the window.
Unfortunately, these buttons aren’t responding.
Have you found any solution for this?

sys_m2m_template