Back up and restore records and comments

Introduction

Kintone APIs can be used to backup and restore both record data and comment data. By storing a backup of an App's data on a local computer or storage device, an App can be restored to a previous generation of data in the case of accidental modification or deletion.

Example of the Final Result

Using the sample code provided, the records and comments of an App can be backed up and then restored in a new App. The following shows an example of the data in its original App and after being restored in a new App.

App to Backup

Record List

The records to be backed up shown in the record list view.

US_01.png

Record Details

One of the records to be backed up shown in the record details view.

US_02.png

App with Restored Data

Record List

The restored records in a new App.

US_03.png

Record Details

The previously backed up record and comments restored in the new App.

US_04.png

 

Implement the Solution

Prepare the App for Backup

Save the following sample code as sample_1.js.

 

 

Upload the sample code sample_1.js to the JavaScript and CSS Customization settings of the App to backup as shown below. Refer to the Customizing an App with JavaScript and CSS article in the Kintone Help Center for details on how to upload JavaScript and CSS files to an App.

 

US_07.png

Run the Backup of the Records

Click the backup button on the Record List page to download backup data of both the records and comments. Two files will be downloaded; one for the records, and one for the comments.

 

US_08.png

Create the App to Restore the Data to

Create an App with the same form and settings as the original App by duplicating the original App. Refer to the Creating a New App by Duplicating an Existing App (Copying an App) article from the Kintone Help Center for details.

Create a Custom View

In the duplicated App, create a form to load the backup file. To do so, first create a custom view in the App Settings. Refer to the Create a Custom View article for details. In the HTML Code field add the following HTML sample code. Pagination does not need to be enabled and Filter and Sort settings may be left as is.

 

Add the JavaScript Customization

Save the following sample code as sample_2.js.

 

 

Upload the sample code sample_2.js and the jQuery file (this article uses jQuery version 3.2.0) found in the jQuery CDN to the App to restore records and comments to in the following order. Make sure to save JavaScript and CSS Customization settings and update the App.

js_css_files.png

Restore the Data

Navigate to the record list screen and open the custom view created previously.

Click Choose File under Records to restore and Comments to restore and select the downloaded backup data of the records and comments respectfully. When the data has been loaded, the contents of the files will be displayed in the text boxes as follows.

 

US_10.png

When both records and comments are loaded, clicking the Restore! button will load the data into the App. Return to the Record List View to confirm the restoration.

Notes on Restoring Comments

1. It is necessary to know the record number of the newly created record

A new record number is assigned to records restored by API, so the record number in the new App may be different from its original record number.

In this case, the list of record numbers returned by the Add Records API when record restoration is complete is matched with the backed up comment data, and the comments are posted to the new record number.

2. Restoring comments with mentions

The Add Comment API allows the user to specify (mention) a user to send a comment to. However, restoring the comment with mention data using the Add Comment API sends the notification to the user.

Therefore, the name of the mentioned user is included in the comment text as a string without the @ symbol. Use this method to avoid sending a notification to the user when restoring the comment.

3. Restoring the name of the original user who posted the comment

The name of the user who posted the comment using the Add Comment API will be the name of the user account that executed the API. When restoring the record, a workaround will be necessary to note the original user who posted the comment, such as adding the name of the user in the comment text.

4. Restoring the original comment date and time

The date and time of posting a comment using the Add Comment API will be the date and time of reposting. This cannot be changed. A workaround is to restore the comment with the original post date and time in the comment text.

When executing a backup, the time is delivered in UTC string. Be sure to convert it to the local time.

Example: Original comment to backup

US_05.png

Example: Restored comment

US_06.png

Code Explanation

The following section will explain the sample codes, sample_1.js and sample_2.js.

sample_1.js

First, a function called getCommentsData is defined that uses the Get Comments API to retrieve comment data of the App's records. The maximum number of comments that can be obtained with this API is 10. To obtain all the comment data, a recursive process is used. The order is also specified as asc to obtain the comments in ascending order.

 

 

Next, a function called downloadFile is defined to process the file download when the backup button is clicked.

 

 

Then, a function called fetchRecords is defined to retrieve the data from all records in the App using the Get Records API.

 

 

The Kintone Record List Onload Event is then declared, and is triggered when the record list page is loaded. Within this event, an HTML button element is created to use to initiate the backup. The onclick event is attached to the created button element to define what should happen when the button is clicked. The Get Record List Header Element API is also used to retrieve the header element space and append the created button there. 

 

 

Within the onclick event, the fetchRecords function that was defined earlier is executed to retrieve the applicable records to backup.

 

 

After retrieving the records, the returned records object is converted to the JSON format and downloaded with the downloadFile function.

 

 

After downloading the record data, the comments in each record are saved in an array and converted to JSON format to be downloaded by the downloadFile function as well.

 

sample_2.js

The first section of the sample code defines two functions, postComment and postComments.

 

The postComment function uses the Add Comment API to add a comment to a record. However, since the Add Comment API can only post a single comment to a specified record, the postComments function is used to reiterate over each comment that needs to be restored to add all comments to the record.

 

 

The next section defines three more functions used to prepare the records and comments to be restored - sanitizeRecords, buildTimeString, and buildComments.

 

The sanitizeRecords function removes the record number key from the records object. This is necessary because the restored records will be assigned a new record number.

 

 

The buildTimeString function formats the datetime that the original comment was posted to add it into the restored comment.

 

 

The buildComments function first removes the mentions property from the comments object to prevent a notification being sent to the mentioned user when the comment is restored. Then, it adds the original comment creator and the original posted datetime formatted with buildTimeString to the comment content. This function pertains to the Notes on Restoring Comments section, notes 2 through 4.

 

 

The Kintone Record List Onload event is declared, and is triggered when the record list page is loaded. The rest of the sample code is placed within this event so that it is executed when the record list page loads. 

 

 

The first section within the event defines a function called inputFileContent that loads file content to be displayed in an HTML element. The HTML elements record-data and comment-data are retrieved, and the inputFileContent function is executed for each to load the selected file content into each HTML element.

 

 

The click event is attached to the HTML button element so that the code within the function is executed when the button is clicked.

 

 

When the button is clicked, the loaded JSON is prepared to be restored as Kintone records and comments, and a dialogue window is shown to confirm whether to restore the data. If confirmed, the code within the if statement is executed.

 

 

The Kintone Add Records REST API is used to restore the records to the current App, and when the records are successfully restored, an object containing data including the array of registered record IDs is returned. Comments are then restored using the array of new record IDs, the array of old record IDs, and the postComments and buildComments functions.

 

 

Limitations of the Sample Code

  • The sample code does not work with data containing Attachment File field or Calculation fields.
    * This can be fixed by adding the same Number field(s) and Calculation field with the same formula into the App the records will be restored to. The same is true for Text fields using the Calculate automatically setting.
  • The sample code loads all data into the computer's memory once during both the backup and restoring process. Be careful that sufficient memory is available when working with Apps with a large number of records.

Finally

This article explained a simple solution to back up and restore record and comment data to a new App. This sample code can be used to create a more sophisticated solution for backup and restoration if necessary. 

Was this article helpful?
0 out of 0 found this helpful
Do you have any questions or issues related to this article?
Please share your views with us in the Community forums!