I have an existing close ui action..
This ui action closes the task record and update the state as well…now that I want to make attachment mandatory I m using saveAttachment('incident',g_form.getTableName(),g_form.getUniqueValue());
In my close ui action…but there thing I want to check if single attachment is added or not and if present have to update the state on form?
Could anyone please guide me how to proceed with this?
Thank you
To achieve your goal of making an attachment mandatory in a UI Action and updating the state only if an attachment is present, you can follow these steps:
1. Modify the Existing UI Action
You need to enhance your existing UI Action to include logic that:
- Checks if an attachment is present.
- Updates the state of the task record based on the attachment status.
2. Steps to Implement the Changes
1. Add Attachment Check in UI Action Script
First, modify the UI Action script to include logic that checks for attachments before updating the record state.
Here's an example of how you can accomplish this:
// UI Action Script // Ensure the g_form is available if (typeof g_form !== 'undefined') { // Add attachment saveAttachment('incident', g_form.getTableName(), g_form.getUniqueValue()); // Function to check if an attachment is present function hasAttachment() { var attachmentGR = new GlideRecord('sys_attachment'); attachmentGR.addQuery('table_name', g_form.getTableName()); attachmentGR.addQuery('table_sys_id', g_form.getUniqueValue()); attachmentGR.query(); return attachmentGR.hasNext(); } // Check if at least one attachment exists if (hasAttachment()) { // Proceed to update the state of the task record g_form.setValue('state', 'Closed'); // Example of setting the state to 'Closed' // Optionally, you can also use GlideAjax to call a server-side script if more complex logic is required var ga = new GlideAjax('YourServerScript'); ga.addParam('sys_id', g_form.getUniqueValue()); ga.getXMLAnswer(function(response) { // Handle server-side response here if needed }); } else { // Display a message to the user g_form.addErrorMessage('Please attach at least one file before closing the task.'); } }
2. Add Server-side Logic (Optional)
If you need to perform server-side checks or actions, create a Script Include to handle those.
- Create a Script Include:
- Go to System Definition > Script Includes.
- Click on New and define the Script Include. For example:
<code>
var YourServerScript = Class.create(); YourServerScript.prototype = Object.extendsObject(AbstractAjaxProcessor, { checkAttachment: function() { var sys_id = this.getParameter('sys_id'); var attachmentGR = new GlideRecord('sys_attachment'); attachmentGR.addQuery('table_name', 'incident'); attachmentGR.addQuery('table_sys_id', sys_id); attachmentGR.query(); return attachmentGR.hasNext(); } });
- Call the Script Include from the UI Action (if needed):
- Use
GlideAjax
to call the server-side Script Include from the client-side script if you need server-side logic or validations.
- Use
3. Update the UI Action Form
Ensure the UI Action is configured correctly to run client-side or server-side code, based on your needs:
- Go to System Definition > UI Actions.
- Find and edit your existing UI Action.
- Ensure the Script field contains your updated script logic.
- Set the Form Button or Form Context Menu according to where you want this UI Action to be available.
4. Testing
- Attach a File: Test by attaching a file and clicking the UI Action button to ensure that the state updates as expected.
- No Attachment: Test without attaching a file to ensure that the user is prompted correctly.
Summary
- Enhance UI Action Script: Modify your existing UI Action script to include logic that checks for attachments before updating the record state.
- Use Script Include (if needed): Implement a Script Include to perform server-side checks or actions if necessary.
- Configure UI Action: Ensure the UI Action is configured correctly in the system.
- Test Thoroughly: Verify that the changes work as expected under different scenarios.
By following these steps, you can ensure that attachments are properly managed and the task state is updated only when an attachment is present.