JavaScript and Web API

Sample preparation

创建实体:经销商和经销商销售计划,1:N关系

经销商(实体名称: gdh_dealer)

字段名称字段逻辑名称类型
经销商名称gdh_dealer_name文本

经销商销售计划(实体名称: gdh_dealer_sales_plan)

字段名称字段逻辑名称类型
关联的经销商gdh_dealer查找
年度gdh_year选项
通知时间gdh_notification_time时间和日期
是否通知gdh_whether_to_notify布尔
销售金额(浮点)gdh_sales_amount_float浮点
销售金额(十进制)gdh_sales_amount_decimal十进制
销售金额(货币)gdh_sales_amount_currency货币
销售金额(整数)gdh_sales_amount_int整数
备注gdh_remark文本

创建记录

创建经销商销售计划记录

   CreateDealerSalesPlan: function () {
     var onlineWebApi = Xrm.WebApi.online;
     var data = {};
     data["gdh_year"] = 2023;
     data["gdh_notification_time"] = new Date("2023-12-17T03:24:00");
     data["gdh_whether_to_notify"] = true;
     data["gdh_sales_amount_float"] = 800.8;
     data["gdh_sales_amount_decimal"] = 1900.88;
     data["gdh_sales_amount_currency"] = 480.99;
     data["gdh_sales_amount_int"] = 300;
     data["gdh_remark"] = "这是经销商销售计划备注";
     // create dealer sales plan record
     onlineWebApi.createRecord("gdh_dealer_sales_plan", data).then(
       function success(result) {
         console.log("Dealer Sales Plan created with ID: " + result.id);
         // perform operations on record creation
       },
       function (error) {
         console.log(
           "**************Error message**************" + error.message
         );
         // handle error conditions
       }
     );
   }

更新记录

 // define the data to update a record
 var data =
     {
         "name": "Updated Sample Account ",
         "creditonhold": true,
         "address1_latitude": 47.639583,
         "description": "This is the updated description of the sample account",
         "revenue": 6000000,
         "accountcategorycode": 2
     }
 // update the record
 Xrm.WebApi.updateRecord("account", "5531d753-95af-e711-a94e-000d3a11e605", data).then(
     function success(result) {
         console.log("Account updated");
         // perform operations on record update
     },
     function (error) {
         console.log(error.message);
         // handle error conditions
     }
 );

删除记录

 Xrm.WebApi.deleteRecord("account", "5531d753-95af-e711-a94e-000d3a11e605").then(
     function success(result) {
         console.log("Account deleted");
         // perform operations on record deletion
     },
     function (error) {
         console.log(error.message);
         // handle error conditions
     }
 );

Retrieve and RetrieveMultiple using Xrm.WebApi

TODO

获取选项值(所有)

   GetOptionset: function (entityName, fieldName) {
     var req = new XMLHttpRequest();
     req.open(
       "GET",
       Xrm.Page.context.getClientUrl() +
         "/api/data/v9.2/EntityDefinitions(LogicalName='" +
         entityName +
         "')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$filter=LogicalName eq '" +
         fieldName +
         "'&$expand=OptionSet",
       true
     );
     req.setRequestHeader("OData-MaxVersion", "4.0");
     req.setRequestHeader("OData-Version", "4.0");
     req.setRequestHeader("Accept", "application/json");
     req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
     req.setRequestHeader("Prefer", 'odata.include-annotations="*"');
     req.onreadystatechange = function () {
       if (this.readyState === 4) {
         req.onreadystatechange = null;
         if (this.status === 200) {
           var results = JSON.parse(this.response);
           for (var i = 0; i < results.value[0].OptionSet.Options.length; i++) {
             var Label =
               results.value[0].OptionSet.Options[i].Label.UserLocalizedLabel
                 .Label;
             console.log(Label);
           }
         } else {
           Xrm.Utility.alertDialog(this.statusText);
         }
       }
     };
     req.send();
   }

Calling a Workflow from JavaScript

TODO

Set and Retrieve Lookup Id and Value using Xrm.WebApi

TODO

Calling an Action from JavaScript

TODO

Calling an Action from JavaScript using Xrm.WebApi.online.execute

TODO

Running FetchXML using Xrm.WebApi

TODO

Refer

Xrm.WebApi.online (Client API reference)

Article · 11/30/2022 · 5 contributors

Provides methods to use Web API to create and manage records and execute Web API actions and functions in model-driven apps when connected to the model-driven apps server (online mode).

 var onlineWebApi = Xrm.WebApi.online;

The onlineWebApi object provides the following methods:

最后修改:2023 年 10 月 09 日
如果觉得我的文章对你有用,请随意赞赏