# 見積書自動採番（年初め又は4月に、001から開始）

**URL:** <https://community.cybozu.dev/t/topic/1473>\
**Category:** kintone 開発相談\
**Created:** [2023 年 3 月 27 日午前 6:50 UTC](https://community.cybozu.dev/t/topic/1473 "2023-03-27T06:50:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account94](https://avatars.discourse-cdn.com/v4/letter/l/6de8d8/32.png) [@Legacy\_Account94](https://community.cybozu.dev/u/Legacy_Account94)\
**Post date:** [2023 年 3 月 27 日午前 6:50 UTC](https://community.cybozu.dev/t/topic/1473/1 "2023-03-27T06:50:36Z")

</div>

自動採番で、下記のプログラムは役に立っていますが、年替わり、0001から開始したいのと依頼があり、別のプログラムでも試しましたが、上手く行きません。

何か良い方法があればご教授願いたく

[https://cybozudev.zendesk.com/hc/ja/articles/202640930-%E8%87%AA%E5%8B%95%E6%8E%A1%E7%95%AA%E3%81%97%E3%81%A6-%E3%83%AC%E3%82%B3%E3%83%BC%E3%83%89%E7%99%BB%E9%8C%B2%E3%81%99%E3%82%8B?\_ga=2.62508462.32070349.1679879093-696949062.1676435743](https://community.cybozu.dev/hc/ja/articles/202640930-%E8%87%AA%E5%8B%95%E6%8E%A1%E7%95%AA%E3%81%97%E3%81%A6-%E3%83%AC%E3%82%B3%E3%83%BC%E3%83%89%E7%99%BB%E9%8C%B2%E3%81%99%E3%82%8B?_ga=2.62508462.32070349.1679879093-696949062.1676435743)

&nbsp;

年号で変化するとの情報はこちらです。

[https://www.joyzo.co.jp/blog/259](https://www.joyzo.co.jp/blog/259)

&nbsp;

(function () {

&nbsp; “use strict”;

&nbsp; &nbsp;

&nbsp; function createNo(event) {

&nbsp; &nbsp; var maxNum = 0; &nbsp; &nbsp; &nbsp; //見積番号の枝番の最大値

&nbsp; &nbsp; var date = new Date();

&nbsp; &nbsp; var year = date.getYear().toString().substr(1,2); //西暦の下2桁を取得する

&nbsp; &nbsp; var month = date.getMonth() + 1;

&nbsp; &nbsp; var day = date.getDay();

&nbsp; &nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; // アプリIDを取得する

&nbsp; &nbsp; var appId = kintone.app.getId();

&nbsp;

&nbsp; &nbsp; //最新の見積番号を取得するためのURLを生成

&nbsp; &nbsp; var sch = year + “-”;

&nbsp; &nbsp; var tmpNo = 0;

&nbsp; &nbsp; var query\_string = ‘no like "’ + sch + ‘" order by no desc limit 1&fields[0]=no’ &nbsp;//見積番号の最大値を取得するクエリ

&nbsp; &nbsp; var apiUrl = kintone.api.url(‘/k/v1/records’) + ‘?app=’ + appId + ‘&query=’ + encodeURI(query\_string);

&nbsp; &nbsp;

&nbsp; &nbsp; var xmlHttp;

&nbsp; &nbsp; xmlHttp = getXmlHttp();

&nbsp;

&nbsp; &nbsp; // 同期リクエストを行う

&nbsp; &nbsp; xmlHttp.open(“GET”, apiUrl, false);

&nbsp; &nbsp; xmlHttp.setRequestHeader(‘X-Requested-With’,‘XMLHttpRequest’);

&nbsp; &nbsp; xmlHttp.send(null);

&nbsp;

&nbsp; &nbsp; if (xmlHttp.status == 200){

&nbsp; &nbsp; &nbsp; if(window.JSON){

&nbsp; &nbsp; &nbsp; &nbsp; var obj = JSON.parse(xmlHttp.responseText);

&nbsp; &nbsp; &nbsp; &nbsp; if (obj.records[0] != null){

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpNo = obj.records[0][‘見積番号’].value.split(“-”); &nbsp;//見積番号を-で区切る（年度と見積番号を分ける）

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxNum = parseInt(tmpNo[1]); &nbsp; &nbsp;//最後の数字（枝番）を取得 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxNum = 0; &nbsp;//データが存在しない = 新年度なので枝番を初期化

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (maxNum === null) {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.record[‘見積番号’].error = ‘見積番号が取得できませんでした’;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return event;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var num = parseInt(maxNum) + 1; //最新の見積番号を発番

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.record[‘見積番号’][‘value’] = year + “-” + (‘0000’ + num).slice(-4); // 発番した見積番号を設定する

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else{

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.record[‘見積番号’].error = xmlHttp.statusText;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else{

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.record[‘見積番号’].error = ‘見積番号取得に失敗しました。’;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return event;

&nbsp; }

&nbsp; &nbsp;

&nbsp; //xmlhttpオブジェクトを生成

&nbsp; function getXmlHttp() {

&nbsp; &nbsp; var xmlhttp = false;

&nbsp; &nbsp;

&nbsp; &nbsp; if(typeof ActiveXObject != “undefined”){ &nbsp;//IE

&nbsp; &nbsp; &nbsp; try {

&nbsp; &nbsp; &nbsp; &nbsp; xmlhttp = new ActiveXObject(“Microsoft.XMLHTTP”);

&nbsp; &nbsp; &nbsp; } catch (e) {

&nbsp; &nbsp; &nbsp; &nbsp; xmlhttp = false;

&nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; }

&nbsp; &nbsp;

&nbsp; &nbsp; if(!xmlhttp && typeof XMLHttpRequest != “undefined”) { &nbsp;//IE以外

&nbsp; &nbsp; &nbsp; xmlhttp = new XMLHttpRequest();

&nbsp; &nbsp; }

&nbsp; &nbsp;

&nbsp; &nbsp; return xmlhttp;

&nbsp; &nbsp;

&nbsp; } &nbsp;

&nbsp; // 新規作成画面から保存時のイベント

&nbsp; kintone.events.on(‘app.record.create.submit’, createNo);

})();

&nbsp;

&nbsp;

&nbsp;

初歩的な件で恐縮です。

&nbsp;

---

<div class="post-metadata">

**Author:** ![Legacy\_Account63](https://avatars.discourse-cdn.com/v4/letter/l/6de8d8/32.png) [@Legacy\_Account63](https://community.cybozu.dev/u/Legacy_Account63)\
**Post date:** [2023 年 3 月 28 日午前 12:24 UTC](https://community.cybozu.dev/t/topic/1473/2 "2023-03-28T00:24:28Z")

</div>

[https://cybozudev.zendesk.com/hc/ja/articles/4977994758297-kintone-JavaScript-%E3%82%AB%E3%82%B9%E3%82%BF%E3%83%9E%E3%82%A4%E3%82%BA%E3%83%87%E3%83%90%E3%83%83%E3%82%B0%E3%81%BE%E3%81%A8%E3%82%81#check-the-network](https://cybozudev.zendesk.com/hc/ja/articles/4977994758297-kintone-JavaScript-%E3%82%AB%E3%82%B9%E3%82%BF%E3%83%9E%E3%82%A4%E3%82%BA%E3%83%87%E3%83%90%E3%83%83%E3%82%B0%E3%81%BE%E3%81%A8%E3%82%81#check-the-network)

エラー箇所は通信部分ということはわかってるので、  
上記記事のようにネットワークタブから送信内容と受信内容をそれぞれ確認すると何かヒントがあるかもしれません。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account94](https://avatars.discourse-cdn.com/v4/letter/l/6de8d8/32.png) [@Legacy\_Account94](https://community.cybozu.dev/u/Legacy_Account94)\
**Post date:** [2023 年 3 月 28 日午前 2:21 UTC](https://community.cybozu.dev/t/topic/1473/3 "2023-03-28T02:21:34Z")

</div>

村濱さんコメントありがとうございました。

javascript自体も初心者で苦戦していますが、ネットワークについては更に試行錯誤しています。

ネットワークの画面を確認したところこの様な画面？

console.log(result);もどこかにコードを足して確認も必要でしょうか

初歩的な質問で恐縮です。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account94](https://avatars.discourse-cdn.com/v4/letter/l/6de8d8/32.png) [@Legacy\_Account94](https://community.cybozu.dev/u/Legacy_Account94)\
**Post date:** [2023 年 3 月 28 日午前 2:25 UTC](https://community.cybozu.dev/t/topic/1473/4 "2023-03-28T02:25:48Z")

</div>



---

<div class="post-metadata">

**Author:** ![Legacy\_Account63](https://avatars.discourse-cdn.com/v4/letter/l/6de8d8/32.png) [@Legacy\_Account63](https://community.cybozu.dev/u/Legacy_Account63)\
**Post date:** [2023 年 4 月 1 日午前 6:59 UTC](https://community.cybozu.dev/t/topic/1473/5 "2023-04-01T06:59:01Z")

</div>

ネットワークタブで確認してもらっている上記画像はkintoneが発している別の通信で、コードの件とは関係なさそうです。

また、触れてませんでしたがxmlhttprequestでの同期処理は非推奨になっております。  
コード的にもわかりづらくいつ使えなくなるかわからないのでkintone.api()を使うのがいいかとおもいます。

async/awaitを利用すれば書きやすいと思います。

[https://cybozudev.zendesk.com/hc/ja/articles/900001244323-%E7%9B%AE%E6%8C%87%E3%81%9B-JavaScript%E3%82%AB%E3%82%B9%E3%82%BF%E3%83%9E%E3%82%A4%E3%82%BA%E4%B8%AD%E7%B4%9A%E8%80%85-%EF%BC%92-Promise%E3%81%AE%E3%81%8B%E3%82%8F%E3%82%8A%E3%81%ABasync-await%E7%B7%A8-#examples](https://cybozudev.zendesk.com/hc/ja/articles/900001244323-%E7%9B%AE%E6%8C%87%E3%81%9B-JavaScript%E3%82%AB%E3%82%B9%E3%82%BF%E3%83%9E%E3%82%A4%E3%82%BA%E4%B8%AD%E7%B4%9A%E8%80%85-%EF%BC%92-Promise%E3%81%AE%E3%81%8B%E3%82%8F%E3%82%8A%E3%81%ABasync-await%E7%B7%A8-#examples)

---

<div class="post-metadata">

**Author:** ![Legacy\_Account94](https://avatars.discourse-cdn.com/v4/letter/l/6de8d8/32.png) [@Legacy\_Account94](https://community.cybozu.dev/u/Legacy_Account94)\
**Post date:** [2023 年 4 月 4 日午前 7:22 UTC](https://community.cybozu.dev/t/topic/1473/6 "2023-04-04T07:22:30Z")

</div>

村濱さん

アドバイスありがとうございました。

無事に完成しました。

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex015/uploads/cybozu/original/1X/d5c841c3c5a6cb66eed514001c1b45147c9e1f4f.png) [@system](https://community.cybozu.dev/u/system)\
**Post date:** [2023 年 10 月 11 日午前 1:32 UTC](https://community.cybozu.dev/t/topic/1473/7 "2023-10-11T01:32:14Z")

</div>

このトピックはベストアンサーに選ばれた返信から 3 日が経過したので自動的にクローズされました。新たに返信することはできません。
