Date validation to hide a attachments on change request
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 10:08 AM - edited 05-16-2024 10:12 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2024 01:48 PM
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.
Change request showing the attachment is unavailable.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2024 11:41 AM
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:
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:
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