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: }