Mandatory confirmation on attachments

Carol2
Tera Contributor

Hi All 

 

I have a requirement I need assistance with "Mandatory confirmation on attachments for Implementation Plan, Test Evidence, Business Approval Pre and Post Implementation on all Changes" what could be the best way to create this confirmation?

 

User must be able to somehow tick that 3 attachments have been added to the change request before submitting. 

 

 

 

10 REPLIES 10

doesn't work i am able to save the change without attachments 

 

my script include 

Carol2_0-1732096381789.png

Client script

 

Carol2_1-1732096423718.png

what am i missing @Runjay Patel ?

Hi @Carol2 ,

 

alert attachmentCount to check what value coming from server side.

Also add gs.log(count) in script include and check see what value coming.

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

This approach is not the optimized way to query. Let me give you other approach using GlideAggregate.

Script Include code: 

getAttachmentCount: function() {

var recordSysId = this.getParameter('sysparm_sys_id');

var tableName = this.getParameter('sysparm_table');

var ga = new GlideAggregate('sys_attachment');

ga.addQuery('table_name',tableName);

ga.addQuery('table_sys_id',recordSysId);

ga.addAggregate('COUNT');

ga.query();

if(ga.next()) {
return ga.getAggregate('COUNT');
}

return 0;
}



OnLoad Client Script: GlideAjax

function onSubmit() {

var ga = new GlideAjax('AttachmentUtils');

ga.addParam('sysparm_name', 'getAttachmentCount');
ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
ga.addParam('sysparm_table', g_form.getTableName());

ga.getXMLWait();

var count = parseInt(ga.getAnswer(),10);

if (count < 3) {

g_form.addErrorMessage('Minimum 3 attachments are required before submission.');

return false;
}

return true;
}



Note: we need to pass the table name and record sys id to the script include for querying the attachment table and have to use the glideAggregate api for the count to optimize the execution.

we are passing the table name and record sys id from client script-glideAjax to the script include.

and this approach can be bypassed when the user enter data through other ways like import/api/background script so we need to have a Before business rule logic to prevent submission without 3 attachments

Before BR :

 

(function executeRule(current, previous) {

var ga = new GlideAggregate('sys_attachment');

ga.addQuery('table_name', current.getTableName());
ga.addQuery('table_sys_id', current.sys_id);

ga.addAggregate('COUNT');
ga.query();

var count = 0;

if (ga.next()) {
count = parseInt(ga.getAggregate('COUNT'),10);
}

if (count < 3) {

gs.addErrorMessage('Minimum 3 attachments are required before submission.');

current.setAbortAction(true);
}

})(current, previous);



   @Carol2 @Runjay Patel @Sohail Khilji 
Hope this will helps, Thank you
Rishi.

 

Sohail Khilji
Kilo Patron

HI @Carol2 ,

 

There are 2 aspect to look and also depends how you can to take this customization.

 

First way :

1. You can create a on submit client script and script include that checks if the record submitted has 3 attachments added before the submission is made.  (in this case you never know if the user has updated the appropriate file which was expected, maybe you can check the file name on submission as an additional condition.)

 

in this way you can use the code given by @Runjay Patel  below to check the file and its name

 

Second Way :

You can introduce 3 new attachment field under the fields '' Implementation Plan, Test Evidence, Business Approval Pre and Post Implementation'' and apply UI policy to make the attachment mandatory.

 

I hope this helps...


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Carol2
Tera Contributor

Hi @Runjay Patel @Sohail Khilji 

 

Thanks for your input, i opted for a business rule and it works. 

Carol2_0-1732100451322.png

 

Regards

Ca