2

我是插件新手。我的问题是,创建案例后,我需要将案例 ID 更新到分类帐中。连接这两者的是leadid。在我的情况下,我将潜在客户重命名为呼出电话。

这是我的代码。我不知道它是否正确。希望你们能帮助我,因为它给了我错误。我设法注册它。构建和注册没有问题,但是当创建案例时,它给了我错误。

using System;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Client;
using System.Net;
using System.Web.Services;


/*
 * Purpose: 1) To update case number into lejar
 *         
 * Triggered upon CREATE message by record in Case form.
 */
namespace UpdateLejar
{
    public class UpdateLejar : IPlugin
    {
        /*public void printLogFile(String exMessage, String eventMessage, String pluginFile)
        {
            DateTime date = DateTime.Today;
            String fileName = date.ToString("yyyyMdd");
            String timestamp = DateTime.Now.ToString();

            string path = @"C:\CRM Integration\PLUGIN\UpdateLejar\Log\" + fileName;
            //open if file exist, check file..
            if (File.Exists(path))
            {
                //if exist, append
                using (StreamWriter sw = File.AppendText(path))
                {
                    sw.Write(timestamp + " ");
                    sw.WriteLine(pluginFile + eventMessage + " event:  " + exMessage);
                    sw.WriteLine();
                }

            }

            else
            {
                //if no exist, create new file
                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.Write(timestamp + " ");
                    sw.WriteLine(pluginFile + eventMessage + " event:  " + exMessage);
                    sw.WriteLine();
                }
            }
        }*/

        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);

            //for update and create event
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parmameters.
                Entity targetEntity = (Entity)context.InputParameters["Target"];

                // Verify that the entity represents a connection.
                if (targetEntity.LogicalName != "incident")
                {
                    return;
                }

                else
                {
                    try
                    {

                        //triggered upon create message
                        if (context.MessageName == "Create")
                        {
                            Guid recordid = new Guid(context.OutputParameters["incidentid"].ToString());

                            EntityReference app_inc_id = new EntityReference();
                            app_inc_id = targetEntity.GetAttributeValue<EntityReference>("new_outboundcalllid");
                            Entity member = service.Retrieve("new_lejer", ((EntityReference)targetEntity["new_outboundcallid"]).Id, new ColumnSet(true));
                            //DateTime createdon = targetEntity.GetAttributeValue<DateTime>("createdon");

                            if (app_inc_id != null)
                            {
                                if (targetEntity.Attributes.Contains("new_outboundcallid") == member.Attributes.Contains("new_outboundcalllistid_lejer"))
                                {



                                    member["new_ringkasanlejarid"] = targetEntity.Attributes["incidentid"].ToString();
                                    service.Update(member);
                                }
                            }

                        }

                        tracingService.Trace("Lejar updated.");
                    }


                    catch (FaultException<OrganizationServiceFault> ex)
                    {
                        //printLogFile(ex.Message, context.MessageName, "UpdateLejar plug-in. ");
                        throw new InvalidPluginExecutionException("An error occurred in UpdateLejar plug-in.", ex);
                    }

                    catch (Exception ex)
                    {
                        //printLogFile(ex.Message, context.MessageName, "UpdateLejar plug-in. ");
                        tracingService.Trace("UpdateLejar: {0}", ex.ToString());
                        throw;
                    }
                }
            }
        }
    }
}
4

2 回答 2

3

请检查,

是否包含属性的实体。

检查并尝试:

if (targetEntity.Contains("new_outboundcallid"))
((EntityReference)targetEntity["new_outboundcallid"]).Id
于 2013-07-31T17:04:39.397 回答
2
member["new_ringkasanlejarid"] = targetEntity.Attributes["incidentid"].ToString();

new_ringkasanlejarid 的类型是什么?您正在为其设置一个字符串。如果 new_ringkasanlejarid 是实体引用,这可能会导致问题。

您可能想分享错误详细信息或跟踪日志,我们所能做的就是假设问题所在。

于 2013-04-24T13:24:57.343 回答