0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Dynamics365/モデル駆動アプリのJavascriptの並列の実装サンプル①

Last updated at Posted at 2021-12-02

Plugin内での並列処理は非推奨になっているため、より高速的に処理を実現するにはD365外部から実装することになります。
APサーバーなどを利用せずに、Javascriptでの並列処理の実現方法を共有させていただきます。
例えば、大量なデータを並列で登録する要件が発生する場合、以下の処理イメージで実装出来ればと思います。
image.png

sample.js
if (typeof (contoso) == "undefined") {
    contoso = { __namespace: true };
}
(function () {
    "use strict";
    var _currentIndex = 0;
    /**スレッド数*/
    var _concurrency = 20;
    /**処理済み件数 */
    var _processedCount = 0;
    /**総件数 */
    var _totalCount = 0;
    var _targetData = null;

    this.startProcessData = function (targetData) {
        _targetData = targetData;
        if (_targetData == null || _targetData.length == 0)
            return;
        _totalCount = _targetData.length;
        _processedCount = 0;
        _currentIndex = 0;
        Xrm.Utility.showProgressIndicator("processing....");
        for (var index = 0; index < _concurrency; index++) {
            processNextData();
        }
    }
    function processNextData () {
        var nextData = getNextData();
        if (nextData != null) {
            executeBusinessLogic(nextData);
        }
    }
    function executeBusinessLogic(data) {
        var data =
        {
            "lastname": data.name
        }
        Xrm.WebApi.createRecord("contact", data).then(
            function success(result) {
                processOneDataFinished(true);
            },
            function (error) {
                console.log(error.message);
                processOneDataFinished(false);
            }
        );
    }
    function getNextData() {
        if (_currentIndex < _targetData.length) {
            return _targetData[_currentIndex++];
        } else {
            return null;
        }
    }
    function processOneDataFinished(success) {
        _processedCount++;
        console.log("processed count:" + _processedCount + ",processing count:" + (_currentIndex - _processedCount));

 

        if (_processedCount >= _totalCount) {
            processAllDataFinished();
        } else {
            Xrm.Utility.showProgressIndicator(_processedCount + "/" + _totalCount);
            processNextData();
        }
    }
    function processAllDataFinished() {
        console.log("all data finished");
        Xrm.Utility.closeProgressIndicator();
    }
}).call(contoso.multithreadsample || (contoso.multithreadsample = {}));

//#region test
var targetData = [
    { name: "test1" },
    { name: "test2" },
    { name: "test3" },
    { name: "test4" },
    { name: "test5" },
    { name: "test6" },
    { name: "test7" },
    { name: "test8" },
    { name: "test9" },
    { name: "test10" }
];
contoso.multithreadsample.startProcessData(targetData);
//#endregion

上記のコードはブラウザのコンソールで実行可能です。
上記のサンプルは、大量なデータの登録のサンプルになりますが、JavascriptからCustomActionまたはCustomApiを呼び出して、さらに機能を拡張することが可能です。
MicrosoftTeams-image (33).png

0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?