Docsity
Docsity

Prepara i tuoi esami
Prepara i tuoi esami

Studia grazie alle numerose risorse presenti su Docsity


Ottieni i punti per scaricare
Ottieni i punti per scaricare

Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium


Guide e consigli
Guide e consigli

titolo del documento, Dispense di Materiali Per Le Costruzioni

descrizione documento, test, test, test,test ,test,test,test,test

Tipologia: Dispense

2020/2021

Caricato il 21/04/2021

mario0256789
mario0256789 🇮🇹

1 documento

1 / 20

Toggle sidebar

Anteprima parziale del testo

Scarica titolo del documento e più Dispense in PDF di Materiali Per Le Costruzioni solo su Docsity! Lab Guide Hack Lab: Attachment Integration APIs Default Login / Password: admin / Knowledge17 itil / Knowledge17 employee / Knowledge17 This Page Intentionally Left Blank 3 © 2014 ServiceNow, Inc. All rights reserved. © ®2016 ServiceNow, Inc. All rights reserved. Lab Goal The Attachment API is documented within the ServiceNow Product Documentation and on the ServiceNow Developer site: • https://docs.servicenow.com/integrate/inbound_rest/concept/c_AttachmentAPI.html • https://developer.servicenow.com/app.do#!/rest_api_doc?v=geneva&to=class__rest_c_attach mentapi__geneva Creating attachments requires several parameters: • file_name – this is the name to give the attachment. This is required. • table_name – this is the table of the record that you want to attach the file to. Example incident. This is required. • table_sys_id – this is the sys_id of the record in the specified table that you want to attach the file to. This is required. • encryption_context – this is the sys_id of an encryption context record to use to encrypt and grant access to the attachment. This is an optional parameter and used only if you are leveraging the encryption support plugin. Configure Postman and Send in an attachment 1. On the right side of the Postman UI, set the method to POST. Lab 2 Inbound Attachments 4 © 2014 ServiceNow, Inc. All rights reserved. © ®2016 ServiceNow, Inc. All rights reserved. 2. Beside POST in the URL box, enter INSTANCE_URL/api/now/attachment/upload Note: Example would be https://INSTANCE-NAME.lab.service- now.com/api/now/attachment/upload 3. Below the word post you will see several tabs to configure Authorization, Headers, and the Body of the Post message. 4. Click Authorization and set the Type to Basic Auth. 5. In the Username field enter admin and in the Password field enter Knowledge17. Note: If you have changed your admin credentials to something else, enter those instead. 6. Click Body. 5 © 2014 ServiceNow, Inc. All rights reserved. © ®2016 ServiceNow, Inc. All rights reserved. 7. Next we need to specify which record in ServiceNow that we want to attach a record to. Remember we need to specify the table name and sys_id of the record as well as a file name. For the purposes of this lab, we will update an out of the box demo incident record INC0000003. This record is in the incident table and the sys_id is e8caedcbc0a80164017df472f39eaed1. Note: You can get any record’s sys_id by clicking Additional Actions and choosing Copy sys_id. 8. Add the following key and value pairs by clicking the word “key” table_name:incident table_sys_id: e8caedcbc0a80164017df472f39eaed1 file_name: choose File to the right 9. Click the Choose Files button and select a file on your machine that you want to upload to the instance. 10. Click Send. 8 © 2014 ServiceNow, Inc. All rights reserved. © ®2016 ServiceNow, Inc. All rights reserved. 4. Beside GET in the URL box, enter INSTANCE_URL/api/now/attachment?sysparm_query=table_sys_id=e8caedcbc0a80164017df47 2f39eaed1 Note: Example would be https://INSTANCE-NAME.lab.service- now.com/api/now/attachment?sysparm_query=table_sys_id=e8caedcbc0a80164017df472f39e aed1 5. Below the word Get you will see several tabs to configure Authorization, Headers, and the Body of the Post message. 6. Click Authorization and set the Type to Basic Auth. 7. In the Username field enter admin and in the Password field enter Knowledge17. Note: If you have changed your admin credentials to something else, enter those instead. 8. Click Send. 9 © 2014 ServiceNow, Inc. All rights reserved. © ®2016 ServiceNow, Inc. All rights reserved. 9. At the bottom of the Postman screen you should get a response back from your instance. If everything worked you should get a Status: 200 OK and a JSON string with details on the attachments in ServiceNow. Your results should only include the 1 attachment, but the below screenshot shows an example of multiple attachments. 10. Highlight the sys_id value in your JSON string and copy it into your clipboard as we will use that in the next query. Query for single attachment metadata 1. In Postman click the + symbol to open a new tab. 2. GET should be set by default, if not select that method. 10 © 2014 ServiceNow, Inc. All rights reserved. © ®2016 ServiceNow, Inc. All rights reserved. 3. Beside GET in the URL box, enter the following. The sys_id at the end is the sys_id from step 10 in the previous exercise. INSTANCE_URL/api/now/attachment/3ca4940f4f96560030505f211310c78c Note: Example would be https://INSTANCE-NAME.lab.service- now.com/api/now/attachment/3ca4940f4f96560030505f211310c78c 4. Below the word Get you will see several tabs to configure Authorization, Headers, and the Body of the Post message. 5. Click Authorization and set the Type to Basic Auth. 6. In the Username field enter admin and in the Password field enter Knowledge17. Note: If you have changed your admin credentials to something else, enter those instead. 7. Click Send. 8. At the bottom of the Postman screen you should get a response back from your instance. If everything worked you should get a Status: 200 OK and a JSON string with details on the specific attachment in ServiceNow. Query for single attachment binary 1. In Postman click the + symbol to open a new tab. 2. GET should be set by default, if not select that method. 3. Go back to your previous tab from the last exercise and copy the Get URL. We will use it for this exercise but modify it slightly. 4. Beside GET in the URL box, enter INSTANCE_URL/api/now/attachment/3ca4940f4f96560030505f211310c78c/file Note: Example would be https://INSTANCE-NAME.lab.service- now.com/api/now/attachment/3ca4940f4f96560030505f211310c78c/file 5. Below the word Get you will see several tabs to configure Authorization, Headers, and the Body of the Post message. 6. Click Authorization and set the Type to Basic Auth. 13 © 2014 ServiceNow, Inc. All rights reserved. © ®2016 ServiceNow, Inc. All rights reserved. 2. Set the following values: Name: Incident Integration with Attachments Table: Incident Advanced: checked When: after Update: checked Filter Conditions: Category is Network and Assignment Group is Network Script: (function executeRule(current, previous /*null when async*/) { var targetInstanceURL = gs.getProperty("glide.servlet.uri"); var targetUserID = "admin"; var targetUserPassword = "Knowledge17"; var answer = ""; var attachmentMsg = ""; //Create a JSON string with the current incident attributes var incidentJSON = {}; incidentJSON.caller_id = current.caller_id.toString(); incidentJSON.category = current.category.toString(); incidentJSON.impact = current.impact.toString(); incidentJSON.urgency = current.urgency.toString(); incidentJSON.priority = current.priority.toString(); incidentJSON.short_description = current.short_description.toString(); // Set the Correlation fields to link the new incident with the source incident incidentJSON.correlation_display = "ServiceNow"; incidentJSON.correlation_id = current.sys_id.toString(); // Encode JSON string var requestBody = new global.JSON().encode(incidentJSON); // Create incident in target instance via REST var restMessage = new sn_ws.RESTMessageV2(); restMessage.setHttpMethod("post"); restMessage.setBasicAuth(targetUserID, targetUserPassword); restMessage.setEndpoint(targetInstanceURL + "api/now/table/incident"); restMessage.setRequestHeader("Content-Type", "application/json"); restMessage.setRequestBody(requestBody); var response = restMessage.execute(); var responseBody = response.getBody(); var httpStatus = response.getStatusCode(); if (httpStatus.toString() == "201") { 14 © 2014 ServiceNow, Inc. All rights reserved. © ®2016 ServiceNow, Inc. All rights reserved. answer = "Incident successfully sent."; // Get target record's SysID var parser = new JSONParser(); var parsed = parser.parse(responseBody); var targetRec = parsed.result; var attachmentCount = sendAttachments(current.getTableName(), current.sys_id, targetRec.sys_id); if (attachmentCount != "none") { attachmentMsg = " Attachments successfully sent: " + attachmentCount[0].toString() + ". Attachments failed to be sent: " + attachmentCount[1].toString(); } else { attachmentMsg = " Record had no attachments to send."; } } else { answer = "Incident could not be sent."; } answer = answer + attachmentMsg; // Set message at top of screen with results. gs.addInfoMessage(answer); function sendAttachments(sourceTable, sourceID, targetID) { var answer = [0, 0]; //successful attachments, failed attachments // Query for any attachments on the current record. var attachmentRec = new GlideRecord("sys_attachment"); attachmentRec.addQuery("table_sys_id", sourceID); attachmentRec.addQuery("table_name", sourceTable); attachmentRec.query(); if (attachmentRec.hasNext()) { while (attachmentRec.next()) { var attachmentMessage = new sn_ws.RESTMessageV2(); attachmentMessage.setHttpMethod("post"); attachmentMessage.setBasicAuth(targetUserID, targetUserPassword); attachmentMessage.setEndpoint(targetInstanceURL + "api/now/attachment/file"); attachmentMessage.setQueryParameter("table_name", attachmentRec.table_name); attachmentMessage.setQueryParameter("table_sys_id", targetID); attachmentMessage.setQueryParameter("file_name", attachmentRec.file_name); attachmentMessage.setRequestHeader("Content-Type", attachmentRec.content_type); attachmentMessage.setRequestHeader("Accept", "application/json"); 15 © 2014 ServiceNow, Inc. All rights reserved. © ®2016 ServiceNow, Inc. All rights reserved. attachmentMessage.setRequestBodyFromAttachment(attachmentRec.sys_id); var response = attachmentMessage.execute(); var responseBody = response.getBody(); var httpStatus = response.getStatusCode(); if (httpStatus.toString() == "201") { answer[0] += 1; } else { answer[1] += 1; } } } else { answer = "none"; } return answer; } })(current, previous); 3. Click Submit. 4. Now let’s test! Navigate to Incident\Open and search for Number = INC0000003 Note: This incident meets the conditions of our business rule where Category is Network and Assignment Group is Network. After an update, this incident should automatically create a new incident in your instance via the REST API along with the attachment. 5. Type anything into the Additional comments and click Update. 6. After update, you should receive a message that the incident was sent: Lab Success Verification 1. In the Filter navigator box, type incident. 2. Click Open under the Incident application. 3. Sort the list in descending order by Number. You should find an incident at the top of the list that matches INC0000003. Short Description: Wireless access is down in my area, Caller: Joe Employee, etc. Open this record.
Docsity logo


Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved