Attachments Archives - ServiceNow Guru https://servicenowguru.com/tag/attachments/ ServiceNow Consulting Scripting Administration Development Tue, 28 May 2024 21:27:38 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.2 https://servicenowguru.com/wp-content/uploads/2024/05/cropped-SNGuru-Icon-32x32.png Attachments Archives - ServiceNow Guru https://servicenowguru.com/tag/attachments/ 32 32 Create an Attachment UI Action https://servicenowguru.com/ui-actions-system-ui/create-attachment-ui-action/ https://servicenowguru.com/ui-actions-system-ui/create-attachment-ui-action/#comments Mon, 19 Sep 2011 11:41:02 +0000 https://servicenowguru.wpengine.com/?p=4035 This post shows how you can easily create an attachment UI action for any form. The attachment capability is part of all forms in ServiceNow and is accessed via a paperclip icon in the top-right corner of the form. In some cases this icon may not be prominent enough for your end users. The simplest

The post Create an Attachment UI Action appeared first on ServiceNow Guru.

]]>
This post shows how you can easily create an attachment UI action for any form. The attachment capability is part of all forms in ServiceNow and is accessed via a paperclip icon in the top-right corner of the form. In some cases this icon may not be prominent enough for your end users. The simplest solution in that case is to set up a UI action button or link that performs the same function.


For this solution we’ll leverage the existing ‘saveAttachment’ function. This function requires a table name and a record sys_id value. The ‘getTableName’ and ‘getUniqueValue‘ client-side functions will help to identify those values. You can add this capability by creating a UI action that looks like this…

‘Save Attachment’ UI Action
Name: Save Attachment
Table: Any table of your choice (Probably Task)
Form Button: True
Client: True
Onclick:

saveAttachment(g_form.getTableName(), g_form.getUniqueValue());

The post Create an Attachment UI Action appeared first on ServiceNow Guru.

]]>
https://servicenowguru.com/ui-actions-system-ui/create-attachment-ui-action/feed/ 11
Defined Related Lists https://servicenowguru.com/relationships/defined-related-lists/ https://servicenowguru.com/relationships/defined-related-lists/#comments Fri, 30 Jul 2010 11:10:35 +0000 https://servicenowguru.wpengine.com/?p=1862 Defined Related Lists can be a very simple and useful tool to provide users with information in a related list directly on a form (even if that information is not directly associated with the record being viewed). The Service-now wiki contains documentation on this topic so I won’t cover that here. The point of this

The post Defined Related Lists appeared first on ServiceNow Guru.

]]>
Defined Related Lists can be a very simple and useful tool to provide users with information in a related list directly on a form (even if that information is not directly associated with the record being viewed). The Service-now wiki contains documentation on this topic so I won’t cover that here. The point of this article is to point out something that often gets overlooked when working with Defined Related Lists and to share a few Defined Related Lists that I’ve used in the past.

One of the familiar defined related lists is the ‘Incidents by Same Caller’ list that you can display at the bottom of your incident form. The name says it all, but it looks something like this…

If you navigate to ‘System Definition -> Relationships’ and open up the ‘Incidents by Same Caller’ relationship record you’ll see that it uses the following query to produce the resultant related list…

current.addQuery('caller_id', parent.caller_id);

But what happens when we set the Caller field with an empty value?

The image above shows that you’ll get exactly what you’re supposed to get, but probably not what you want. Depending on your setup, there might not be a whole lot of value in showing a list of incidents with no caller…at least at the bottom of your incident form. The solution to this is to filter out any records where the field you are querying is empty. In this case, we want to filter out any records where the Caller field is empty like this…

current.addQuery('caller_id', parent.caller_id);
current.addNotNullQuery('caller_id');

Adding the ‘addNotNullQuery’ line gives us the result we would expect at the bottom of our incident form.

Here are a few other examples of defined related lists I have implemented in the past. Please share any defined related lists you have come up with by commenting on this post below!

Approval – Affected CIs
–Shows a list of Affected CIs for the task being approved at the bottom of an approval form.
Name: Affected CIs
Applies to table: Approval (sysapproval_approver)
Queries from table: CIs Affected (task_ci)
Query with:

current.addQuery('task', parent.sysapproval);
current.addNotNullQuery('task');
All Request Attachments
–Shows a list of attachments from the parent request, request item, and catalog task records on a catalog task form.
Name: All Attachments
Applies to table: Catalog task (sc_task)
Queries from table: Attachment (sys_attachment)
Query with:

var qc = current.addQuery('table_sys_id', parent.sys_id);
qc.addOrCondition('table_sys_id', parent.request_item.sys_id);
qc.addOrCondition('table_sys_id', parent.request_item.request.sys_id);
current.addNotNullQuery('table_sys_id');
All Request task and Approval Attachments
–Shows a list of attachments from the parent request, request item, catalog task, and approval records on a request item form.
Name: All Attachments
Applies to table: Requested item (sc_req_item)
Queries from table: Attachment (sys_attachment)
Query with:

//Current record Attachments
var qc = current.addQuery('table_sys_id', parent.sys_id);//Request Attachments
qc.addOrCondition('table_sys_id', parent.request.sys_id);

//Catalog Task Attachments
var tsk = new GlideRecord('sc_task');
tsk.addQuery('request_item', parent.sys_id);
tsk.query();
var tskIDArr = [];
while(tsk.next()){
tskIDArr.push(tsk.sys_id.toString());
}
var tskIDStr = tskIDArr.join();
qc.addOrCondition('table_sys_id', 'IN', tskIDStr);

//Approval Attachments
var app = new GlideRecord('sysapproval_approver');
app.addQuery('document_id', parent.sys_id);
app.query();
var appIDArr = [];
while(app.next()){
appIDArr.push(app.sys_id.toString());
}
var appIDStr = appIDArr.join();
qc.addOrCondition('table_sys_id', 'IN', appIDStr);

//Do not include attachments not associated with a record
current.addNotNullQuery('table_sys_id');
Approval Task Attachments
–Shows a list of attachments from the associated approval task(s) on an approval form. In addition to displaying attachments for the specific task, this also shows attachments from the parent change request (for change tasks), the parent request item and request (for catalog tasks), the parent request (for catalog items), and the associated child items (for catalog requests).
Name: Approval Task Attachments
Applies to table: Approval (sysapproval_approver)
Queries from table: Attachment (sys_attachment)
Query with:

//Add attachments for the approval task
current.addNotNullQuery('table_sys_id');
var qc = current.addQuery('table_sys_id', parent.sysapproval);//Add parent task attachments based on the approval task type
var appTask = '';
if(parent.sysapproval.sys_class_name){
appTask = parent.sysapproval.sys_class_name;
}
if(appTask != ''){
var tsk = new GlideRecord(appTask);
tsk.get(parent.sysapproval);

//Check for approval task type and add necessary queries
if(appTask == 'change_task'){
//Query for attachments from the associated change
qc.addOrCondition('table_sys_id', tsk.change_request);
}
else if(appTask == 'sc_task'){
//Query for attachments from the associated item and request
qc.addOrCondition('table_sys_id', tsk.request_item);
qc.addOrCondition('table_sys_id', tsk.request_item.request);
}
else if(appTask == 'sc_req_item'){
//Query for attachments from the associated request
qc.addOrCondition('table_sys_id', tsk.request);
}
else if(appTask == 'sc_request'){
//Set up a variable to store item sys_ids
var items = '';
//Query for attachments from the associated request items
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request', parent.sysapproval);
ritm.query();
while(ritm.next()){
items = items + ',' + ritm.sys_id.toString();
}
if(items != ''){
qc.addOrCondition('table_sys_id', 'IN', items);
}
}
}

The post Defined Related Lists appeared first on ServiceNow Guru.

]]>
https://servicenowguru.com/relationships/defined-related-lists/feed/ 39
Download Attachments as a ZIP File https://servicenowguru.com/scripting/download-attachments-zip-file/ https://servicenowguru.com/scripting/download-attachments-zip-file/#comments Wed, 21 Apr 2010 04:33:58 +0000 https://servicenowguru.wpengine.com/?p=1595 I am really enjoying being here at Knowledge 10 and meeting so many of you who read our posts.  I have been busy in one-on-one sessions hearing requests and helping come up with answers.  One of our readers wanted to know how they would go about downloading all of the attachments on a record as

The post Download Attachments as a ZIP File appeared first on ServiceNow Guru.

]]>
I am really enjoying being here at Knowledge 10 and meeting so many of you who read our posts.  I have been busy in one-on-one sessions hearing requests and helping come up with answers.  One of our readers wanted to know how they would go about downloading all of the attachments on a record as a zip file.  Another reader wanted to know how to send attachments as a zip file via a web service.  I believe this post should help solve both questions.

The challenge we will solve in this post is this: if a record has any attachments, we will add a button to the form that allows you to download all of the attachments as a single ZIP file.

Step 1: Create a UI action on the task table

Name:Save Attachments as ZIP
Condition:

current.hasAttachments();

Script:

action.setRedirectURL('exportAttachmentsToZip.do?sysparm_sys_id=' + current.sys_id + '&sysparm_table=' + current.getTableName());

The only real secret to the above code is the setRedirectURL. This allows us to call a processor in our system that will popup a download dialog to download the zip file. The exportAttachmentsToZip processor does not yet exist in the system and will need to be created. Processors are found under System Definition->Processors.

