How to check if HR case has attachment

Pearl3
Kilo Contributor

Our HR wanted to check if HR case has attachment.

I created a field "u_has_attachment" on the table and a business rule to that uses function hasAttachment. However, this function is cannot be used on a scoped application. HI Support suggested we create our custom script to make this possible. 

Is there any way that we can check if HR cases has attachment?

1 ACCEPTED SOLUTION

Can you test with the exact code I provided earlier or the code below? So leave getValue('table_name') as it is 🙂 don't replace this with sn_hr_core etc..

When I test with below code it works fine:

(function executeRule(current, previous /*null when async*/) {

	var gr = new GlideRecord(current.getValue('table_name'));
	gr.addQuery('sys_id', current.getValue('table_sys_id'));
	gr._query();

	if(gr._next()) {
		gr.u_has_attachment = true;
		gr.update();
	}

})(current, previous);

Only thing is, earlier I did had a condition on the Business Rule. That condition will not always work. Because for example opening a random out-of-the-box HR Case, might go actually to a different table then sn_hr_core_case, for example, sn_hr_core_case_total_rewards.

For testing purposes I just removed the condition for now and it works fine on a random HR case.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

9 REPLIES 9

Mark Roethof
Tera Patron
Tera Patron

Hi there,

You could set up a GlideRecord query, to query the sys_attachment table. Just an IF query, with setLimit(1), if there's result, you have an attachment.

Would that be something?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Mark Roethof
Tera Patron
Tera Patron

For example:

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', current.getTableName());
gr.addQuery('table_sys_id', current.getUniqueValue());
gr.setLimit(1);
gr._query();

if(gr._next()) {
	//Your code goed here
}

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Pearl3
Kilo Contributor

Thank for your response, but the purpose is to set the custom field set to true if there is an attachment on the hr case. Because we are planning to use the field for reporting purposes.

So then you could use this code or something similar in an update Business Rule, to update your case? That matches what you described as requirement.

Or alternative (just tested, works):

A async on insert business rule on sys_attachment:

(function executeRule(current, previous /*null when async*/) {

	var gr = new GlideRecord(current.getValue('table_name'));
	gr.addQuery('sys_id', current.getValue('table_sys_id'));
	gr._query();

	if(gr._next()) {
		gr.setValue('u_has_attachment', true);
		gr.update();
	}

})(current, previous);

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn