LoginSignup
2
3

More than 1 year has passed since last update.

GASでkintoneのレコードを条件付きで取得する

Posted at

kintone運用する中で、GASを使って電話番号が一致するレコードを取得したくなったので書いてみました。

function getCustomer(phone){
    const apiToken = ""; // kintoneで作成したAPIトークン
    const subdomain = ""; // kintone URLの https://***.cybozu.com における"***"の部分
    const appId = ""; //kintoneアプリの https://***.cybozu.com/k/xx/ における"xx"の部分

    const url = "https://" + subdomain + ".cybozu.com/k/v1/records.json?"
                + "app=" + appId
                + "&query=" + encodeURIComponent('phone = "080-1234-5678"'); //"phone"というフィールドコードで作成しカラムを検索条件にする場合

    const formHeader = {
        'X-Cybozu-API-Token': apiToken
    };
    const options = {
        'method': 'get',
        'headers': formHeader,
    };

    var res = UrlFetchApp.fetch(url, options);
    var json = JSON.parse(res.getContentText());
    console.log("length: " + json["records"].length); //該当データの件数
    console.log("response: "+ res); //該当データ内容
    return res;
}

複数条件指定したい場合は、encodeURIComponent(phone="hoge" and email="fuga")みたいな感じにしておく。

KintoneManager使った方が楽かもしれませんが、参考になれば。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3