Dynamics 365 js中调用action

第一种

js 

var json = {};
json["EntityId"] = Xrm.Page.data.entity.getId();
json["EntityName"] = Xrm.Page.data.entity.getEntityName();
json["UserId"] = Xrm.Page.context.getUserId();
json["Name"] = "1";
var params = {};
params["json"] = JSON.stringify(json);
var request = {};
request["action"] = "ew_OutboundDeliveryAction";
request["params"] = params;
PostAction(request, function (data) {
    var res = JSON.parse(data.value);
    if (res.code == 0) {
        alert(res.msg);
        Xrm.Page.data.save().then(function () { parent.window.location.reload(); });
    } else {
        alert(res.msg);
    }
}, function (err) {
    alert(err.message);
});

    /**
 * @param {any} data 参数
 * @param {any} callback 成功返回方法
 * @param {any} errorcallback 失败返回方法
 */
function PostAction(data, callback, errorcallback, async = false) {
    var requestURL = Xrm.Page.context.prependOrgName("/api/data/v9.0/" + data.action);
    var req = new XMLHttpRequest()
    req.open("post", encodeURI(requestURL), async);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function () {
        if (this.readyState == 4) {
            if (this.status == 200) {
                callback(JSON.parse(this.responseText));
            } else {
                errorcallback(JSON.parse(this.responseText));
            }
        }
    };
    req.send(window.JSON.stringify(data.params));
}

C#

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ride.Crm.Order
{
    public class Order : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService serviceAdmin = serviceFactory.CreateOrganizationService(null);
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            if (context.Depth > 2) return;
            try
            {
                string jsons = context.InputParameters["json"] as string;
                var json = JsonConvert.DeserializeObject<Josn>(jsons);
                var entity = serviceAdmin.Retrieve(json.EntityName, json.EntityId, new ColumnSet(true));
                var value = "Success";
                switch (json.Name)
                {
                    case "1"://
                        Entity entity1 = new Entity(entity.LogicalName, entity.Id);
                        entity1["status"] = new OptionSetValue(20);
                        service.Update(entity1);
                        break;
                }

                context.OutputParameters["value"] = JsonConvert.SerializeObject(new { code = 0, msg = value });
            }
            catch (Exception ex)
            {
                context.OutputParameters["value"] = JsonConvert.SerializeObject(new { code = -1, msg = ex.Message });
                throw new Exception(ex.Message);
            }
        }
        public class Josn
        {
            public Guid UserId { get; set; }
            public Guid EntityId { get; set; }
            public string EntityName { get; set; }
            public string Name { get; set; }
        }
    }
}

第二种

js

let url = Xrm.Page.context.getClientUrl() + "/api/data/v9.1/new_DOCX";
let data = {
    json: Xrm.Page.data.entity.getId().replace('{', '').replace('}', '')//实体id;
};
let res = window.YWRequest(url, JSON.stringify(data), 'post');
if (res.value) {
     alert("成功");
} else {
    alert("失败");
}


/**
 * Ajax请求
 * @param {*} url 
 * @param {*} data 
 * @param {*} type 
 */
function YWRequest(url, data = null, type = "get", async = false) {
    let response;
    $.ajax({
        url: url,
        type: type,
        dataType: "json",
        data: data,
        async: async, // 同步
        contentType: "application/json; charset=utf-8",
        success: function (res) {
            response = res;
        }, error: function (error) {
            console.error(error.responseJSON.error.message);
        }
    });
    return response;
}

C#

public void Execute(IServiceProvider serviceProvider)
{
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService serviceAdmin = serviceFactory.CreateOrganizationService(null);
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    if (context.Depth > 2) return;
    try
    {

        string json = context.InputParameters["json"] as string;
        var entity = serviceAdmin.Retrieve("xxx", Guid.Parse(json), new ColumnSet(true));
        
        context.OutputParameters["value"] = "";
        
    }
    catch (Exception ex)
    {
        context.OutputParameters["value"] = ex.Message;
    }
}
        private class EntityJosn
        {
            /// <summary>
            /// 当前用户id
            /// </summary>
            public Guid UserId { get; set; }
            /// <summary>
            /// 实体id
            /// </summary>
            public Guid EntityId { get; set; }
            /// <summary>
            /// 实体名
            /// </summary>
            public string EntityName { get; set; }
        }

第三种

js

var entityId = Xrm.Page.data.entity.getId().replace("{", "").replace("}", "");
var parameters = {
    "Type": "xxxx",
    "Data": entityId
};
RequestAction("ew_QuoteApplicationAction", parameters, true, function (res)
{
    //var data = JSON.parse(res);
    console.log(res);
});



/**
 * 执行Action
 * @param {any} actionName  Action名字
 * @param {any} data        参数
 * @param {any} async       同步异步
 * @param {any} success     成功执行方法
 * @param {any} fail        失败执行方法
 */
function RequestAction(actionName, data, async, success)
{
    var request = {
        actionName: actionName,
        parameters: data
    };

    var options = {
        async: async
    };
    Xrm.WebApi.online.execute(request, options).then(success, function (res)
    {
        console.error(res);
    });
}

C#

public void Execute(IServiceProvider serviceProvider)
{
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService serviceAdmin = serviceFactory.CreateOrganizationService(null);
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    if (context.Depth > 2) return;
    try
    {

        string json = context.InputParameters["json"] as string;
        var entity = serviceAdmin.Retrieve("xxxxx", Guid.Parse(json), new ColumnSet(true));
        
        context.OutputParameters["value"] = "";
        
    }
    catch (Exception ex)
    {
        context.OutputParameters["value"] = ex.Message;
    }
}
        private class EntityJosn
        {
            /// <summary>
            /// 当前用户id
            /// </summary>
            public Guid UserId { get; set; }
            /// <summary>
            /// 实体id
            /// </summary>
            public Guid EntityId { get; set; }
            /// <summary>
            /// 实体名
            /// </summary>
            public string EntityName { get; set; }
        }

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dynamics 365是一款由微软公司推出的全面的企业资源计划(ERP)和客户关系管理(CRM)解决方案,它提供了一系列的应用程序和工具,用于帮助企业管理和优化他们的业务流程。 Dynamics 365开发是指通过使用Dynamics 365平台的开发工具和技术,为企业定制和构建应用程序,以满足其独特的业务需求。Dynamics 365开发人员可以利用平台上提供的工具和功能,如PowerApps和Power Automate等,快速创建定制的业务应用程序。同时,他们还可以使用Dynamics 365的集成开发环境和API,将其现有的业务应用程序和系统与Dynamics 365集成起来,实现数据的共享和协同工作。 Dynamics 365开发可以为企业带来以下好处: 1. 定制化能力:Dynamics 365平台提供了广泛的开发工具和功能,让开发人员可以根据企业的具体需求来定制和构建应用程序。这使得企业能够更好地适应不断变化的市场需求,提高业务的灵活性和竞争力。 2. 效率和自动化:Dynamics 365开发工具和技术可以帮助企业实现业务流程的自动化和优化,减少人工操作和错误。通过将现有的业务应用程序和系统与Dynamics 365集成,企业可以实现数据的实时共享和自动化的工作流程,从而提高工作效率和产能。 3. 数据分析和洞察力:Dynamics 365提供了强大的数据分析和洞察力功能,帮助企业从海量数据提取有价值的信息和洞察力。开发人员可以通过利用Dynamics 365平台上的分析工具和技术,为企业构建智能化的数据分析和报告系统,帮助企业做出更准确的决策和战略规划。 总之,Dynamics 365开发为企业提供了一个灵活和强大的平台,帮助他们定制和构建适合自己独特需求的应用程序,并实现业务流程的自动化和优化。随着企业的需求不断变化和发展,Dynamics 365开发将继续发挥重要作用,帮助企业保持竞争优势。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值