About the author

The author is just another obsessive compulsive hacker (the good kind) who when confronted with anything new can't help but wonder "how'd they do that?!?"

By day, I'm the Software Architect for CBMC; by night, I just try to "keep the clients happy" as Director of Technology for sdgInteractive.


MCPD

RSS Feed

ObjectTypeCode and Entity Creation

by hilkiah 20. May 2009 11:28

I’m doing more and more integration with Microsoft CRM and the new public CBMC website we’re getting to roll in a few weeks. The more I work with it, I’m struck by two distinct things: 1) the sheer power and flexibility of CRM as a platform (and I don’t like tossing the “p” word around too much – I think it’s over used these days) and 2) the significant vacuum of good documentation when going beyond the basics of working with the web services.

Today was a case in point. We’re trying to allow our members to do as much self-service as possible when it comes to maintaining their profile data. Historically, this has been somewhat of a bottleneck for us because that data was stored in a legacy donor system that has limited numbers of integration points and so many data changes required sending a request to the data entry staff who would update it manually. Obviously, that proves to be a pretty linear problem when it comes to staff cost as there are very limited economies of scale. Moving forward, we’re moving that data to CRM as the central storage hub and exposing the data directly to the end user via their profile and allowing them to update a lot more data directly for us without involving the data entry staff.

All that to say I was wrapping up the Address editing functionality  and I ran info a problem trying to do either a create or update on a customeraddress entity. CRM consistently would return the ever so cryptic “CRM 0x80040216 An unexpected error occurred” message. After much poking and prodding on Google, I discovered that problem was that a customeraddress entity can be associated with more than one type of parent entity. Therefore, setting the parentid property is not sufficient for the system to know what entity that particular address is associated with (for instance, it coudl be a contact or an account). Instead, you must also set the objecttypecode value to the entity type you’re associating it with using an EntityNameReference set to the name of the associated entity. Adding that piece let’s the save routine shown below work like a charm.

This is probably a no-brainer for CRM vets, but if you’re new to the system it’s doesn’t necessarily jump out at you.

   1: public Guid SaveProfileAddress(ProfileAddress address)
   2:         {
   3:             try
   4:             {
   5:                 using (CrmService crmService = GetCRMService())
   6:                 {
   7:                     DynamicEntity entity = new DynamicEntity { Name = "customeraddress" };
   8:                     entity.Properties.Add(new LookupProperty
   9:                                               {
  10:                                                   Name = "parentid",
  11:                                                   Value = new Lookup
  12:                                                               {
  13:                                                                   name = "parentid",
  14:                                                                   Value = address.UserID
  15:                                                               }
  16:                                               });
  17:                     entity.Properties.Add(new EntityNameReferenceProperty
  18:                                               {
  19:                                                   Name = "objecttypecode",
  20:                                                   Value = new EntityNameReference
  21:                                                               {
  22:                                                                   Value = EntityName.account.ToString()
  23:                                                               }
  24:                                               });
  25:                     entity.Properties.Add(new PicklistProperty
  26:                                               {
  27:                                                   Name = "addresstypecode",
  28:                                                   Value =
  29:                                                       new Picklist
  30:                                                           {
  31:                                                               name = "addresstypecode",
  32:                                                               Value = (int) address.AddressType
  33:                                                           }
  34:                                               });
  35:                     entity.Properties.Add(new StringProperty { Name = "line1", Value = address.Address1 });
  36:                     entity.Properties.Add(new StringProperty { Name = "line2", Value = address.Address2 });
  37:                     entity.Properties.Add(new StringProperty { Name = "line3", Value = address.Address3 });
  38:                     entity.Properties.Add(new StringProperty { Name = "city", Value = address.City });
  39:                     entity.Properties.Add(new StringProperty { Name = "stateorprovince", Value = address.State });
  40:                     entity.Properties.Add(new StringProperty { Name = "postalcode", Value = address.ZipCode });
  41:  
  42:                     if (address.AddressID == Guid.Empty) // Insert
  43:                     {
  44:                         TargetCreateDynamic targetCreate = new TargetCreateDynamic {Entity = entity};
  45:                         CreateRequest createRequest = new CreateRequest {Target = targetCreate};
  46:                         CreateResponse createResponse = (CreateResponse)crmService.Execute(createRequest);
  47:  
  48:                         if (createResponse != null)
  49:                         {
  50:                             return createResponse.id;
  51:                         }
  52:                     }
  53:                     else // Update
  54:                     {
  55:                         entity.Properties.Add(new KeyProperty
  56:                                                   {
  57:                                                       Name = "customeraddressid",
  58:                                                       Value = new Key { Value = address.AddressID }
  59:                                                   });
  60:  
  61:                         TargetUpdateDynamic targetUpdate = new TargetUpdateDynamic {Entity = entity};
  62:                         UpdateRequest updateRequest = new UpdateRequest {Target = targetUpdate};
  63:                         UpdateResponse updateResponse = (UpdateResponse) crmService.Execute(updateRequest);
  64:  
  65:                         if (updateResponse != null)
  66:                         {
  67:                             return address.AddressID;
  68:                         }
  69:                     }
  70:                 }
  71:             }
  72:             catch (SoapException spex)
  73:             {
  74:                 errorService.LogError(spex);
  75:             }
  76:             catch (Exception ex)
  77:             {
  78:                 errorService.LogError(ex);
  79:             }
  80:  
  81:             return Guid.Empty;
  82:         }

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

.NET | CRM | Web Services

Comments