public override void ExecuteCRMWorkFlowActivity(CodeActivityContext executionContext, LocalWorkflowContext crmWorkflowContext) { string uprn; string buildingnumber; string street; string postcode; string country; uprn = UPRN.Get(executionContext); buildingnumber = BuildingNumber.Get(executionContext); street = Street.Get(executionContext); postcode = PostCode.Get(executionContext); country = Country.Get(executionContext); street = Street.Get(executionContext); SCII.Helper objCommon = new SCII.Helper(executionContext); AddressData addressData = new AddressData(); Guid addressId = Guid.Empty; Guid contactDetailId = Guid.Empty; OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(objCommon.service); if (!string.IsNullOrEmpty(country)) { string countryValue = country.ToUpper(); crmWorkflowContext.Trace("Country search started" + country); var CountryRecord = from c in orgSvcContext.CreateQuery(SCS.Address.COUNTRY) where (((string)c["defra_isocodealpha3"]) == countryValue) && (int)c[SCS.ContactDetails.STATECODE] == 0 select new { CountryId = c.Id }; Guid countryGuid = CountryRecord != null && CountryRecord.FirstOrDefault() != null?CountryRecord.FirstOrDefault().CountryId : Guid.Empty; crmWorkflowContext.Trace("country found" + countryGuid); if (countryGuid != Guid.Empty) { EntityReference entityRef = new EntityReference(SCS.Address.COUNTRY, countryGuid); OutCountryEntityRef.Set(executionContext, entityRef); } crmWorkflowContext.Trace("Finished: Defra.CustMaster.Identity.WfActivities.ExecuteCRMWorkFlowActivity.CustomerAddress"); } if (country.Trim().ToUpper() == "GBR") { if (uprn != null) { crmWorkflowContext.Trace("UPRN search:started.."); var propertyWithUPRN = from c in orgSvcContext.CreateQuery(SCS.Address.ENTITY) where ((string)c[SCS.Address.UPRN]).Equals(uprn.Trim()) && (int)c[SCS.ContactDetails.STATECODE] == 0 select new { AddressId = c.Id }; addressId = propertyWithUPRN != null && propertyWithUPRN.FirstOrDefault() != null?propertyWithUPRN.FirstOrDefault().AddressId : Guid.Empty; crmWorkflowContext.Trace("UK UPRN Address:" + addressId); } if (addressId == Guid.Empty && street != null && postcode != null && buildingnumber != null) { crmWorkflowContext.Trace("postcode and street search:started"); var propertyWithDuplicate = from c in orgSvcContext.CreateQuery(SCS.Address.ENTITY) where ((string)c[SCS.Address.STREET]).Equals(street.Trim()) && ((string)c[SCS.Address.POSTCODE]).Equals(postcode.Trim()) && ((string)c[SCS.Address.PREMISES]).Equals(buildingnumber.Trim()) && (int)c[SCS.ContactDetails.STATECODE] == 0 select new { AddressId = c.Id }; addressId = propertyWithDuplicate != null && propertyWithDuplicate.FirstOrDefault() != null?propertyWithDuplicate.FirstOrDefault().AddressId : Guid.Empty; crmWorkflowContext.Trace("UK PostCode address:" + addressId); } } else { if (addressId == Guid.Empty && street != null && postcode != null && buildingnumber != null) { crmWorkflowContext.Trace("postcode and street search:started"); var propertyWithDuplicate = from c in orgSvcContext.CreateQuery(SCS.Address.ENTITY) where ((string)c[SCS.Address.STREET]).Equals(street.Trim()) && ((string)c[SCS.Address.INTERNATIONALPOSTCODE]).Equals(postcode.Trim()) && ((string)c[SCS.Address.PREMISES]).Equals(buildingnumber.Trim()) && (int)c[SCS.ContactDetails.STATECODE] == 0 select new { AddressId = c.Id }; addressId = propertyWithDuplicate != null && propertyWithDuplicate.FirstOrDefault() != null?propertyWithDuplicate.FirstOrDefault().AddressId : Guid.Empty; crmWorkflowContext.Trace("Non UK Internaltional PostCode address:" + addressId); } } if (addressId != Guid.Empty) { EntityReference entityRef = new EntityReference("defra_address", addressId); OutAddressEntityRef.Set(executionContext, entityRef); } }
public override void ExecuteCRMWorkFlowActivity(CodeActivityContext executionContext, LocalWorkflowContext crmWorkflowContext) { string uprn; string buildingnumber; string buildingname; string street; string postcode; string country; int type; uprn = UPRN.Get(executionContext); buildingnumber = BuildingNumber.Get(executionContext); buildingname = BuildingName.Get(executionContext); street = Street.Get(executionContext); postcode = PostCode.Get(executionContext); country = Country.Get(executionContext); street = Street.Get(executionContext); SCII.Helper objCommon = new SCII.Helper(executionContext); type = AddressType.Get(executionContext); Guid customerId = string.IsNullOrEmpty(CustomerId.Get(executionContext)) ? Guid.Empty : new Guid(CustomerId.Get(executionContext)); AddressData addressData = new AddressData(); Guid addressId = Guid.Empty; Guid contactDetailId = Guid.Empty; OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(objCommon.service); try { if (string.IsNullOrEmpty(buildingname)) { if (string.IsNullOrEmpty(buildingnumber)) { throw new Exception("Provide either building name or building number, Building name is mandatory if the building number is empty;"); } } if (!Enum.IsDefined(typeof(SCII.AddressTypes), type)) { throw new Exception("Option set value for address of type not found;" + type); } if (string.IsNullOrEmpty(postcode)) { throw new Exception("Postcode is required"); } else if (postcode.Length > 8) { throw new Exception("postcode length can not be greater than 8 for UK countries;"); } if (string.IsNullOrEmpty(country)) { throw new Exception("Country is required"); } else if (country.Length > 3) { throw new Exception("Country ISO ALPHA - 3 Code cannot be greater than 3;"); } if (country.Trim().ToUpper() == "GBR") { if (postcode.Length > 8) { throw new Exception("postcode length can not be greater than 8 for UK countries;"); } } else { if (postcode.Length > 25) { throw new Exception("postcode length can not be greater than 25 for NON-UK countries;"); } } if (customerId != Guid.Empty) { var contactDetailsWithType = from c in orgSvcContext.CreateQuery(SCS.ContactDetails.ENTITY) where ((string)c[SCS.ContactDetails.ADDRESSTYPE]).Equals(type) && ((EntityReference)c[SCS.ContactDetails.CUSTOMER]).Id.Equals(customerId) select new { contactDetailsId = c.Id }; contactDetailId = contactDetailsWithType != null && contactDetailsWithType.FirstOrDefault() != null?contactDetailsWithType.FirstOrDefault().contactDetailsId : Guid.Empty; if (contactDetailId != Guid.Empty) { throw new Exception("Contact details of same type already exist for this customer:" + contactDetailId); } } } catch (Exception ex) { crmWorkflowContext.Trace(ex.Message); throw ex; } }