Use case
Customer wants an excel as a attachment in the mail with the list of all users in SNOW who are using the below Dashboards at what time.
- Test-DB
- Test-Backup
Solution
To achive this we will create a scheduled job
-
Navigate to System Defination > scheduled job
-
Click on New Button
-
Select “Automatically run a script your choosing”
- Fillup fileds as required.
Then RECIPENTS(c.mukherjee88@gmail.com) will receive a mail with an excel with below details:
## Another example ( youtube video source code)
Email Attachment
//This will be email Subject
var SUBJECT = "Monthly Reports";
//Recipent list
var RECIPENTS = ["learnnowlab@gmail.com"];
//Mention report columns here
var REPORT_COLUMNS = ["Incident Number", "Short Description", "Assigned To"];
//Name of the CSV
var CSV_NAME = "Incident Tracker.csv";
//Email body static content
var EMAIL_BODY = "Please find attached Incident updated this month";
//Instance URL
var instanceURL = gs.getProperty('glide.servlet.uri');
//SRAPI URL that we will create in later half of this video
var restAPIEndpoint = "api/196834/download_link/reports";
var downloadLink = instanceURL + restAPIEndpoint;
//Inserting record in sys_email table
var mailGr = new GlideRecord('sys_email');
mailGr.initialize();
//email should be send when we are ready.let us pause in the queue
mailGr.type = 'send-ignored';
mailGr.notification_type = 'SMTP';
mailGr.subject = SUBJECT;
mailGr.recipients = RECIPENTS.toString();
mailGr.content_type = 'multipart/mixed';
mailGr.insert();
var csvData = REPORT_COLUMNS.toString() + "\r\n";
var content = '';
//Extracting data from Incident table
var gr = new GlideRecord('incident');
gr.addEncodedQuery("sys_updated_onONThis month@javascript:gs.beginningOfThisMonth()@javascript:gs.endOfThisMonth()");
gr.query();
while (gr.next()) {
content = '';
content = content + gr.number + ",";
content = content + gr.short_description + ",";
//The new line is important it should be added on the last column value
content = content + gr.assigned_to.getDisplayValue() + "\r\n";
csvData = csvData + content;
}
//Inserting in sys_attachement table of mailgr record.
var sa = new GlideSysAttachment();
sa.write(mailGr, CSV_NAME, 'application/csv', csvData);
var attachmentget = new GlideSysAttachment();
var agr = attachmentget.getAttachments('sys_email', mailGr.sys_id);
if (agr.next()) {
//Additionally we have to link the mailgr with an entry in sys_email_attachment
var sysEmailAttachment = new GlideRecord('sys_email_attachment');
sysEmailAttachment.initialize();
sysEmailAttachment.attachment = agr.sys_id;
sysEmailAttachment.file_name = agr.file_name.toString();
sysEmailAttachment.source = 'notification';
sysEmailAttachment.content_disposition = 'attachment';
sysEmailAttachment.email = mailGr.sys_id;
sysEmailAttachment.insert();
//construct download link ( will be used when user click on downlink in email)
downloadLink = downloadLink + "/" + agr.sys_id;
}
//construct email body
mailGr.body = '<html><head></head><body>' + EMAIL_BODY + 'Optionally you can download also from this <a href="'+downloadLink+'">link</a></body></html>';
//All ok now tag email as ready so that notification queue picks up and send email
mailGr.type = 'send-ready';
mailGr.update();
Scripted restApi
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
//Extract the sysid of the attachment from the request url
var attachment_sys_id = request.pathParams.sys_id + '';
//Construct headers
var hdrs = {};
hdrs['Content-Type'] = 'application/octet-stream';
//Get filename from attachment table.This will be the name that user will see when link is clicked
var gr = new GlideRecord('sys_attachment');
gr.addQuery('sys_id', attachment_sys_id);
gr.query();
if (gr.next()) {
hdrs['Content-Disposition'] = 'attachment;filename=' + gr.file_name;
}
response.setStatus(200);
response.setHeaders(hdrs);
//write the content as output stream
var writer = response.getStreamWriter();
var attachmentStream = new GlideSysAttachmentInputStream(attachment_sys_id);
writer.writeStream(attachmentStream);
})(request, response);
- Understanding Request, RITM, Task in ServiceNow
- Steps to create a case in ServiceNow (CSM)
- Performance Analytics in 10 mins
- Event Management in 10 minutes - part1
- Event Management in 10 minutes - part2
- Custom Lookup List
- Script includes in 5 minutes
- Interactive Filter in 5 minutes
- UI Policy in 6 Minutes
- Client Side Script Versus Server Side Script in 3 minutes
-
Snow
- Performance Analytics
- ServiceNow Scripts
- Script include
- Useful scripts
- Basic Glide Scripts
- Client Script
- Advance Glide Script
- Glide System Script
- Admin
- Import Set
- Work Flow
- ACL
- SLA
- Notification
- Core Application
- UI Policy
- UI Action
- Client Script
- CAB Workbech
- Data Policy
- Connect Support
- Catalog
- Discovery
- CSM
- Event Management
- HR
- Integrations
- SSO Integration
- LDAP Integration
- SCCM Integration
- AWS Intergration
- Slack Integration
- CTI Integration
- Jira Integration
- Ebonding ServiceNow
- SOAP Integration
- IBM Netcool Integration
- VIP Mobile App Integration
- Rest Integration
- Service Portal
- Questions
- ACL
- Performance analytics(PA) Interactive Filter
- Various Configurations in Performance analytics(PA)
- Service Portal
- Performance Analytics(PA) Widgets
- Performance Analytics(PA) Indicator
- Performance Analytics(PA) Buckets
- Performance Analytics(PA) Automated Breakdown
- Client Script
- Rest Integration
- Understanding the Request, RITM, Task
- Service Catalogs
- Events in ServiceNow
- Advance glide script in ServiceNow
- CAB Workbench
Comments