Avatar

Copy data from another app

Ngọc Tân Hoàng

I'm new in kintone, I need copy from another app and show it in my current app. But I dont know how to do it. 

Please help me.

Follow 4 followers
0

8 comments

Avatar
Ngọc Tân Hoàng 0 votes

I try to use POST but it can not update when I change value or add new record in source App

0
Avatar
William Sayama 0 votes

Hi Ngọc Tân Hoàng, welcome to the Community Forums.

Are you trying to copy record data from another App into your main App?
If so, you can either use the Lookup fields or Related Record fields for that functionality. No API customization will be needed.

If you are trying to solve a different issue using API, please give us more information on what API you are using, and the requests and the responses that are given (please keep your domain name and authentication info hidden for security purposes)

0
Avatar
Ngọc Tân Hoàng 0 votes

Hi William,

My requirement is "create app and button, when click button show data of source App" and I must use JS customize. 
My solution is GET data from source App and POST it to main App, but it's not posible because I cant update my main App

0
Avatar
William Sayama 0 votes

Thanks for the information.

Please try using the Update Record API which uses the PUT method to update your App's record.
After updating your record, the screen must be refreshed for you to view the update changes.

If you are still running into issues, please share us your response data, and we can take a deeper look :)

0
Avatar
Ngọc Tân Hoàng 1 vote

This's my code

Help me check and correct it, please. 

let copyId = 112;
let appID = kintone.app.getId();
function getCopyAppData() {
let body = {
"app": copyId,
"query": kintone.app.getQuery()
};
return kintone.api(kintone.api.url('/k/v1/records', true), 'GET', body).then(function(resp) {return resp;})


}
function addDataToCurrentApp(data) {
let body = {
"app": appID,
"records": data
};
return kintone.api(kintone.api.url('/k/v1/records', true), 'POST', body).then(function(resp) {
return resp;
})
}
getCopyAppData().then(function(data) {
let events = ['app.record.create.show', 'app.record.edit.show', "app.record.index.show"];
kintone.events.on(events, function(e) {
if (document.getElementById('addData') !== null) {
return;
}
let menuButton = document.createElement("button");
menuButton.id = "addData";
menuButton.innerHTML = "Add data";
kintone.app.getHeaderMenuSpaceElement().appendChild(menuButton);
menuButton.onclick = function() {
addDataToCurrentApp(data);
}
})
})
1
Avatar
Junko Werner 2 votes

Hi Ngọc Tân Hoàng,

I have noticed the following two things you might want to modify to correct your coding.

1. The error is likely to be output on the API side because it does not give the information that it is trying to pass to POST well.

 I have tried your coding and got the error message "Missing or invalid input."  since in the following procedure, the  "Data" value is not set.

 addDataToCurrentApp(data);


 I was able to see the errors by changing the POST processing as follows:

   return kintone.api(kintone.api.url('/k/v1/records', true), 'POST', body).then(function(resp) {
          return resp;
        },function(err) {
            console.log(err);
        })

2. Even if you are able to pass the information to POST, it will be an error because the get response and the request parameters at Post are different.

 On the following website, there are sample field response and request for each field. You might want to check them out.

 ▼ Field Types
 https://developer.kintone.io/hc/en-us/articles/212494818/#responses

I have worked on your coding and modified it as below. It works now with my app. Please review it as a reference. Because it depends on an app, even if you use it at your end as it is, it might not work as you desire. So please change some coding as necessary.

(function() {
   "use strict";

    let copyId = 112;
    let appID = kintone.app.getId();
    function getCopyAppData() {
        let body = {
          "app": copyId,
          "query": kintone.app.getQuery()
        };
        // return kintone.api(kintone.api.url('/k/v1/records', true), 'GET', body).then(function(resp) {return resp;})
        return kintone.api(kintone.api.url('/k/v1/records', true), 'GET', body).then(function(resp) {
            addDataToCurrentApp(resp.records);
            return resp
        })
   
         
     }
      function addDataToCurrentApp(data) {
       

        var wData = [];
        for(var i =0;i<data.length;i++){
            var param2 = {
                          "str":{
                             "value":data[i].str.value
                        }
                    }
            wData.push(param2);
        }

//        wData.push(data[i]);
       
       

        let body = {
          "app": appID,
          "records":wData
        };
       
        return kintone.api(kintone.api.url('/k/v1/records', true), 'POST', body).then(function(resp) {
          return resp;
        },function(err) {
            console.log(err);
        })
      }
    // getCopyAppData().then(function(data) {
      let events = ['app.record.create.show', 'app.record.edit.show', "app.record.index.show"];
        kintone.events.on(events, function(e) {
          if (document.getElementById('addData') !== null) {
              return;
          }
          let menuButton = document.createElement("button");
              menuButton.id = "addData";
              menuButton.innerHTML = "Add data";
          kintone.app.getHeaderMenuSpaceElement().appendChild(menuButton);
          menuButton.onclick = function() {
            // addDataToCurrentApp(data);
            getCopyAppData();
          }
        })
    // })    
   
   
})();

The idea of creating customizations is that you should check the operation of each one of the processes you have created while debugging.
By debugging, you can also see what the contents of get responses and post requests are, and identify errors. Here are the debugging tips.

▼ Debugging Tips for kintone JS
https://developer.kintone.io/hc/en-us/articles/115003211768-Debugging-Tips-for-kintone-JS

Hope it helps.

Thanks.

Junko

2
Avatar
Ngọc Tân Hoàng 0 votes

Hi Junko Werner, 

Thanks for your response!

May I ask you more?

Your code can display data from source App to current App when click button. But source App add/edit/delete field, how can I update my current App. 

I try using Delete all record and Post data again, it usually do well but sometime response "POST https://crm-mypl.cybozu.com/k/v1/records.json 520 (520)", "Uncaught (in promise) {code: "GAIA_DA02", id: "DRJRzgjkwjDDGA33mwO8", message: "Failed to save the changes because the database co…not be locked. Please wait a while and try again."}". 

I know that bug "https://developer.kintone.io/hc/en-us/community/posts/360024901313-Update-Record-Permissions" but I dont have any better idea. 

Help me, please!

0
Avatar
Yasuhisa Kaneko 1 vote

Hi Ngọc Tân Hoàng,

Like the previous post regarding locked database error message,

the error message you received seems due to too much process at the same time.
 
 
I would think the followings might help to avoid the same kind of error.
 
•Reduce the number of fields and registered records in each app so that it doesn't take too long to process such as record updates.
 
•Split API requests and avoid duplication of processing time
 
•Simplify your app settings, such as setting permissions, with complex settings, etc.
 
•Records that could not be updated with an error should be left for retry processing, and then retry at a time when other update processing and time do not overlap.
 
PUT methods can update records.
 
▼ Update Record (PUT) 
 
▼ Kintone REST API List 
 
Thank you,
 
Yasu
1