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 Is there a way to update this to include an additional condition to set the field to false if there are no attachments or the attachment has been removed?

Any luck, for example with my last tested example?

Just to show, when I add a random image to the record, immediately the custom field is being updated:

find_real_file.png

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

Hi, I just did this on our instance and it is not working. The u_has_attachment field is in the hr core table. I think, I'm doing something wrong. Can you tell?

find_real_file.png

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

That works perfectly! Thank you! Just have one more question, the condition is async, what can you suggest for those existing case that has already attachment?