locked
Plugin that creates a CustomerAddress and associates it to a contact RRS feed

  • Question

  • Hi,

    When converting a lead to a contact I would like the lead's address information to be added to the contact as a 'More address' record. To get this done, I decided to try to write a small plugin that intercepts the create message in the post stage. I have however some troubles to get the plugin to work. Let me first list the general steps that I think are required in the business logic part of my plugin.

    - check if the contact has a lead source, if not return 
    - create a new customeraddress record
    - add to the customeraddress record the address info from the lead
    - set the customeraddress parentid to the contactid and the objecttypecode value to 2
    - save the customeraddress

    Below I present the code that I wrote in order to implement these steps (in their most basic form).

       private void addContactAddress(Guid ContactID, ICrmService service)
    
            {
    
                DynamicEntity address = new DynamicEntity();
    
                address.Name = EntityName.customeraddress.ToString();
    
                address.Properties = new PropertyCollection();
    
                address.Properties.Add(new StringProperty("name", "My address"));
    
    
    
                Lookup lookup = new Lookup();
    
                lookup.Value = ContactID;
    
                lookup.type = EntityName.contact.ToString();
    
                address.Properties.Add(new LookupProperty("parentid", lookup));
    
    
    
                Picklist objecttype = new Picklist();
    
                objecttype.Value = 2;
    
                address.Properties.Add(new PicklistProperty("objecttypecode", objecttype));
    
    
    
                TargetCreateDynamic targetCreate = new TargetCreateDynamic();
    
                targetCreate.Entity = address;
    
                CreateRequest create = new CreateRequest();
    
                create.Target = targetCreate;
    
                CreateResponse created = (CreateResponse)service.Execute(create);
    
            }
    
    

    The registration process of the plugin works fine. When the plugin is triggered however, Dynamics displays an unspecified error. Now I am hoping that someone could perhaps point out my mistakes in the above code (should I perhaps fill more fields of the customeraddress, or do something else differently?)

    I'm hoping someone could help me out.

    Many thanks in advance!

    Ygor

    PS. Building, signing, registration and testing of the sample plugins all work fine.

    Thursday, April 23, 2009 11:35 PM

Answers

  • Hi Ygor,

    I recommend to debug your PlugIn. You can find the information in the CRM SDK.

    Then you will get a better error message which can be the solution for your problem.

    Best regards,
    Jürgen
    Jürgen Beck

    Dipl. Kfm./Wirtschaftsinformatik
    MVP, MCSD.NET, MCITP DBA, MCDBA, MCSE
    Microsoft Certified Business Management Solutions Professional
    Microsoft Certified CRM Developer
    Microsoft Certified Trainer

    ComBeck IT Services & Business Solutions
    Microsoft Gold Certified Partner
    Microsoft Small Business Specialist

    Developing & Supporting Business Applications from small business to big enterprises covering scores of sectors

    http://www.combeck.de
    Friday, April 24, 2009 7:37 AM
    Moderator
  • Here you can find the Step-by-Step description for debugging a PlugIn:
    http://msdn.microsoft.com/en-us/library/cc151088.aspx

    I hope you do not a a Visual Studio Express edition, because you can not debug an other process with the Express edition.

    Best regards,
    Jürgen
    Jürgen Beck

    Dipl. Kfm./Wirtschaftsinformatik
    MVP, MCSD.NET, MCITP DBA, MCDBA, MCSE
    Microsoft Certified Business Management Solutions Professional
    Microsoft Certified CRM Developer
    Microsoft Certified Trainer

    ComBeck IT Services & Business Solutions
    Microsoft Gold Certified Partner
    Microsoft Small Business Specialist

    Developing & Supporting Business Applications from small business to big enterprises covering scores of sectors

    http://www.combeck.de
    Friday, April 24, 2009 7:40 AM
    Moderator
  • Hi
        In 'customeraddress' entity 'objecttypecode' is not of type Picklist but EntityNameReference. Change it and if guid is correct it schould work.
    everything is a matter of probability...
    Friday, April 24, 2009 9:00 AM

