- 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 12:41 PM
Hi Ravi, The below mentioned Client Script is working but, we should not use Glide Record in CLient Scripts and use Script include and GLideAjax instead.
How Can i change the Script
function onSubmit() {
//Type appropriate comment here, and begin script below
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()) {
g_form.setValue('u_attachment',true);
}
else{
g_form.setValue('u_attachment',false);
}
}

- 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-27-2019 04:58 AM
Hi Ajay, Thanks, i tried it and it is working but I need to manually check the has attachments checkbox, it is not selecting on its own the first time.
How to correct this?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2019 06:56 AM
Hi Prabhmeet,
It will work, if the incident record is inserted or updated, depending upon whether any attachment is present or not.
Regards,
Ajay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2019 07:26 AM
Hi Ajay,
It is working perfectly if I am creating a new incident.
But If I am updating an incident the attachment field is not changing to true. I have to manually check the checkbox.
Is updating working for you?