お世話になっております。
kintone.proxyを使用してメール送信し、送信した後得意先をサブテーブルに登録したいと考えております。
実現したい事
1.送信対象者のメールアドレス等を別アプリから取得
2.対象者にメールを送信する
3.送信した得意先をサブテーブルに登録する
メールの送信が完了し、サブテーブルに登録する事は出来たのですが、
登録件数が一致しない事があります。
ご教示ほどよろしくお願いします。
(function()
{
    "use strict";
    kintone.events.on('app.record.detail.show',function(event)
        //メール送信ボタン増殖防止
        if(document.getElementById('Mail_Send_Button')!=null){
            return;
        }
        var Mail_Send_Button = document.createElement('button');
        Mail_Send_Button.id = 'Mail_Send_Button';
        Mail_Send_Button.innerHTML = 'お知らせ送信';
        Mail_Send_Button.onclick = function()
        {
            //送信日時が空白の場合メール送信を行う
             if(event.record['送信日時'].value == '')
             {
                //お知らせメール送信対象者を取得する
                let type = event.record['メール送信対象得意先'].value;
                let query = '';
                switch(type)
                {
                    case '特定の得意先':
                        query = '得意先名 = "' + event.record['得意先選択'].value + '"';
                    break;
                    case '得意先A':
                    case '得意先B':
                        query = 'グループ in ( "' + type + '" )';                
                    break;
                }
                var params = {
                     "app": Master_APP_ID,
                     "query": query
                }
                
                kintone.api(kintone.api.url('/k/v1/records', true), 'GET', params,function(resp) {
                 let resp_record = resp.records;
                 let err_flg = false;
                 let max_length = resp_record.length;
         //取得したレコード分回す
         for(let i = 0; i < resp_record.length; i++)
                 {                 
                     const record_id = kintone.app.record.getId();
           var out_data = { 
                         mail : resp_record[i]['メールアドレス'].value,
                         customer : resp_record[i]['得意先名'].value,
                         title : event.record['件名'].value,
                         body : event.record['本文'].value,
                         url : Kintone_URL+record_id,
           }
                    //メール送信
                     kintone.proxy('メール送信プログラム','POST',{'Content-Type': 'application/json'},out_data).then((args) =>{
console.log(i)                        
                         console.log(resp_record[i]['得意先名'].value)
                         console.log(event.record['確認履歴テーブル']);
                         
                         if(i==0)
                         {
                            event.record['確認履歴テーブル']['value'][i]['value']['得意先名'].value = resp_record[i]['得意先名'].value;
}
                         else
                         {
                             event.record['確認履歴テーブル']['value'].push({
                                    value:{
                                    "確認日時":{
                                        value:'',
                                        type:'SINGLE_LINE_TEXT'
                                    },
                                    "得意先名":{
                                        value: resp_record[i]['得意先名'].value,
                                        type:'SINGLE_LINE_TEXT'
                                    }    
                                }                     
                             })                             
                         } 
if(max_length == i && !err_flg)
                         {
                             var params = {
                             "app": appId,
                             "id": record_id,
                             "record":{
                                 "確認履歴テーブル":{
                                    "value":event.record['確認履歴テーブル'].value
                                    }
                                 }
                             }
                            kintone.api(kintone.api.url('/k/v1/record', true), 'PUT', params).then(function(resp) {
                                alert('更新');
                                window.location.reload();
                            },function(resp){
                            if (resp.message !== undefined) {
                                alert(resp.message);
                            } 
                            });                         
                             
                         }
                     },(error) => {
                         alert('メール送信に失敗いたしました。');
                     });                 
              }              
                });
          }
             else
             {
                 alert('メール送信済みの為送信処理を終了いたします。');
                 return false;
             }
        }
        kintone.app.record.getHeaderMenuSpaceElement().appendChild(Mail_Send_Button);
    });
})();