All replies

  • Hi Ygor,

    I recommend to debug your PlugIn. You can find the information in the CRM SDK.

    Then you will get a better error message which can be the solution for your problem.

    Best regards,
    Jürgen
    Jürgen Beck

    Dipl. Kfm./Wirtschaftsinformatik
    MVP, MCSD.NET, MCITP DBA, MCDBA, MCSE
    Microsoft Certified Business Management Solutions Professional
    Microsoft Certified CRM Developer
    Microsoft Certified Trainer

    ComBeck IT Services & Business Solutions
    Microsoft Gold Certified Partner
    Microsoft Small Business Specialist

    Developing & Supporting Business Applications from small business to big enterprises covering scores of sectors

    http://www.combeck.de
    Friday, April 24, 2009 7:37 AM
    Moderator
  • Here you can find the Step-by-Step description for debugging a PlugIn:
    http://msdn.microsoft.com/en-us/library/cc151088.aspx

    I hope you do not a a Visual Studio Express edition, because you can not debug an other process with the Express edition.

    Best regards,
    Jürgen
    Jürgen Beck

    Dipl. Kfm./Wirtschaftsinformatik
    MVP, MCSD.NET, MCITP DBA, MCDBA, MCSE
    Microsoft Certified Business Management Solutions Professional
    Microsoft Certified CRM Developer
    Microsoft Certified Trainer

    ComBeck IT Services & Business Solutions
    Microsoft Gold Certified Partner
    Microsoft Small Business Specialist

    Developing & Supporting Business Applications from small business to big enterprises covering scores of sectors

    http://www.combeck.de
    Friday, April 24, 2009 7:40 AM
    Moderator
  • Hi
        In 'customeraddress' entity 'objecttypecode' is not of type Picklist but EntityNameReference. Change it and if guid is correct it schould work.
    everything is a matter of probability...
    Friday, April 24, 2009 9:00 AM
  • Hi,

    I changed my code now into the following.

                ICrmService service = context.CreateCrmService(true);
                    Guid ContactID = new Guid(context.OutputParameters.Properties["id"].ToString());
                    
                    DynamicEntity address = new DynamicEntity(EntityName.customeraddress.ToString());
                    address.Properties = new PropertyCollection();
                    address.Properties.Add(new StringProperty("name", "My address"));
                    address.Properties.Add(new StringProperty("line1", "Mijn adres regel 1"));
    
                    Lookup lookup = new Lookup(EntityName.contact.ToString(), ContactID);
                    address.Properties.Add(new LookupProperty("parentid", lookup));
    
                    EntityNameReference objecttypecode = new EntityNameReference();
                    objecttypecode.Value = "2";
                    address.Properties.Add(new EntityNameReferenceProperty("objecttypecode", objecttypecode));
    
                    service.Create(address);
    The error message has now changed; I get a message box saying "No entity". Do you have any idea what that means?
    Furthermore, where can I find information on which fields (properties) are required to be set when creating a new DynamicEntity and saving it?

    Thanks,

    Ygor
    Friday, April 24, 2009 9:08 AM
  • Change objecttypecode.Value to "contact"
    everything is a matter of probability...
    Friday, April 24, 2009 9:13 AM
  • Yeps, just noticed that mistake after I submitted my question. But still, many thanks for the quick reply! Do you perhaps also have an answer to my other question: where do I find the types of the different properties and where do I find which properties need to be set in order for the dynamic entity to be properly created?

    Thanks again,

    Ygor 
    Friday, April 24, 2009 9:16 AM
  • You can know that from SDK help by looking at entity description and type that it is of. Maybe you can get this iformation from metadata service but I never tried it.

    everything is a matter of probability...
    Friday, April 24, 2009 9:52 AM