Should avoid copying of attachment to new CR on click of Copy Change when in Closed/Cancelled state

Venkat03
Tera Contributor

On the Change Request form when I click on the Copy Change button the attachment should be copied over to the new CR form only up to the Review state 

 

When I click the same Copy Change button in the Closed or Cancelled state the attachment should be copied to the new CR

 

Thanks,

Venkat

1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi @Venkat03 ,

 

This can be achieved, but the most important thing to be mindful is this will be a customization. Going forward you would need to maintain it being a de-merit when we customize things.

 

To achieve above, please follow the steps below:

1. OOTB there is a method named "copyChangeAttachments" within the script include "ChangeUtilsSNC" which is responsible for copying attachments when Copy Change is being used. 

2. To modify this, you would need to override this method as the above one is read only.

3. Navigate to the Script Include named "ChangeUtils" using the below link :

https://instance Name.service-now.com/nav_to.do?uri=sys_script_include.do?sys_id=5eab2cf09f333100292... 

 

Replace "instance name" with your instance name in above URL

4. Now update this Script include using the script shared below and as shown in screenshot below:

var ChangeUtils = Class.create();
ChangeUtils.prototype = Object.extendsObject(ChangeUtilsSNC, {

    initialize: function(request, responseXML, gc) {
        ChangeUtilsSNC.prototype.initialize.call(this, request, responseXML, gc);
    },

    /***************************
     *
     *Add customer changes below
     *
     ****************************/

    copyChangeAttachments: function( /*String*/ srcSysId, /*String*/ targetSysId) {
        var isCopyEnabled = gs.getProperty(this.PROP_CHANGE_ATTACH_COPY_ENABLED, 'true') == 'true';
        if (isCopyEnabled) {
            var gr = new GlideRecordSecure(this.CHANGE_REQUEST);
            gr.addQuery('sys_id', srcSysId);
            gr.addQuery('state', '!=', 3);
            gr.query()
            if (gr.next()) {
                return this._copyAttachments(gr, targetSysId);
            } else {
                this.logErr('copyChangeAttachments: Provided Change Request does not exist - ' + srcSysId);
            }
        }
    },
	_copyAttachments: function(srcGr, targetSysId) {
		var res = [];
		if (srcGr.hasAttachments() && this.hasReadWriteAccess()) {
			var table = srcGr.getTableName();
			res = j2js(GlideSysAttachment.copy(table, srcGr.getUniqueValue(), table, targetSysId));
		}
		return res;
	},

	hasReadWriteAccess: function() {
		var tableDesc = GlideTableDescriptor.get(this.CHANGE_REQUEST);
		return tableDesc.canRead() && tableDesc.canWrite();
	},


    type: 'ChangeUtils'
});

/********************************************************************
 *The function below is written in this way to provide access via the
 *signature ChangeUtils.isCopyChangeEnabled() from UI Action -
 *'Copy Change' > Condition.
 *
 *Customers are suggested to override methods inside the body of
 *ChangeUtils' object definition.
 ********************************************************************/

ChangeUtils.isCopyChangeEnabled = function(current) {

    var changeUtils = new ChangeUtils();

    if (changeUtils.isCopyFlagValid() && changeUtils.isCopyRulesValid(current)) {
        return true;
    } else {
        return false;
    }

};

shloke04_0-1704623811232.png

 

5. Once you have made the changes, for closed state attachments will not get copied using copy change UI Action.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

5 REPLIES 5

shloke04
Kilo Patron

Hi @Venkat03 ,

 

This can be achieved, but the most important thing to be mindful is this will be a customization. Going forward you would need to maintain it being a de-merit when we customize things.

 

To achieve above, please follow the steps below:

1. OOTB there is a method named "copyChangeAttachments" within the script include "ChangeUtilsSNC" which is responsible for copying attachments when Copy Change is being used. 

2. To modify this, you would need to override this method as the above one is read only.

3. Navigate to the Script Include named "ChangeUtils" using the below link :

https://instance Name.service-now.com/nav_to.do?uri=sys_script_include.do?sys_id=5eab2cf09f333100292... 

 

Replace "instance name" with your instance name in above URL

4. Now update this Script include using the script shared below and as shown in screenshot below:

