- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 09-09-2022 08:29 AM
1. Create Folder
2. Copy Attachment to Drive
3. Copy Drive Files
Pre-Requisite:
- Knowledge of Flow Designer and Actions is required
Set up the Google Drive Spoke : Set Up Google Drive Spoke
Once you install the spoke, you will be able to view all the available Actions under the Google Drive Spoke Application. Details and the functionalities of these actions can be found under the link : Google Drive Spoke
-> Set Up Actions as per requirement: (Have included only those steps which requires modifications)
a. Create Folder
This Action helps you to create a Folder on your Google Drive.
- Inputs: Used to define a name and description for your new folder. The Parent Folder ID attribute is used to provide a particular destination if required. The ID typically looks like this “https://drive.google.com/files/d/<Folder ID>/view”
- Create Folder: This is the crucial part of the set up. The Out of Box set up can remain same.
An important catch! Suppose the requirement is to use Shared Drives as Destination to create folders, the Out of the Box settings will return you an error as “Provided Folder ID not found”. Solution: Using the Google Developer APIs, (https://developers.google.com/drive/api/v3/reference/files/ ) we need to add “supportsAllDrives=true” attribute in the Resource Path to enable this Action to read Shared Drives and Shared Folders. After adding, it should look like this:
Remember to use this attribute every time you want to work with Shared Folders/Shared Drives
3. Outputs: This is where the Action will return you the Folder ID of the same folder it created on the Google Drive. This ID can then be later used if you want to reference the folder further in flow.
b. Copy Attachments to Drive
With the help of this Action, you can copy all the physical attachments that you have on your record in your Google drive or to a specific folder on your drive.
- Inputs: In this section you need to provide the Sys ID of the attachment record which need to be copied over the Drive. The second field, Parent Folder ID is the Destination where you want to copy the attachment (the alpha-numeric part of the URL ) “https://drive.google.com/files/d/<Folder ID>/view”
- Copy Attachment to Drive: Like the Copy Folder Action, if you are using a Shared folder, do not forget to add the “supportsAllDrives=true” attribute. If this action is required on your own drive, you can skip this.
- Add Metadata to File: Ensure to use the Patch HTTP method in the step. The Shared file attribute needs to be used here as well.
- Outputs: The step will return you the File ID and File URL that was created when the Attachment from ServiceNow was copied to the Drive.
c. Copy Drive Files
This action helps you create a copy of an existing Google drive file/document and copy it to a target drive or folder.
- Inputs: In this section you need to provide the File ID of the desired file so that it gets copied to the Destination you select. (Hint: lookout for the alpha numeric characters between files/d/<File ID>/view or /d/<File ID>/edit). You can also provide a name for your file once it gets copied over.
- Copy Drive Files: Ensure using the Shared Drive attribute. After modifying, it should look like this
- Outputs: This will return the new File ID once it is present in the Specified folder.
Note: Once you set up an Action, do not forget to Test it before you publish it.
-> Use the Actions in your Flow
- Create a new Flow. We will take an example of Requested Item Table. Provide a trigger condition
- Table = Requested Item (sc_req_item)
- Record is created
- Actions:
- Use Look Up Attachments to check if the record has attachments or not. This will return you sys ID of the attachment.
- If attachment exists, use the Create Folder Action for folder creation. Provide the Parent Folder ID and Name of the Folder. In this case, the RITM’s number field data picker was used for the name of the folder. Also, we used a shared folder’s ID in the Parent Folder ID field.
- Use Get Attachments on Record to get the Sys ID of all the attachments present on the RITM.
- Use For Each condition to read all your attachments. Use the Output (Attachments - Record) of the Get Attachments on Record action as Data picker here.
- Use the Copy Attachments to Drive action after For Each.
- Attachment record: Use the Output of For Each -> Attachment Record -> Sys ID
- Parent Folder ID: Use the Output of Create Folder -> Folder ID
- File Name: Use the Output of For Each -> Attachment Record -> Name
- Next steps…
For the catalog item of the RITM, we created a Multi-Row Variable set (MVRS) with two fields (document_url and document_name) where users can enter multiple Google Drive links which they want to be available in the folder. From the entire link, we only need the alphanumerical value as the File ID to process this action. Below are the steps to retrieve it.
- Use the Get Catalog Variable Provide the record from the data pill picker
- Next, provide which variable you want to use (where the links are stored)
- Since the MVRS stores data in JSON format, we need to parse this variable.
- Use For Each action to read all the elements of the MVRS. Use the Output (Array.Object type) of Get Catalog Variable in the data pill picker.
- In the File ID input of Copy Drive Files, provide the below script
var json_document = fd_data.trigger.current.variables.<your_variable>;
//gs.info("my variable" + doc);
var folder_id = ((JSON.parse(json_document))[0].document_url).split('/d/').pop();
const folder_id_FINAL = folder_id.substring(0,folder_id.indexOf('/')) || folder_id;
return folder_id_FINAL ;
This script helps you to retrieve the alpha-numerical ID from the entire URL. (Suggestions to improve this script are welcome 😊)
- In the File name field below script can be used
var doc = fd_data.trigger.current.variables.<your_variable>;
//gs.info("my variable" + doc);
var folder = JSON.parse(doc);
var folder_name = folder[0].document_name;
return folder_name ;
- Provide Output of the Create Folder Action to the Parent Folder ID field.
After you set up the flow, test the flow by providing a particular RITM record. Once you see that all the steps of the flow are running without error, you can activate it.
Common issues that might be encountered:
- Provided file or folder does not exist / Error 404.
Solution: Check if the Folder ID is correct. Check if your shared drive attribute is enabled (if you want to use Shared Drives or Folders).
- Invalid Token
Solution: Check if your Credential is returning the correct token. Use the Get OAuth Token Related link to identify the correct token. - Error 405 / method not allowed
Solution: This occurs if you try to access a shared drive without enabling the “supportsAllDrives=true” attribute.
- Error during Copy Drive File.
Solution: Maybe your script is not able to fetch the folder ID from the provided google drive link. You can try your own scripting knowledge to retrieve the Folder ID from the URL if it has a fixed format.
Once I get an opportunity to work on other actions of the Google Drive Spoke, I will share my insights on them as well !
- 1,605 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Good work and great article @Swati Mishra
I had similar requirement and done the same more with G-Spoke's action itself.
I am trying to integrate this by getting inputs from Catalog Item to make it more dynamic.
Many thanks for this article.