如何在Microsoft Dynamics 365 F&O中点击按钮后获取API响应并填入列
D365 F&O 天气查询表单功能实现问题
我在Microsoft D365 F&O中创建了一个简单表单,包含:
- 以网格形式展示的
weatherTable(包含Location、Temperature字段) - 网格上方的一个按钮
系统预期逻辑:
- Location列可编辑,用户输入地区
- 点击按钮后,只读的Temperature列需根据该地区显示对应温度
此功能通过指定API实现,该API接收地区作为参数并返回包含详情的JSON文件,我已通过Postman测试该API可正常工作。
- API端点:
https://api.weatherapi.com/v1/current.json?key=5b6d44effc87449990c101000241405&aqi=no&q=location - JSON响应示例:
{ "location": { "name": "New York", "region": "New York", "country": "United States of America", "lat": 40.71, "lon": -74.01, "tz_id": "America/New_York", "localtime_epoch": 1716281298, "localtime": "2024-05-21 4:48" }, "current": { "last_updated_epoch": 1716281100, "last_updated": "2024-05-21 04:45", "temp_c": 15.0, "temp_f": 59.0, "is_day": 0, "condition": { "text": "Clear", "icon": "//cdn.weatherapi.com/weather/64x64/night/113.png", "code": 1000 } } }
我需要实现的是:读取表单中Location的地区值,作为参数发送请求,将JSON响应中的temp_c值填入Temperature列。
我尝试编写了以下代码,但知道还需补充大量内容:
public void clicked() { System.Net.HttpWebRequest request; System.IO.Stream stream; System.Exception sysEx; str URLAPI; str location; Query query; QueryRun queryRun; WeatherTable weatherTable; // Define a query to retrieve the location from WeatherTable query = new Query(); // Create a query run queryRun = new QueryRun(query); // Run the query and retrieve the location value queryRun.next(); WeatherTable = queryRun.get(tableNum(WeatherTable)); location = weatherTable.Location; // Construct the API URL URLAPI = strFmt("https://api.weatherapi.com/v1/current.json?key=5b6d44effc87449990c101000241405&aqi=no&q=%1", location); // Create the HTTP request request = System.Net.WebRequest::Create(URLAPI) as System.Net.HttpWebRequest; request.Method = 'POST'; request.ContentType = 'application/json'; // Further code for handling the HTTP request... // Set the request headers //System.Net.WebHeaderCollection headerCollection = request.Headers; //headerCollection.Set('Name', 'Value'); var utf8 = System.Text.Encoding::get_UTF8(); // Set the request body var byteArrayPayload = utf8.GetBytes("{\"key1\":\"value1\",\"key2\":\"value2\"}"); try { // send out the payload using (System.IO.Stream dataStream = request.GetRequestStream()) { dataStream.Write(byteArrayPayload, 0, byteArrayPayload.Length); } // request.GetResponse() may already result in an error if the request was e.g. a Bad Request(Status Code 400). This should be handled upstream via our global error handling. using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse) { stream = response.GetResponseStream(); System.IO.StreamReader reader = new System.IO.StreamReader(stream); str responseBody = reader.ReadToEnd(); } } catch (sysEx) { throw; } }
内容的提问来源于stack exchange,提问作者Tom
相关产品推荐
相关产品推荐