var ChangeUtils = Class.create();
ChangeUtils.prototype = Object.extendsObject(ChangeUtilsSNC, {

    initialize: function(request, responseXML, gc) {
        ChangeUtilsSNC.prototype.initialize.call(this, request, responseXML, gc);
    },

    /***************************
     *
     *Add customer changes below
     *
     ****************************/

    copyChangeAttachments: function( /*String*/ srcSysId, /*String*/ targetSysId) {
        var isCopyEnabled = gs.getProperty(this.PROP_CHANGE_ATTACH_COPY_ENABLED, 'true') == 'true';
        if (isCopyEnabled) {
            var gr = new GlideRecordSecure(this.CHANGE_REQUEST);
            gr.addQuery('sys_id', srcSysId);
            gr.addQuery('state', '!=', 3);
            gr.query()
            if (gr.next()) {
                return this._copyAttachments(gr, targetSysId);
            } else {
                this.logErr('copyChangeAttachments: Provided Change Request does not exist - ' + srcSysId);
            }
        }
    },
	_copyAttachments: function(srcGr, targetSysId) {
		var res = [];
		if (srcGr.hasAttachments() && this.hasReadWriteAccess()) {
			var table = srcGr.getTableName();
			res = j2js(GlideSysAttachment.copy(table, srcGr.getUniqueValue(), table, targetSysId));
		}
		return res;
	},

	hasReadWriteAccess: function() {
		var tableDesc = GlideTableDescriptor.get(this.CHANGE_REQUEST);
		return tableDesc.canRead() && tableDesc.canWrite();
	},


    type: 'ChangeUtils'
});

/********************************************************************
 *The function below is written in this way to provide access via the
 *signature ChangeUtils.isCopyChangeEnabled() from UI Action -
 *'Copy Change' > Condition.
 *
 *Customers are suggested to override methods inside the body of
 *ChangeUtils' object definition.
 ********************************************************************/

ChangeUtils.isCopyChangeEnabled = function(current) {

    var changeUtils = new ChangeUtils();

    if (changeUtils.isCopyFlagValid() && changeUtils.isCopyRulesValid(current)) {
        return true;
    } else {
        return false;
    }

};

shloke04_0-1704623811232.png

 

5. Once you have made the changes, for closed state attachments will not get copied using copy change UI Action.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Thank you @shloke04 for your response

 

Thanks,

Venkat

Iraj Shaikh
Mega Sage
Mega Sage

Hi @Venkat03 

 

You need to update the OOTB existing client callable 'ChangeUtils' script include as below to accomplish your requirement

 

 

var ChangeUtils = Class.create();
ChangeUtils.prototype = Object.extendsObject(ChangeUtilsSNC, {

    initialize: function(request, responseXML, gc) {
        ChangeUtilsSNC.prototype.initialize.call(this, request, responseXML, gc);
    },

    /***************************
     *
     *Add customer changes below
     *
     ****************************/

    // Override the copyChangeAttachments method to include state check
    copyChangeAttachments: function(srcSysId, targetSysId) {
        // Get the source change request record
        var gr = new GlideRecord(this.CHANGE_REQUEST);
        if (!gr.get(srcSysId)) {
            this.logErr('copyChangeAttachments: Provided Change Request does not exist - ' + srcSysId);
            return [];
        }

        // Check if the state is valid for copying attachments
        if (this.isStateValidForCopy(gr)) {
            // Call the base class method to copy attachments
            return ChangeUtilsSNC.prototype.copyChangeAttachments.call(this, srcSysId, targetSysId);
        }

        // If the state is not valid, do not copy attachments
        return [];
    },

    // Method to check if the current state allows copying attachments
    isStateValidForCopy: function(changeRequestGR) {
        // Define the states that allow copying attachments
        var validStates = [
            '-5', // New
            '-4', // Assess
            '-3', // Authorize
            '-2', // Scheduled
            '-1', // Implement
            '0' // Review
        ];
        // Get the current state of the change request
        var currentState = changeRequestGR.getValue('state');
        // Check if the current state is in the list of valid states
        return validStates.indexOf(currentState) !== -1;
    },


    type: 'ChangeUtils'
});

/********************************************************************
 *The function below is written in this way to provide access via the
 *signature ChangeUtils.isCopyChangeEnabled() from UI Action -
 *'Copy Change' > Condition.
 *
 *Customers are suggested to override methods inside the body of
 *ChangeUtils' object definition.
 ********************************************************************/

ChangeUtils.isCopyChangeEnabled = function(current) {

    var changeUtils = new ChangeUtils();

    if (changeUtils.isCopyFlagValid() && changeUtils.isCopyRulesValid(current)) {
        return true;
    } else {
        return false;
    }

};

 

 

 

Please mark this response as correct or helpful if it assisted you with your question.

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Venkat03 

 

OOTB it is configured in property, by using property you change in UI action

 

LearnNGrowAtul_0-1704643928175.png

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************