Problem changing customer from contact to accoun on incident create using plugin MS CRM 2011

คำตอบ Problem changing customer from contact to accoun on incident create using plugin MS CRM 2011

  • 24 เมษายน 2555 12:21
     
      มีโค้ด

    This is a bit strange. When a user created an Incident from an e-mail the sender is assigned as customer on the case. The e-mail comes from a contact and I like to have the contacts parent account as being listed as the customer on the incident and then write the contact information to another field.

    To achieve this I have created a plugin that runs on the pre-create event of the incident. Everything works fine with the exception the customer name that is shown on the incident form. The customer link is now linking to the contacts parent account but the name in the customer field remains that of the contact.

    I have also tried updating the incident on the create post event but with the same result. The text in the customer field remains the same even if its linking to another entitytype. Any suggestions to why this is?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xrm.Sdk;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk.Query;
    
    namespace JDM.Incident.Create
    {
        public class AssignCustomerToIncident: IPlugin
        {
    
            IOrganizationService service;
    
            public void Execute(IServiceProvider serviceProvider)
            {   
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                service = serviceFactory.CreateOrganizationService(context.UserId);
                
                Entity entity;
                            
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is Entity)
                {
                     entity = (Entity)context.InputParameters["Target"];
                    
                    if (entity.Attributes.Contains("customerid"))
                    { 
                        EntityReference customerref = (EntityReference)entity.Attributes["customerid"];
    
                        if (customerref.LogicalName.Equals("contact"))
                        {     
                            EntityReference parentaccount = GetParentAccountId(customerref.Id);
                            
                            if (parentaccount != null)
                            {                            
                                EntityReference contact = new EntityReference();
                                contact.Id          = customerref.Id;
                                contact.LogicalName = customerref.LogicalName;
                                contact.Name        = customerref.Name;
                                
                                entity.Attributes.Add("jdm_kontaktperson", contact);
                                                            
                                entity.Attributes["customerid"] = parentaccount;
                            }
                        }
                    }
                }
            }
    
            private EntityReference GetParentAccountId(Guid contactid)
            {
                Entity e = (Entity)service.Retrieve("contact", contactid, new ColumnSet(new string[] { "parentcustomerid" }));
    
                if (e.Attributes.Contains("parentcustomerid"))
                    return (EntityReference)e.Attributes["parentcustomerid"];
                else
                    return new EntityReference();
            }
        }
    }
    

ตอบทั้งหมด

  • 24 เมษายน 2555 12:33
     
     

    Try by setting  

    entity.Attributes["customerid"] = new EntityReference("account",parentaccount.Id);


    Thanks Anil Chelasani http://exploringxrm.wordpress.com

  • 24 เมษายน 2555 20:00
     
     

    The problem remains the same. Customer is changes from a contact to an account but the customer name is still also the contacts name. This is very strange.

  • 24 เมษายน 2555 20:26
     
     
    Rollup 6 is installed on the server. I know this is not the newest but I'm sure this is not the reason for this.
  • 25 เมษายน 2555 6:28
    ผู้ดูแล
     
     คำตอบ

    The customer data-type is handled in a specific way within the plugin pipeline. I can think of 2 things you could try:

    1. Register the plugin on the pre-event outside of the transaction (stage 10)
    2. Modify the accountid / contactid attributes as well as / instead of the customerid attribute. Whether you will be permitted to modify these may differ depending on which pipeline stage the plugin is registered on

    Microsoft CRM MVP - http://mscrmuk.blogspot.com  http://www.excitation.co.uk


  • 25 เมษายน 2555 12:14
     
      มีโค้ด
    Try setting the name explicitly on the entity reference:
    var eref = new EntityReference
                                   {Id = parentaccount.Id, LogicalName = "account", Name = "S. Vindel  & B. Drag"};

  • 26 เมษายน 2555 11:09
     
     
    Thank you DavidJennaway. My mistake was that I registered the plugin to run on the Pre-Operation event (stage 20) and not on the Pre-Validation event (stage 10).