/// <summary> /// Executes the WorkFlow. /// </summary> /// <param name="crmWorkflowContext">The <see cref="LocalWorkflowContext"/> which contains the /// <param name="executionContext" > <see cref="CodeActivityContext"/> /// </param> /// <remarks> /// For improved performance, Microsoft Dynamics 365 caches WorkFlow instances. /// The WorkFlow's Execute method should be written to be stateless as the constructor /// is not called for every invocation of the WorkFlow. Also, multiple system threads /// could execute the WorkFlow at the same time. All per invocation state information /// is stored in the context. This means that you should not use global variables in WorkFlows. /// </remarks> public override void ExecuteCRMWorkFlowActivity(CodeActivityContext executionContext, LocalWorkflowContext crmWorkflowContext) { if (crmWorkflowContext == null) { throw new ArgumentNullException("crmWorkflowContext"); } try { var companyRegNumber = CompanyRegistrationNumber.Get(executionContext); var account = new Entity(crmWorkflowContext.WorkflowExecutionContext.PrimaryEntityName) { Id = crmWorkflowContext.WorkflowExecutionContext.PrimaryEntityId }; // TODO: Implement your custom activity handling. if (!string.IsNullOrEmpty(companyRegNumber) && account.Id != Guid.Empty) { //OS Places Service used for address matching var OSTARGETURL = "https://api.ordnancesurvey.co.uk/"; var OSAPIKey = "BIrATYCSumrlXQuQkpOE0D2WPy1qBB5p"; var osService = new OSPlacesService(OSTARGETURL, OSAPIKey); //Companies House Service var CHTARGETURL = "https://api.companieshouse.gov.uk"; var CHAPIKey = "R63PBUpJhDM5_GrZ0XDN0Fe2aksMjSJU7EDj6DJ4"; var chService = new CompaniesHouseServiceDynamics(CHTARGETURL, CHAPIKey, companyRegNumber, crmWorkflowContext.OrganizationService, crmWorkflowContext.TracingService); chService.ValidateCustomer(account, osService); } } catch (FaultException <OrganizationServiceFault> e) { // Handle the exception. throw e; } }
public void ValidateCustomer(Entity Account, OSPlacesService osService) { if (Account.Id != Guid.Empty && !string.IsNullOrEmpty(this.CompanyRegistrationNumber)) { _crmTracing.Trace(string.Format("Validate Customer Account {0} with Company Registration Number {1}", Account.Id.ToString(), this.CompanyRegistrationNumber)); //Retrieve the account Account = _crmService.Retrieve(Account.LogicalName, Account.Id, new ColumnSet("name", "defra_validatedwithcompanyhouse", "defra_companyhousestatus")); //Get the company details this.GetCompany(); if (this.Company != null && !string.IsNullOrEmpty(this.Company.company_name)) { //Create or fetch the Contact Details Entity contactDetails = this.GetCustomerRegAddressContactDetails(Account); //Get the company registered address from CH this.GetRegisteredAddress(); //Try match the registered address with OS Places if (this.Company.registered_office_address != null) { //Create or update the contact details address this.CreateOrUpdateRegisteredAddress(contactDetails); } _crmTracing.Trace(string.Format("Company Status = {0}", this.Company.company_status.ToString())); //Update the customer bool accountToBeUpdated = false; if (!Account.Attributes.Contains("name") || (string)Account["name"] != this.Company.company_name) { Account["name"] = this.Company.company_name; accountToBeUpdated = true; } if (!Account.Attributes.Contains("defra_companyhousestatus") || (CompaniesHouseCompanyStatus)((OptionSetValue)Account["defra_companyhousestatus"]).Value != this.Company.company_status) { Account["defra_companyhousestatus"] = new OptionSetValue((int)this.Company.company_status); accountToBeUpdated = true; } if (!(bool)Account["defra_validatedwithcompanyhouse"]) { Account["defra_validatedwithcompanyhouse"] = true; accountToBeUpdated = true; } if (accountToBeUpdated) { _crmService.Update(Account); } //Try match address with OS Places //OSPlacesAddress osAddress = osService.MatchCompaniesHouseAddress(this.Company.registered_office_address); //Get the company directords this.GetCompanyDirectors(); //Create the Company Directors this.CreateDirectors(Account); } else { throw new InvalidPluginExecutionException(string.Format("Company with Company Registration Number {0} has not been found", this.CompanyRegistrationNumber)); } } }