Date validation to hide a attachments on change request

Indira8
Kilo Sage

Hi,

We have a requirement to show the attachments for change requests only until 15 days after closure date. Could you help me with the logic for the onload client script for this requirement. 

 

Thank you 

2 REPLIES 2

Community Alums
Not applicable

Hi @Indira8 ,

 

The only way to hide the attachment is to make it unavailable(state field in sys_attachment) after a specific date so that its not downloadable.

The other way is to delete the attachments, which i believe is not appropriate.

 

So getting back to our first way, restricting the attachment to be downloaded.

 

Create a client script to do the same using GlideAjax

var gr = new GlideRecord("sys_attachment");
			gr.addQuery('table_sys_id', "currentRecord");
			gr.addQuery('table_name','change_request');
			gr.query();
			if (gr.next()) {
			   gr.state = not_available; // set the state
			}

 

Use the above logic to make the state of the attachment unavailable.

 

setting the state of attachment corresponding record as unavailable.

SanjayG_0-1715892226973.png

 

Change request showing the attachment is unavailable.

SanjayG_1-1715892262684.png

 

There can be a way for DOM manipulation but ServiceNow doesn't recommends it.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

 

swathisarang98
Giga Sage
Giga Sage

Hi @Indira8 ,

 

Create aonload client script call a script include to add years and then validdate with. your field and based on that hide attachment,

 

Client side:

swathisarang98_0-1715971215183.png

 

function onLoad() {
	var closedDate =g_form.getValue('end_date');// change to your field name
    var ga = new GlideAjax('getenddDate');
    ga.addParam('sysparm_name', 'getDate');
	ga.addParam('sysparm_date',closedDate);
    ga.getXML(getResponse);
}

function getResponse(response) {
    var ans = response.responseXML.documentElement.getAttribute('answer');
    alert('ans' + ans);
    if (g_form.getValue('end_date') >= ans) {
        document.getElementById('header_attachment').style.display = 'none';
        document.getElementById('header_attachment_line').style.display = 'none';
    }

}

 

Script include:

swathisarang98_1-1715971239185.png

 

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

	getDate: function(){
		var endDate = this.getParameter('sysparm_date');
		var todayDate = new GlideDate(endDate);
		todayDate.addDays(15);
		gs.info('today Date' + todayDate);
		return todayDate;

	},

    type: 'getenddDate'
});

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang