Dynamics CRM 365 online中使用Plugin的PostOperation开发,创建完account后自动创建note

目标:

Dynamics CRM 365 online中使用Plugin的PostOperation开发,创建完account后自动创建note


内容:

使用Visual studio 2019开发Plugin dll, 直接贴上如下代码

using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace Fei_Crm_Plugin
{
   public  class AccountNoteCreate :IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];               
                try
                {                  
                    var welcomeText = entity.Attributes["accountnumber"].ToString();

                    Entity NoteEntity = new Entity("annotation");

                    if (NoteEntity != null)
                    {
                        NoteEntity.Attributes.Add("subject", $"created on {DateTime.UtcNow.ToString()}");

                        NoteEntity.Attributes.Add("notetext", $"This is auto note for {welcomeText}");
                        NoteEntity.Attributes.Add("objectid", entity.ToEntityReference());

                        Guid note_guid = service.Create(NoteEntity);

                    }
                }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException($"An error occurred in Plug-in {ex.Message.ToString()}", ex);

                }
                catch (Exception ex)
                {
                    tracingService.Trace($"Fei_Plugin:{ex.ToString()}");
                    throw;
                }
            }
        }
    }
}

使用Plugin registration Tool连接CRM注册dll,此处需要注意的是选择PostOperation

![在这里插入图片描述](https://img-blog.csdnimg.cn/20201211153843522.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDEzNjgyMw==,size_16,color_FFFFFF,t_70)

测试结果是积极的,如下:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fei-AX&CRM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值