Показать сообщение отдельно
Старый 03.04.2014, 17:01   #8  
Lavdislav is offline
Lavdislav
Участник
 
34 / 10 (1) +
Регистрация: 28.02.2014
Цитата:
Сообщение от maksii Посмотреть сообщение
Я сомневаюсь, что мой код будет работать, хотя вроде должен.

Ну а так, если я правильно понял:
Было:
Код:
if (!record.Contains("subject") || record["subject"].ToString() != uniq)
                {
                    uniq = string.Format("{0}", uniq.Length > uniqLength ? (uniq.Substring(0, uniqLength - 3) + "...") : uniq);
                    record["subject"] = uniq;
                    crmService.Update(record);
                }
Стало:

Код:
if (!record.Contains("subject") || record["subject"].ToString() != uniq)
                {
                    uniq = string.Format("{0}", uniq.Length > uniqLength ? (uniq.Substring(0, uniqLength - 3) + "...") : uniq);
                    record["subject"] = uniq;
                    if(executionContext.MessageName == MessageName.Update) 
                     {
                    crmService.Update(record);
                    }
                }
Ну и мой код должен выглядить как-то так, исходя из вашего класса:

Код:
            Entity yourEntityName = (Entity)executionContext.InputParameters["Target"];

            if(yourEntityName.Attributes.Contains("subject"))
            yourEntityName.Attributes["subject"] = yourEntityName.GetAttributeValue<EntityReference>("regardingobjectid").Name;
                    if(executionContext.MessageName == MessageName.Update) 
                    {
                    crmService.Update(yourEntityName );
                    }
Я только сомневаюсь, что у EntityReference имя референса называется Name
Получилось у меня что-то типо такого:
Код:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Bum.Survey.CRM.Plugin.BaseLib;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;

namespace Bum.Survey.CRM.Plugin
{
    public class EvalObjectCreateTest : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            var process = new EvalObjectCreateTestProcess(serviceProvider);
            process.LoadEntity();
            if (!process.ValidateEntityName("bf_evaluatingobject")) return;
            if (!process.ValidateMessage(MessageName.Create, MessageName.Update)) return;
            process.Run();
        }
    }

    class EvalObjectCreateTestProcess : bf_PluginProcess
    {
        public override void Execute()
        {
            if (TargetEntity.Contains("regardingobjectid"))
            {
                string uniq = "";

                int uniqLength = 1024;

                Entity record = crmService.Retrieve(TargetEntity.LogicalName, TargetEntity.Id, new ColumnSet("subject", "regardingobjectid"));

                if (record.Contains("regardingobjectid"))
                {
                    uniq = record.GetAttributeValue<EntityReference>("regardingobjectid").Name;

                    RetrieveEntityRequest rerq = new RetrieveEntityRequest
                    {
                        LogicalName = record.GetAttributeValue<EntityReference>("regardingobjectid").LogicalName,
                        RetrieveAsIfPublished = true
                    };

                    RetrieveEntityResponse rers = (RetrieveEntityResponse)crmService.Execute(rerq);

                    uniq = rers.EntityMetadata.DisplayName.UserLocalizedLabel.Label + " " + "[" + uniq + "]";
                }

                if (!record.Contains("subject") || record["subject"].ToString() != uniq)
                {
                    uniq = string.Format("{0}", uniq.Length > uniqLength ? (uniq.Substring(0, uniqLength - 3) + "...") : uniq);
                    record["subject"] = uniq;
                    if (executionContext.MessageName == MessageName.Update)
                    {
                        crmService.Update(record);
                    }
                }
            }

            Entity yourEntityName = (Entity)executionContext.InputParameters["Target"];

            if (yourEntityName.Attributes.Contains("subject"))
                yourEntityName.Attributes["subject"] = yourEntityName.GetAttributeValue<EntityReference>("regardingobjectid").Name;
            if (executionContext.MessageName == MessageName.Update)
            {
                crmService.Update(yourEntityName);
            }
        }

        public EvalObjectCreateTestProcess(IServiceProvider serviceProvider)
            : base(serviceProvider)
        {
        }
    }
}
На Create ставлю Pre-Operation, на Update ставлю Post-Operation, при создании выдаёт ту же ошибку - bf_evaluatingobject with id ... does not exist. Явно я тупак и не могу допереть))