attachments mandatory based on the Short Description of a Task

Dileep Raju
Giga Guru

attachments mandatory based on the Short Description of a Task and only for one task

6 REPLIES 6

Gaurav Rathaur
Kilo Guru

Hi @Dileep Raju ,

 

To make attachments mandatory for a specific task based on the Short Description, you'll need to use a combination of a UI Policy or Client Script and a Business Rule to enforce this. Here's a straightforward approach using a Client Script:

Client Script: Mandatory Attachments Based on Short Description (Catalog Task)

  1. Create a Client Script

    • Navigate to System Definition → Client Scripts.

    • Create a new onSubmit Client Script.

Script Configuration:

  • Table: Task (or sc_task if it's a catalog task)

  • Type: onSubmit

(function executeOnSubmit() {
    // Define the short description that requires attachments
    var requiredShortDescription = "Specific Task Description";  // Replace this with the exact short description

    // Get the short description from the form
    var shortDescription = g_form.getValue('short_description');

    // Check if the short description matches
    if (shortDescription === requiredShortDescription) {
        // Check for attachments
        var attachments = g_form.getAttachments();

        if (attachments.length === 0) {
            alert("Attachments are required for this task.");
            return false;
        }
    }

    // Allow form submission
    return true;
})();

 

How It Works:

  • Trigger Condition: It checks if the short description matches the specified value.

  • Attachment Check: If the task matches the required short description, it checks for attachments.

  • Block Submission: If no attachments are found, it shows an alert and prevents form submission.

If my response was helpful, please mark it as the correct answer and close the thread. This will help others who come across the same issue.

Thanks!
Gaurav Rathaur

Hi @Gaurav Rathaur 


Greetings of the day,


I am getting error when i try to close task after attached attachment
g_form.getAttachments is not a function:
. Could you please help me with this

@Dileep Raju 

 

You can try this:

 

1. Create a UI script called: "setAttachmentMandatory". Set UI Type to "All".

Put this in the script section.

 

(function() {
    "use strict";

    return {

        setAttachmentMandatory: function(isMandatory) {
            var isSuccess;
            try {
                angular.element("#sc_cat_item").scope().c.data.sc_cat_item.mandatory_attachment = isMandatory;
                isSuccess = true;
            } catch (e) {
                isSuccess = false;
                //gs.log('setAttachmentMandatory() failed: ' + e);
            }

            return isSuccess;
        },

        type: "AttachmentUtil"
    };
})();

 

2. Then Client script... and add this script:

Make sure to check the "isolate script" and set UI Type to "All"

 

function onSubmit() {
    var requiredShortDescription = "Specific Task Description";  // Replace this with your actual description
    var shortDescription = g_form.getValue('short_description');

    // Only run attachment logic if short description matches
    if (shortDescription === requiredShortDescription) {
        if (g_form.getValue('test') === 'Yes') {
            g_ui_scripts.getUIScript('setAttachmentMandatory').then(function (script) {
                script.setAttachmentMandatory(true);
            });
        } else {
            g_ui_scripts.getUIScript('setAttachmentMandatory').then(function (script) {
                script.setAttachmentMandatory(false);
            });
        }
    }

    return true;
}

Hi @Dileep Raju , 

 

you should use the GlideAttachment API to verify attachments.

 


If my response was helpful, please mark it as the correct answer and close the thread. This will help others who come across the same issue.

Thanks!
Gaurav Rathaur