- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2025 09:55 PM
Hello,
I want to create a new field called Attachment and type is True/False on Case table. If a case has an attachment, it should mark the field as true and if not, it should remain false.
Below is my field
Business Rule:
(function executeRule(current, previous /*null when async*/) {
if (current.table_name !== 'sn_customerservice_case') {
return;
}
var caseId = current.table_sys_id;
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'sn_customerservice_case');
attachmentGR.addQuery('table_sys_id', caseId);
attachmentGR.query();
var hasAttachments = attachmentGR.hasNext();
var caseGR = new GlideRecord('sn_customerservice_case');
if (caseGR.get(caseId)) {
if (caseGR.u_attachment !== hasAttachments) {
caseGR.u_attachment = hasAttachments;
caseGR.update();
}
}
})();
I have written after insert delete BR, above is my code, I have attached a attachment in one of the case record in case table.
I have added that field in list view, always it is showing as false
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2025 11:56 PM
try to exclude the current sysId in query
(function executeRule(current, previous /*null when async*/ ) {
var caseId = current.table_sys_id;
var attachmentRec = new GlideRecord('sys_attachment');
attachmentRec.addQuery('table_sys_id', caseId);
attachmentRec.addQuery('sys_id', '!=', current.sys_id); // exclude the current one
attachmentRec.setLimit(1);
if (!attachmentRec.hasNext()) {
var caseGR = new GlideRecord('sn_customerservice_case');
if (caseGR.get(caseId)) {
caseGR.u_attachment = false;
caseGR.update();
}
}
})();
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2025 10:33 PM
have 2 business rules
1) after insert on sys_attachment
2) before delete on sys_attachment
condition should be this for After insert
Table Name = sn_customerservice_case
Script:
(function executeRule(current, previous /*null when async*/ ) {
var caseId = current.table_sys_id;
var caseGR = new GlideRecord('sn_customerservice_case');
if (caseGR.get(caseId)) {
caseGR.u_attachment = true;
caseGR.update();
}
})();
Business rule Before Delete:
Table Name = sn_customerservice_case
Script:
(function executeRule(current, previous /*null when async*/ ) {
var caseId = current.table_sys_id;
var caseGR = new GlideRecord('sn_customerservice_case');
if (caseGR.get(caseId)) {
caseGR.u_attachment = true;
caseGR.update();
}
})();
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2025 11:15 PM
after insert is working fine, when I have added attachment in any case record, attachment is changed to true - working as expected.
before delete is not working means, after I have removed the attachment from case record, still it is showing as true only.
below is example
there is no attachment
still it is showing as true
in before delete script
caseGR.u_attachment = true;
is it true or false?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2025 11:19 PM
Hi @Gopal14 , in the before delete script it should be -
caseGR.u_attachment = false;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2025 11:21 PM
it should be before delete and not after delete
Did you add logs and see what came in log for case sysId etc?
whether it's going inside the IF part?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2025 11:26 PM