Step 2: Create an exportAttachmentsToZip processor
Create a new processor. Processors are found under System Definition->Processors.
Name: exportAttachmentsToZip
Type: script
Path: exportAttachmentsToZip
Script:

var sysid = g_request.getParameter('sysparm_sys_id');
var table = g_request.getParameter('sysparm_table');var theRecord = new GlideRecord(table);
theRecord.addQuery('sys_id', sysid);
theRecord.query();
theRecord.next();

var zipName = 'attachments.zip';

var StringUtil = GlideStringUtil;

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', theRecord.sys_id);
gr.addQuery('table_name', theRecord.getTableName());
gr.query();

if (gr.hasNext()){
g_response.setHeader('Pragma', 'public');
g_response.addHeader('Cache-Control', 'max-age=0');
g_response.setContentType('application/octet-stream');
g_response.addHeader('Content-Disposition', 'attachment;filename=' + zipName);
var out = new Packages.java.util.zip.ZipOutputStream(g_response.getOutputStream());
var count=0;
while (gr.next()){
var sa = new GlideSysAttachment();
var binData = sa.getBytes(gr);

var file = gr.file_name;
addBytesToZip(out, zipName, file, binData);
count ++;
}
// Complete the ZIP file
out.close();
}

function addBytesToZip (out, dir, file, stream){
// Add ZIP entry to output stream.
out.putNextEntry(new Packages.java.util.zip.ZipEntry(file));
out.write(stream, 0, stream.length);
out.closeEntry();
}

That’s all there is to it. The result will appear like the following screen shot.

This code could be slightly tweaked to send the ZIP file as a web service. Remember, though, that if you do this, you will need to base64 encode the binary data before sending it over.

The post Download Attachments as a ZIP File appeared first on ServiceNow Guru.

]]>
https://servicenowguru.com/scripting/download-attachments-zip-file/feed/ 30
Sending attachments to a 3rd-party service desk https://servicenowguru.com/integration/sending-attachments-3rdparty-service-desk/ https://servicenowguru.com/integration/sending-attachments-3rdparty-service-desk/#comments Wed, 14 Apr 2010 22:47:33 +0000 https://servicenowguru.wpengine.com/?p=1529 I often hear requests for this to be bidirectional. Sending attachments from Service-now to a third-party system isn’t something that we’ve actually implemented in the past. However, with the number of people asking for it lately, I decided to write up a solution. Fetch Attachment as Base64 Encoded Data The following code is assumed to

The post Sending attachments to a 3rd-party service desk appeared first on ServiceNow Guru.

]]>
I often hear requests for this to be bidirectional. Sending attachments from Service-now to a third-party system isn’t something that we’ve actually implemented in the past. However, with the number of people asking for it lately, I decided to write up a solution.

Fetch Attachment as Base64 Encoded Data

The following code is assumed to be within a business rule on the task table (or a table that inherits from task, such as incident, problem, change, etc).

var StringUtil = Packages.com.glide.util.StringUtil;

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', current.sys_id);
gr.addQuery('table_name', current.getTableName());
gr.addQuery('file_name', &'truck7.jpg');
gr.query();

if (gr.next()){
var sa = new Packages.com.glide.ui.SysAttachment();
var binData = sa.getBytes(gr);
var encData = StringUtil.base64Encode(binData);
}
else{
gs.log('record not found');
}

The above code is where the magic happens. The attachment to the incident is retrieved from the Service-now database and is now in base64 encoded data, ready to be sent to the integrating party via web services.

Send Attachment Data via SOAP

The code for sending the encoded data to a 3rd-party system might look something like this:

var envelope = new SOAPEnvelope();
envelope.createNameSpace('xmlns:imp', 'http://www.abcCompany.com/soap');
envelope.setFunctionName('imp:insert');
envelope.addFunctionParameter('uid', current.correlation_id);
envelope.addFunctionParameter('content', encData);
envelope.addFunctionParameter('content_type', gr.content_type);
var request = new SOAPRequest('http://www.abcCompany.com/addAttachment', 'user','pass');
var res = request.post(envelope);

Fetch Attachment Data – Alternate Method

You very well may want to have the business rule run on the sys_attachment table instead of the TASK table. If this is the case, your business rule will look like the following:

var StringUtil = Packages.com.glide.util.StringUtil;

var sa = new Packages.com.glide.ui.SysAttachment();
var binData = sa.getBytes(current);
var encData = StringUtil.base64Encode(binData);

The post Sending attachments to a 3rd-party service desk appeared first on ServiceNow Guru.

]]>
https://servicenowguru.com/integration/sending-attachments-3rdparty-service-desk/feed/ 14