- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2019 07:15 AM
Hi,
I want that if incident has an attachment attached the field should show as true, otherwise false.
I am new to servicenow and not good at coding but I have written the following before business rule code on incident table.
Please also let me know Client Script is better or Business Rule.
Below mentioned code is not working. Can someone please help me with the mistake?
var rec = new GlideRecord('incident');
rec.addQuery('sys_id', current.incident_sys_id);
rec.query();
if(rec.next()){
if(rec.hasAttachments()){
rec.u_attachment= true;
}
}
Thanks
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2019 11:10 PM
Hi ,
I tried the following code in a before insert, update BR in my dev instance, which is working fine,
(function executeRule(current, previous /*null when async*/) {
if(current.hasAttachments()){
current.setValue("u_has_attachment","true");
gs.addInfoMessage("Attachment Present");
}
else if(!current.hasAttachments())
{
current.setValue("u_has_attachment","false");
gs.addInfoMessage("Attachment Absent");
}
Regards,
Ajay

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2019 07:28 AM
HI,
Please follow this link it's helpful you
Please mark reply as Helpful/Correct, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2019 08:34 AM
Hi Prabhmeet,
use the below code
var sys_id = gel('sys_uniqueValue').value;
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery('table_name','incident');
attachment.addQuery('table_sys_id',sys_id);
attachment.query();
if (attachment.next()) {
alert ('There is an attachment.');// Here you can return true
}
else {
alert ('There is no attachment.');// Here you can return false
}
Mark it correct/helpful if it helps you
Regards
Ravindra

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2019 10:09 AM
Hi Prabhmeet,
I think the script provided by Ravi will work.
"Please also let me know Client Script is better or Business Rule"
> If you can achieve something by using a server side(in this case: Business rule) script than go with it, instead of using a client-side script.
Client-side programming is often preferred by users, due to the immediate interactivity of the scripts. Server-side programming is often preferred by developers due to eased complexity and better performance.
Refer Client side and server side programming for more information.
Regards,
Ajay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2019 10:38 AM
Hi Prabhmeet,
Did you try the code?