Attachment mandatory for catalog item

imran sk
Tera Contributor

Hello,

 

This line I have used to check attachment mandatory during the request submission and is working as expected...now attachment is mandatory while submitting request.

 

"this.document.getElementsByClassName('get-attachment').length==0". Can someone please explain how it is working.. how we know what is the class name.. 

 

Thanks in advance 🙂 

4 REPLIES 4

Community Alums
Not applicable

Hi @imran sk ,

The class name "get-attachment" is typically defined in the ServiceNow UI macro or script that implements the attachment retrieval functionality. This class name is used to identify the elements responsible for triggering the attachment retrieval process, such as buttons or links.

Here are some possible sources for the class name:

  • UI macro: If the attachment retrieval is implemented using a UI macro, the class name can be defined in the macro's HTML template or JavaScript code.
  • Script include: If the attachment retrieval is implemented using a script include, the class name can be defined in the script's code and then used in the UI macro or other components that interact with the script.
  • Custom application: If the attachment retrieval is part of a custom application, the class name can be defined in the application's UI components or scripts.

To find the specific location of the class name in your ServiceNow instance, you can search through the following areas:

  • UI macros: Open the UI macro editor and search for the class name in the HTML template or JavaScript code.
  • Script includes: Open the script include editor and search for the class name in the script's code.
  • Custom applications: If you know the name of the custom application, you can search for the class name in the application's UI components or scripts.

If you are unable to find the class name using these methods, you may need to consult with your ServiceNow administrator or developer to get more information.

 

Do we get other classes as well ?

 

Yes, in ServiceNow, you can find various other class names associated with different UI elements and functionalities. Here are some common classes you might encounter:

1. **Attachment Classes**: Besides `get-attachment`, you might see classes like `attachment` or `attachment_link` used for managing and displaying attachments.

2. **Form and Field Classes**: Classes such as `form-control`, `input`, or `field_label` are often used for form fields and labels.

3. **Notification and Message Classes**: Classes like `notification`, `error`, or `info` may be used for displaying system messages or alerts.

4. **Button Classes**: Buttons may use classes like `btn`, `btn-primary`, or `btn-secondary` for styling and functionality.

5. **Navigation and Layout Classes**: Classes like `nav`, `sidebar`, or `header` are common for structuring the layout of pages.

6. **Custom Classes**: If your organization has customizations, you might find additional classes defined by developers for specific purposes.

Finding Classes

To discover more classes:

- **Inspect Elements**: Use the browser’s Developer Tools to inspect elements on any ServiceNow page.
- **Review Custom Scripts**: Look at client scripts, UI policies, and other customizations that might add specific classes.
- **Documentation and Community**: Check ServiceNow’s official documentation or community resources for commonly used classes and their purposes.

Summary

ServiceNow uses a variety of classes to style and manage different aspects of the user interface. Inspecting elements directly will give you the most accurate picture of what’s being used in your instance.

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

Hello @Community Alums 

 

I am unable to get this.

 

 

To find the specific location of the class name in your ServiceNow instance, you can search through the following areas:

  • UI macros: Open the UI macro editor and search for the class name in the HTML template or JavaScript code.
  • Script includes: Open the script include editor and search for the class name in the script's code.
  • Custom applications: If you know the name of the custom application, you can search for the class name in the application's UI components or scripts.

SN_Learn
Kilo Patron
Kilo Patron

Hi @imran sk ,

 

Please check the below which will explain it with an example:

 

HTML DOM Document getElementsByClassName() 

Document: getElementsByClassName() method 

 

 

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Brad Bowman
Kilo Patron
Kilo Patron

What is more important to realize is that this is DOM manipulation, and it is not supported or recommended by ServiceNow, using it will cause an appearance on Health Scan / Impact reports as an at risk finding, and it will not work in Service Portal / ESC / workspaces.  In this case their are multiple alternatives for achieving the same:

 

1) If using Service Portal... only (not the native UI Service Catalog) there is a checkbox in the Portal Settings tab of the Catalog Item to make attachments mandatory.

 

2) Consider using a variable with the type of Attachment that you can easily make mandatory always, or under certain conditions.

 

3) For the most robust solution, enabling you to also require a certain number, filename, and/or file extension on attachments, use an onSubmit Catalog Client Script which calls a Script Include using GlideAjax.  This example just checks the minimum - is there an attachment for the request that's about to be submitted.

function onSubmit() {
	var itemsysid = g_form.getValue('sysparm_item_guid');
	var ga = new GlideAjax('CheckAttachment');
	ga.addParam('sysparm_name', 'attached');
	ga.addParam('sysparm_sysid', itemsysid);
	ga.addParam('sysparm_tablename', 'sc_cart_item');
	ga.getXMLWait();

	var answer = ga.getAnswer();
	answer = answer.evalJSON(); //Transform the JSON string to an object

	if(answer.attached == 'false') {
		alert("You must have an attachment to submit this request.");
		return false;
	}
}

Client callable Script Include

var CheckAttachment = Class.create();
CheckAttachment.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	attached: function() {
		var obj = {};  
		obj.attached = 'false';  
		obj.count = 0;  
		var id = this.getParameter('sysparm_sysid');
		var tablename = this.getParameter('sysparm_tablename');   //'sc_cart_item'
		var attachment = new GlideRecord('sys_attachment');
		attachment.addQuery('table_name', tablename);
		attachment.addQuery('table_sys_id',id);
		attachment.query();

		while (attachment.next()) {
			obj.count++;
			obj.attached = 'true';
		}
		var json = new JSON();
		var data = json.encode(obj);//JSON formatted string  

		return data;
	},

	type: 'CheckAttachment'
});