Store the number of attachments when record is created and compare to present number of attachments

AM24
Giga Guru

Hello, when I submit a form using a record producer, I add an attachment. When I access the record on the backend, I want to update this record and change the state of the record. I want a check in place to make sure that an additional attachment has been added before I make this update. Therefore, I am trying to store the initial number of attachments and compare it to the current number of attachments, so upon update, it can be checked whether the current number of attachments is greater than the initial. If this is not the case, an error will be thrown. I have not really found good way to achieve this yet.

2 REPLIES 2

Tony Chatfield1
Kilo Patron

Hi, exactly how would depend on your record producer configuration. You would need to add a string or int field to your target table to record the number of attachments, then depending on your record producer configuration IE when the target record is created and attachments copied to it, you would update the record producer script (or if necessary use an after insert BR) to query sys_attachment table and get a count of the attachments, then populate this value into your new 'count' field.
You would then need a before insert BR on sys_attachment to validate any additional attachments for your record.
Also, what will you do if someone deletes an attachment and then adds another?

I would also recommend you carefully work through potential impact to other records created outside of record producer in case they are impacted by your solution.

 

reshmapatil
Tera Guru

Hi @AM24 ,

 

1. You can use Post Insert Script present on Record Producer:

reshmapatil_0-1671566536643.png

var attachCount;
var attachGR = new GlideAggregate('sys_attachment');
attachGR.addQuery('table_sys_id', '=', current.sys_id.toString());
attachGR.addAggregate('COUNT');
attachGR.query();
while (attachGR.next()) {
attachCount = attachGR.getAggregate('COUNT');
}
current.correlation_id = attachCount;
current.update();

 

2. Before update Business Rule:

reshmapatil_1-1671566612743.pngreshmapatil_2-1671566635593.png

var attachCount;
var attachGR = new GlideAggregate('sys_attachment');
attachGR.addQuery('table_sys_id','=',current.sys_id.toString());
attachGR.addAggregate('COUNT');
attachGR.query();
while (attachGR.next()) {
attachCount = attachGR.getAggregate('COUNT');
}
if(attachCount<=current.correlation_id){
gs.addInfoMessage('Please add new Attachment');
current.setAbortAction(true);
}

 

 

Regards,

Reshma

**Please mark my answer correct or helpful based on the impact**