Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get the attachments from the parent case table to the case task.

Supriya KM
Tera Contributor

Hello Everyone,

I have a case table where we can add the attachments and also in the related list of case table I have case task, when I open that I want to add the attachments from the case task so that I can get the attachments which are there in the parent case table. This I should get by clicking on the button which is UI action.

Can anyone help me in achieving this.

3 REPLIES 3

Syedmd08
Kilo Guru

Yes, I can help you with that. Here are the steps to achieve this in ServiceNow:

  1. Create a UI Action:

Go to the Case Task table in ServiceNow and create a new UI action. Set the following values:

  • Name: Add Attachment
  • Table: Task
  • Action name: add_attachment
  1. Define the UI Action Script:

Next, add the following code to the UI Action script:

 

(function() {
var ga = new GlideAjax('AttachmentUploader');
ga.addParam('sysparm_name', 'addAttachment');
ga.addParam('sysparm_table_name', 'task');
ga.addParam('sysparm_table_sys_id', g_form.getUniqueValue());
ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
ga.addParam('sysparm_target', 'case');
ga.addParam('sysparm_target_sys_id', g_form.getValue('parent'));
ga.addParam('sysparm_nostack', 'yes');
ga.addParam('sysparm_value', 'test');
ga.getXML();
})();

 

  1. Save the UI Action:

Save the UI Action after adding the script.

  1. Test the UI Action:

Test the UI action by navigating to a Case Task record and clicking on the "Add Attachment" button. The attachment should now be added to the parent Case record's attachments list.

That's it! You have now successfully added a UI action to add attachments from a Case Task record to the parent Case record in ServiceNow.

 

Please mark my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hello Syed, 

Thanks for the quick response.

I didn't find AttachmentUploader in OOB script include, can you please help me with that

Syedmd08
Kilo Guru

The AttachmentUploader script include is not an out-of-the-box script include in ServiceNow. Instead, it is a custom script include that needs to be created in your ServiceNow instance.

Here are the steps to create the AttachmentUploader script include:

  1. Navigate to System Definition > Script Includes in the left navigation pane.

  2. Click the New button to create a new script include.

  3. Set the following values for the new script include:

    • Name: AttachmentUploader
    • Application: Global
    • Accessible from: All application scopes
  4. Add the following code to the script include:

var AttachmentUploader = Class.create();
AttachmentUploader.prototype = {
initialize: function() {
},

addAttachment: function(table_name, table_sys_id, sys_id, target, target_sys_id, value) {
var dialogClass = GlideModal ? GlideModal : GlideDialogWindow;
var attachmentDialog = new dialogClass('attachment_select', false, 600);
attachmentDialog.setTitle('Add Attachment');
var view = attachmentDialog.getView('sys_attachment', 'select_file');
view.setAttribute('table_name', table_name);
view.setAttribute('table_sys_id', table_sys_id);
view.setAttribute('sys_id', sys_id);
view.setAttribute('target', target);
view.setAttribute('target_sys_id', target_sys_id);
view.setAttribute('value', value);
attachmentDialog.render();
},

type: 'AttachmentUploader'

 

  1. Save the script include.

Once you have created the AttachmentUploader script include, you can use the UI action script that I provided earlier to add attachments from a Case Task to the parent Case record in ServiceNow.

 

Please mark my reply as Helpful and/or Accept Solution, if applicable. Thanks!


};