Показать сообщение отдельно
Старый 25.11.2013, 18:52   #10  
Space-06 is offline
Space-06
Участник
 
57 / 10 (1) +
Регистрация: 23.01.2013
Пытаясь изменить код под свои нужды, столкнулся вот с какой проблемой, вроде все делаю так как надо:

X++:
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Crm.Sdk.Messages;
using System.ServiceModel;

namespace SetAccountName
{
    public class SetAccountName : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Получаемо сервис CRM из контекста
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            // Получаемо объект Target
            Entity entity = (Entity)context.InputParameters["Target"];
            ColumnSet cs = new ColumnSet();
            //try
            //{
                if (entity.LogicalName == "phonecall")
                {
                    if (entity.Contains("regardingobjectid"))
                    {
                        Guid phoneid = entity.Id;
                        //Entity PhoneCall = service.Retrieve("phonecall", phoneid, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
                        Entity PhoneCall = service.Retrieve("phonecall", phoneid, cs);
                        EntityReference regarding = ((EntityReference)PhoneCall.Attributes["regardingobjectid"]);
                        
                        switch (regarding.LogicalName.ToString())
                        { 
                            case "account":
                                Entity account = service.Retrieve("account", regarding.Id, cs);
                                entity.Attributes.Add("new_nameofaccount", account.FormattedValues.ToString());
                            //entity.Attributes["new_nameofaccount"] = account.FormattedValues["name"].ToString();
                                //entity.Attributes["new_nameofaccount"] = account.;
                                //entity.Attributes["new_companyname"] = ((EntityReference)account.Attributes["name"]);
                                break;
                            case "lead":
                                Entity lead = service.Retrieve("lead", regarding.Id, cs);
                                entity.Attributes["new_nameofaccount"] = lead.FormattedValues["new_companyname"].ToString();
                                break;
                            case "opportunity":
                                Entity opport = service.Retrieve("opportunity", regarding.Id, cs);
                                entity.Attributes["new_nameofaccount"] = opport.FormattedValues["customerid"].ToString();
                                break;
                        }
                        service.Update(PhoneCall);
                    }
              }
                       
//catch (Exception ex)
//                {
//                    throw new InvalidPluginExecutionException("An error occured in the Plugin.", ex);
//                }        
        
        }

    }   
    }
и все равно поле не обновляется.

Прошу сильно не пинать меня ногами. Это первый так сказать опыт по написанию своего собственного плагина, и как говорится сроки очень сильно поджимают. Подскажите - где мог ошибится и как все таки правильно реализовать получение значения имени организации в первом Кейсе.

За ранее спасибо.